Sending Confirmation Emails
Get familiar with the configuration of order confirmation emails.
Sending email in Rails requires three basic steps:
- Configuring how the email is to be sent
- Determining when to send the email
- Specifying what we want to say in the email.
We’ll cover each of these three in turn.
Configuring email
Email configuration is part of a Rails application’s environment and involves a Rails.application.configure
block. If we want to use the same configuration for development, testing, and production, add the configuration to environment.rb
in the config
directory; Otherwise, add different configurations to the appropriate files in the config/environments
directory.
Inside the block, we need to have one or more statements. We first have to decide how we want our mail to be delivered:
config.action_mailer.delivery_method = :smtp
Alternatives to :smtp
include :sendmail
and :test
.
The :smtp
and :sendmail
options are used when we want Action Mailer to attempt to deliver email. We’ll clearly want to use one of these methods in production.
The :test
setting is great for unit and functional testing. Email won’t be delivered, but will instead be appended to an array, which is accessible via the ActionMailer::Base.deliveries
attribute. This is the default delivery method in the test environment. Interestingly, though, the default in development mode is :smtp
. If we want Rails to deliver email during the development of our application, this is good. If we’d rather disable email delivery in development mode, edit the development.rb
file in the ...
Get hands-on with 1400+ tech skills courses.