Visualize ActionMailer once, send many times?

I am trying to figure out if it is possible to use ActionMailer without Rails to render the html.erb view once, and then send it several times, just using different emails: to?

NOTE. I DO NOT use the full Rails stack, just ActionMailer

So in the mailer class

class MyMailer < ActionMailer::Base default :from => ' johndoe@example.com ', :subject => 'New Arrivals!' def new_products(customer, new_products) @new_products = new_products mail :to => customer.email do |format| format.html end end end 

Then in the client code we need to get new products and customers.

 products = Product.new_since_yesterday customers = Customer.all customers.each do |c| MyMailer.new_products(c, products).deliver end 

Let's say this is sent once a day, so we want to receive new products only from the moment we sent the letter. We want to do this only once, as new products today will not change between e-mail messages. As far as I know, this will trigger a render every time an email is created and sent.

Is there a way to tell ActionMailer to do this only once, and then somehow reference the object containing the rendered view. This will cut out all the time it takes for the render to do this. Sent email addresses will be changed, but email content will not.

Obviously, for a large number of emails, you simply do not scroll through the list and do not create / send emails. You can use a queue for this. In the general case, when you do not need to repeatedly render the rendering step, how do you do it once and use this result for all letters?

Potentially, my unfamiliarity with ActionMailer does not allow me here.

+4
source share
1 answer

I have not tried this, but calling the mailer simply returns a plain old Mail :: Message object, full body. Thus, you should be able to simply capture the body and reuse it.

 message = MyMailer.new_products(c, products) message_body = message.body customers.each do |c| mail = Mail.new do from ' yoursite@sample.com ' to c.email subject 'this is an email' body message_body end mail.deliver end 

You may even be able to get more "effective" by tricking the message

 message = MyMailer.new_products(c, products) customers.each do |c| mail = message.dupe() mail.to = c.email mail.deliver end 
+2
source

All Articles