Rails Mailer: sending emails to a local file

2020-05-17 09:48发布

问题:

Is there any way to send emails in rails 3 to a local file or so, instead of using an SMTP server? I'd like to test the email's contents without using any kind of SMTP, ideally only in DEV environment.

The best would be a local SMTP or something that allowed me to inspect emails, check email addresses, etc.

回答1:

You can use the :test system delivery

ActionMailer::Base.delivery_method = :test

There are also a :file delivery_method

ActionMailer::Base.delivery_method = :file

You can define where put all email with file_settings

ActionMailer::Base.file_settings = { :location => Rails.root.join('tmp/mail') }


回答2:

There is also the letter_opener gem which will save mail to disk and open it in a browser.

Or mails_viewer which has a Web UI to view the emails instead of opening them in a browser.