How to cache a mailer view when sending massive email

461 Views Asked by At

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.

1

There are 1 best solutions below

1
On BEST ANSWER

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("[email protected]")
  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.