ActionMailer: Previewing mails directly in your email client

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

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

Michael Leimstädtner About 5 years ago