Rails now allows EML files to be downloaded from the email preview

To test emails in Rails, ActionMailer::Preview provides an easy way to preview emails in the browser. This feature is very useful for developers to test emails in the browser. However, to get a real sense of how an email will look in the inbox, it is necessary to download the email as an .eml file and open it in an email client. This was only possible by sending the email to a real email address (or using a service like MailCatcher, MailDev etc.) and downloading it from the inbox. This is not a very convenient way to test emails.

Before

Let’s create a simple mailer and previewer.

app/mailers/custom_mailer.rb

class CustomMailer < ApplicationMailer
  def invite
    mail(to: '[email protected]', subject: 'You have been invited!')
  end
end

app/views/custom_mailer/invite.html.erb

# Preview all emails at http://localhost:3000/rails/mailers/custom_mailer
<p>Hello,</p>

<p>You've been invited! Please confirm your account by clicking the link below:</p>

<p><a href="https://example.com/confirm?token=sdfjk23rlkjsdfo08">https://example.com/confirm?token=sdfjk23rlkjsdfo08</a></p>

test/mailers/previews/custom_mailer_preview.rb

# Preview all emails at http://localhost:3000/rails/mailers/custom_mailer
class CustomMailerPreview < ActionMailer::Preview
  def invite
    CustomMailer.invite
  end
end

Now let’s start the Rails server and navigate to the email previewer at http://localhost:3000/rails/mailers/custom_mailer/invite. We can see the email preview in the browser. However, there is no way to download the email as an .eml file from the preview.

After

Thanks to this PR, we can now download the email as an .eml file directly from the browser. This is enabled by default.

Here’s how it looks.

Need help on your Ruby on Rails or React project?

Join Our Newsletter