Posted over 1 year ago. Visible to the public. Repeats.

ActionMailer: Preview mails directly in your email client

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

Copy
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:

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

Now, close the rails console and preview the mail:

Copy
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 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

Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Michael Leimstädtner
Last edit:
6 months ago
by Henning Koch
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 short-lived cookies to improve usability.
Accept or learn more