简体   繁体   中英

Ruby on Rails: Loop through check boxes of email addresses and send an email to each?

I am trying to create a system in RoR to send out emails, and what I want a user to be able to do is select multiple email addresses through checkboxes on a form and the system loop through each of those selected checkboxes sending an email to each. At the minute a user can select multiple email addresses on the form and the system joins them together so that one email is sent to multiple recipients, eg the user selects the email addresses "1@a.com" and "2@b.com" and the system sends an email to "1@a.com;2@b.com".

I have done this through the view:

<%= form_for(@email) do |f| %>
  <% if @email.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@email.errors.count, "error") %> prohibited this email from being saved:</h2>

      <ul>
      <% @email.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <p>
    <%= f.label :from_name %><br>
    <% if @user.present? %>
    <%= f.select :from_name, [@user.firstname + " " + @user.surname, @user.organisation] %>
    <% end %>
  </p>

  <p>
    <%= f.label :From_Email_Address %><br>
    <%= f.collection_select :account_id, Account.where(user_id: session[:user_id]), 
      :id,:email %>
  </p>

  <p>
    <%= f.label :to %><br>
    <%= f.collection_check_boxes(:to, @contacts, :email, :email) %>
  </p>

  <p>
    <%= f.label :cc %><br>
    <%= f.text_field :cc %>
  </p>

  <p>
    <%= f.label :bcc %><br>
    <%= f.text_field :bcc %>
  </p>

  <p>
    <%= f.label :subject %><br>
    <%= f.text_field :subject %>
  </p>

  <p>
    <%= f.label :message %><br>
    <%= f.text_field :message %>
  </p>

  <div class="actions">
    <%= f.submit %>
  </div>

With the line <%= f.collection_check_boxes(:to, @contacts, :email, :email) %> showing the checkboxes for each of the email addresses saved in the contacts database table.

This then goes through to the emails_controller , which has the following code:

class EmailsController < ApplicationController
  before_action :set_email, only: [:show, :edit, :update, :destroy]

  def index
    # Only show the email accounts for the user logged in
    @emails = Email.where(user_id: session[:user_id])
  end

  def show
  end

  def new
    @user = User.find(session[:user_id])
    @contacts = Contact.where(user_id: session[:user_id])
    @email = Email.new
  end

  def edit
    @user = User.find(session[:user_id])
    @contacts = Contact.where(user_id: session[:user_id])
  end

  def create
    @email = Email.new(email_params)
    @email.user_id = session[:user_id]
    @user = session[:user_id]
    if @email.save
        UserEmails.send_email(@email).deliver_now
        redirect_to @email, notice: 'Email was successfully created.'
    else
        redirect 'new'
    end
  end

  def update
      if @email.update(email_params)
        redirect_to @email, notice: 'Email was successfully updated.'
      else
        redirect 'edit'
      end
  end

  def destroy
    @email.destroy
    redirect_to emails_url, notice: 'Email was successfully destroyed.'
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_email
          @email = Email.find(params[:id])
end

    # Never trust parameters from the scary internet, only allow the white list through.
    def email_params 
        res = params.require(:email).permit(:account_id, :cc, :bcc, :subject, :message, :from_name, to: []) 
        res[:to] = res[:to].join('; ') 
        res 
    end
end

With the email_params code joining the email addresses selected together with a ";" between each one.

As you can see above in the create method, the code UserEmails.send_email(@email).deliver_now is ran once the email is saved - this code sends the email out.

So, what my question is, is whether anyone can help me to change the code so that one email is sent to each of the email addresses selected on the form?

Below is what my form with the check boxes looks like.

在此处输入图片说明

Use Multiple Selection box and then select2 for making email address selection box like google does after that following code works

Your form

  <div class="form-group"> <%= f.label :select_user_email %><br/> <select class="form-control select2" name="user[email][]"> <option value="" selected disabled>Enter email address</option> <%@users.each do |user|%> <option value="<%=user.email%>"><%=user.name%></option> <%end%> </select> </div> 

def create
  @emails = (params[:email]).split(",")
  @emails.each do |email|
   UserMailer(email, @message).deliver_now
end
end

Remember select2 is a javascript and css for making selection box like this 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM