ruby-on-railsemailruby-on-rails-3.2massmail

How to cache a mailer view when sending massive email


I need to send massive email,I will use for brackground job Delayed Job, and have to create the email message in 3 languages (de, en, re), How can I cache the view so it doesn't have to create each time I'm calling the the mail method.


Solution

  • The deliver method is the one that sends the email, so you can do this:

    def send_emails
      # You can set here the email with attachments and all stuff
      mail = MyMailer.send_message("demo@example.com")
      body = mail.html_part.body
    
      User.all.each do |u|
        mail.to = u.email
        mail.html_part.body = body.gsub(/user_id/, u.id)
        mail.deliver
      end
    end
    

    Of course it's better if you set this method for background processing.