Read more

ActionMailer: Previewing mails directly in your email client

Michael Leimstädtner
April 18, 2019Software engineer at makandra GmbH

In Rails, we usually have a mailer setup like this:

class MyMailer < ActionMailer::Base

  def newsletter
    mail to: 'receiver@host.tld',
      from: 'sender@host.tld',
      subject: 'My mail'
  end

end
Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

If you want to preview your mail in the browser, you can use the Action Mailer Preview. To inspect the mail directly in your email client, just create an .eml file and open it with your client:

mail = MyMailer.newsletter
File.open('my_mail.eml', 'w') { |file| file.write(mail.to_s) }

Now, close the rails console and preview the mail:

xdg-open my_mail.eml

There is not much magic in this example: mail is an instance of Mail::Message, which exposes a public API Show archive.org snapshot , including #to_s.

Opening an e-mail from the Rails log

In development Rails will print the mail's text representation to your log/development.log. You may also save this output as-is to an .eml file and open it with your mail client.

See also

Posted by Michael Leimstädtner to makandra dev (2019-04-18 14:34)