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 online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
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)