Send to multiple recipients in Rails with ActionMailer

天大地大妈咪最大 提交于 2019-11-28 18:33:33

i'm having this same problem.. dunno what the deal is.. I sidestep it by:

instead of calling

Mailer.request_replacement(shift).deliver 

from my controller,

I'd define a class method on the mailer, and call that. That method would then iterate through the list and call deliver "n" times... that seems to work

class Mailer

   def self.send_replacement_request(shift)
     @recipients = ...
     @recipients.each do |recipient|
       request_replacement(recipient, shift).deliver
     end
   end

   def request_replacement(recipient, shift)
     ...
     mail(...)
   end
end

and from the controller, call

Mailer.send_replacement_request(shift)

You can just send one email for multiple recipients like this.

def request_replacement(shift)
  @shift = shift
  @user = shift.user
  @recipients = User.where(:replacement_emails => true)
  @url  = root_url
  emails = @recipients.collect(&:email).join(",")
  mail(:to => emails, :subject => "A replacement clerk has been requested")
end

This will take all your @recipients email addresses and join them with ,. I think you can also pass an array to the :to key but not sure.

The only problem is you won't be able to use @name in your template. :(

In the Rails guides (Action Mailer Basics) it says the following regarding multiple emails:

The list of emails can be an array of email addresses or a single string with the addresses separated by commas.

So both "test1@gmail.com, test1@gmail.com" and ["test1@gmail.com", "test1@gmail.com"] should work.

See more at: http://guides.rubyonrails.org/action_mailer_basics.html

To prevent each recipient from seeing the other email addresses:

@recipients.each{ |recipient| Mailer.request_replacement(recipient, shift).deliver }

I'm using Rails 5 and I have the same situation, the email was sent only to the last recipient but also it was sent just as plain text not as HTML email.

After trying some advices, I ended up fixing it in this way:

The mailer:

class BrochureMailer < ApplicationMailer
    default from: "info@site.com"

    def newsletter(sponsor, brochures_list)
        @sponsor = sponsor
        @brochures = brochures_list

        mail(
            to: @sponsor.email,
            subject: "Interesting subject!"
        )
    end
end

The controller where the mailer is invoked:

class Admin::DashboardController < Admin::BaseController
    def send_newsletter
        sponsors = params[:sponsor_ids]
        brochures = params[:brochure_ids]

        sponsors = Sponsor.where(id: sponsors)
        brochures = Brochure.where(id: brochures).to_a

        # Send Newsletter email to the given Sponsors
        sponsors.each do |sponsor|
            BrochureMailer.newsletter(sponsor, brochures).deliver_later
        end

        redirect_back(fallback_location: admin_root_path, success: 'Newsletter sent!')
    end
end

And in the view, something like this:

<% @brochures.each do |brochure| %>
    <table width="280" border="0" cellpadding="0" cellspacing="0" align="left" valign="top" class="floater">
        <tr>
            <td align="center" valign="top">
                <a target="_blank" href="<%= brochure_url(brochure) %>">
                    <img border="0" vspace="0" hspace="0" src="<%= brochure.image.blank? ? 'default.png' : brochure.image.url(public: true) %>" width="250" height="142">
                    <b><%= brochure.title %></b>
                </a>
                <br>
                <%= brochure.description.truncate(60) %>
            </td>
        </tr>
    </table>
<% end %>

And it works like a charm! I'm not sure if this is the correct way or the most optimal way to go but just consider it as a second possibility.

I hope it could be useful for somebody else.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!