I am creating a system to send out emails through Ruby on Rails, and what I want a user to be able to do is select multiple email addresses 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, e.g. 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.
Aucun commentaire:
Enregistrer un commentaire