【问题标题】:How to send emails to multiple recipients using the same form field如何使用相同的表单域向多个收件人发送电子邮件
【发布时间】:2015-11-05 19:48:52
【问题描述】:

我有一个与每个记分牌相关的邀请功能。每个记分牌都有_许多已发送的_邀请,并且每个邀请都属于_一个记分牌。该功能运行良好。当前界面允许管理员分别输入每个电子邮件地址以发送邀请。

但是,我想改进界面,允许管理员以相同的格式输入多个电子邮件地址,用逗号分隔或突出显示以让用户知道电子邮件已完成,然后发送一封电子邮件至所有这些电子邮件地址。

我不确定如何让我的表单接受以逗号或任何其他方式分隔的多封电子邮件?我不认为 rails 为此提供了不同的表单助手。我将如何在我的控制器中处理这个?我将如何为此设置正则表达式?我从未实现过一种接受多个电子邮件地址然后向每个人发送一封电子邮件的表单。有关此主题的任何信息都会非常有帮助。

新表单的代码。

<h1>Invitation</h1>
<div class="row">
    <div class= "col-md-4 col-md-offset-4">
      <%= form_for [@scoreboard, @invitation] do |f| %> <!-- you have to pass in the scoreboard id into the forms as well -->
        <%= render 'shared/error_messages', object: f.object %>

        <%= f.label :recipient_name %>
        <%= f.text_field :recipient_name, class: "form-control", placeholder: "First and last name." %>

        <%= f.label :recipient_email, placeholder: "Enter email" %>
        <%= f.text_field :recipient_email, class: "form-control", placeholder: "Enter a valid email address." %>

        <%= f.submit "Send Invitation", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

邀请控制器代码如下:

def create
        @scoreboard = Scoreboard.find(params[:scoreboard_id])
        @invitation = @scoreboard.sent_invitations.build(invitation_params)
        if @invitation.save && User.exists?(email: @invitation.recipient_email) == true
            flash[:success] = "Invitation sent successfully"
            UserMailer.registered_invitation_email(@scoreboard, @invitation_email).deliver_now
            redirect_to new_scoreboard_invitation_path
         elsif
             @invitation.save && User.exists?(email: @invitation.recipient_email) == false
             UserMailer.non_registered_invitation_email(@scoreboard, @invitation).deliver_now
             flash[:success] = "Invitation sent successfully"
             redirect_to new_scoreboard_invitation_path
        else
            render 'new'
        end
    end
end

【问题讨论】:

    标签: ruby-on-rails email


    【解决方案1】:

    我会编写另一种方法来拆分电子邮件参数,例如emails = params[:recipient_email].split(",")。这将创建一个由逗号分隔的所有电子邮件的数组。然后做: @invitations = [] emails.each {|email| @invitations << @scoreboard.sent_invitations.build(email)} 或类似的东西。

    【讨论】:

      猜你喜欢
      • 2012-09-24
      • 2019-03-01
      • 2012-05-18
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多