Posted 9 months ago. Visible to the public. Repeats.

ActionMailer: Preview 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'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, including #to_s.

Opening an e-mail from the Rails log

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

See also

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Michael Leimstädtner
Last edit:
9 months ago
by Michael Leimstädtner
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Michael Leimstädtner to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more