ActionMailer: How to send a test mail directly from the console

Updated . Posted . Visible to the public.

If your rails application is unable to send mails, it might be useful to debug your settings using the rails console. Here is a snippet that shows the current settings and lets you send a test mail directly from the console:

mailer = ActionMailer::Base.new

# check settings:
mailer.delivery_method # -> :smtp
mailer.smtp_settings # -> { address: "localhost", port: 25, domain: "localhost.localdomain", user_name: nil, password: nil, authentication: nil, enable_starttls_auto: true }

# send mail:
mailer.mail(from: 'sender@example.com', to: 'recipient@example.com', subject: 'test', body: "Hello, you've got mail!").deliver

You could use this to test the spam score of your emails, e.g. by using an existing mailer:

ActionMailer::Base.default_url_options[:protocol] = 'https'
ActionMailer::Base.default_url_options[:host] = '<your production domain>'

user = User.first
user.email = '<your mail address of mail-tester.com>' # do not save!

UserMailer.welcome(user).deliver_now

Profile picture of Daniel Straßner
Daniel Straßner
Last edit
Daniel Straßner
License
Source code in this card is licensed under the MIT License.
Posted by Daniel Straßner to makandra dev (2018-04-24 07:25)