开发者

Send multiple mails in one deliver command?

I try to send multiple mails in one single .deliver command. My mailer method looks like this:

def feedback_mail
    @verteiler = ["foo@bar.com", "bar@foo.com"]
    for v in @verteiler.sort
      mail(:to => v, :subject => "Foo Bar")
    end
end

The problem is, only开发者_JAVA技巧 one mail gets send, to the last entry of the array. any advice here?


This idea won't scale much - you'd need to implement something like Resque and workers to process sending all the emails out.

Yes, @socjopata's idea is good, but you'll really want to iterate adding them to the job queue and spawn a couple workers to process them. As per the guides, this is how to do it:

class AdminMailer < ActionMailer::Base
  default :to => ["foo@bar.com", "bar@foo.com"],
          :from => "notification@example.com"

  def new_registration(user)
    @user = user
    mail(:subject => "New User Signup: #{@user.email}")
  end
end

I'd essentially hand of YourMailer.feedback_mail(<PASS ARGS HERE>).deliver to Resque-

Resque.enqueue(SendMail, <ARGS>)  # replace this in your controller

The worker would look like

#send_mail.rb
class SendMail
  @queue = :feedback_mail_queue
  def self.perform(<ARGS>) # pass these in enqueue call
    YourMailer.feedback_mail(<PASS ARGS HERE>).deliver
  end
end

As per section 2.3.4 of the Rails Guides:

It is possible to send email to one or more recipients in one email (for e.g. informing all admins of a new signup) by setting the list of emails to the :to key. The list of emails can be an array of email addresses or a single string with the addresses separated by commas.

Looking back at your attempt, FYI - this should work

def feedback_mail
    @verteiler = ["foo@bar.com", "bar@foo.com"]
    mail(:to => @verteiler, :subject => "Foo Bar")
end


This has taken me the better part of 1-2 weeks to figure out, so hopefully this code will give you some idea about what to do.

Here's my Lists controller where I generate the message to be sent to the mailer:

def messages
  @list = List.find(params[:id]) 
  #@message =  Message.new( :user_id => @list.user.id, :list_id => @list.id, 
  #:subject => params[:subject], :body => params[:body] )
  @message = Message.new(params[:message])
  @contacts = @list.contacts

   respond_to do |format|
     if @list.save && @message.save  

       #Blaster is my mailer, and blast is the send method in the blaster.rb mailer

       Blaster.blast(@message, @contacts)
       format.html { 
         redirect_to @list, notice: 'Message was saved.' }
         format.json { render json: @list, status: :created, location: @list }
     else
       format.html { redirect_to @list, notice: 'There was an error, try again'}
       format.json { render json: @list.errors, status: :unprocessable_entity }
     end
   end

end

=========

Here's my mailer - it's called blast.rb

def blast(message, contacts)
    # can't send without a message, and an array of contacts 
    @message = message
    @contacts = contacts

    # with variables set, let's create the loop to do its magic 
    @contacts.each do |contact|
        mail = mail(
          :to => "#{contact.email}",
          :from => "noreply@foo.org",
          :return_path => "noreply@foo.org",  
          :subject => @message.subject,
          :body => @message.body
          #:template_path => 'blaster',
          #:template_name => 'blast'
          ) do |format|
            format.html { render 'blast.html.erb'}
            format.text { render 'blast.text.erb'}
            end   
        mail.deliver
    end # contacts.each loop
 end #blast method 

I'm pretty sure this code is sub-optimal and of poor quality, so caveat emptor, but perhaps you'll find something in here that'll help you solve your unique implementation. Hope it helps!

D.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜