Read more

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

Daniel Straßner
April 24, 2018Software engineer at makandra GmbH

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
Illustration book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
Read more Show archive.org snapshot

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

Posted by Daniel Straßner to makandra dev (2018-04-24 09:25)