In Rails applications, ActionMailer is a powerful module used to send emails. It’s essential to configure your ActionMailer settings correctly to ensure that your application can send emails reliably through your chosen SMTP server. Typically, these settings are configured in your environment files (e.g., config/environments/production.rb). However, there may be instances where you need to override these settings temporarily through the Rails console—for example, when testing a custom configuration or troubleshooting email delivery issues.

To override the ActionMailer settings, start by accessing your Rails console. Open your terminal and run the following command:

1
rails console

Once in the console, you can override the settings by directly setting the ActionMailer::Base.smtp_settings. Here’s an example configuration:

1
2
3
4
5
6
7
8
9
ActionMailer::Base.smtp_settings = {
  address:              'smtp.office365.com',
  port:                 587,
  user_name:            '[email protected]'
  password:             'passwords',
  authentication:       :login,
  enable_starttls_auto: true,
  read_timeout: 60
}

You can override all the options available here through this.

Once you’ve set the smtp_settings, you can test if everything is configured correctly by sending a test email directly from the console. Here’s how you can do it:

1
DeveloperMailer.any_mailer('[email protected]').deliver_now

This example assumes you have a mailer (DeveloperMailer) and an email method (any_mailer) set up in your application. If the email sends successfully, it indicates that the settings are correct.

It’s important to remember that changes made in the Rails console are temporary and will be lost once you exit the console or restart your server. If you need to persist these settings, you should update your environment files.