[Heroku] SendGrid Email Sending Configuration Method for Rails 5.x
I configured email sending via SendGrid (Heroku addon) in Ruby on Rails 5.x, so I’ll introduce that method.
This article covers the process up to testing email sending with Rails ActionMailer.
First, I decided to adopt SendGrid as the email delivery service because it had particularly good reviews.
SendGrid pricing plans can be checked here:
Up to 12,000 emails per month can be used with the free plan. (As of October 20, 2017)
When using Rails ActionMailer, you don’t need to create a SendGrid API key.
First, add the SendGrid Heroku add-on.
heroku addons:create sendgrid:starter --app yourapp
The SMTP configuration for sending emails via SendGrid in Ruby on Rails looks like this:
config/environments/production.rb
# Setup the mailer config
# Use SendGrid - Add-ons - Heroku
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host: 'yourapp.herokuapp.com' }
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
user_name: ENV['SENDGRID_USERNAME'],
password: ENV['SENDGRID_PASSWORD'],
domain: 'herokuapp.com',
address: 'smtp.sendgrid.net',
port: 587,
authentication: :plain,
enable_starttls_auto: true
}
There are many config.action_mailer configuration options, so configure them as needed.
First, generate an ActionMailer named TestMailer using the rails generate command.
$ rails generate mailer TestMailer
create app/mailers/test_mailer.rb
invoke haml
create app/views/test_mailer
identical app/views/layouts/mailer.text.haml
conflict app/views/layouts/mailer.html.haml
Overwrite /Users/codenote/yourapp/app/views/layouts/mailer.html.haml? (enter "h" for help) [Ynaqdh] n
skip app/views/layouts/mailer.html.haml
invoke rspec
create spec/mailers/test_mailer_spec.rb
create spec/mailers/previews/test_mailer_preview.rb
Create a template file for the text email body and fill in the content appropriately.
app/views/test_mailer/notify.text.haml
テストメール本文です。
The rails console command can also be used on Heroku.
I tested email sending by executing the TestMailer.notify.deliver method from the REPL in rails console as follows:
$ heroku run rails console --app yourapp
Running rails console on ⬢ yourapp... up, run.8677 (Free)
Loading production environment (Rails 5.1.4)
irb(main):001:0> TestMailer.notify.deliver
I, [2017-10-20T13:41:57.721166 #4] INFO -- : Rendering test_mailer/notify.text.haml within layouts/mailer
I, [2017-10-20T13:41:57.725015 #4] INFO -- : Rendered test_mailer/notify.text.haml within layouts/mailer (3.6ms)
D, [2017-10-20T13:41:57.888214 #4] DEBUG -- : TestMailer#notify: processed outbound mail in 169.2ms
I, [2017-10-20T13:41:58.186642 #4] INFO -- : Sent mail to [email protected] (298.1ms)
D, [2017-10-20T13:41:58.186805 #4] DEBUG -- : Date: Fri, 20 Oct 2017 13:41:57 +0000
From: [email protected]
To: [email protected]
Message-ID: <59e9fd25d9aa4_4a82f3c5336d@494abe47-1f52-48d9-abe0-679ebf9b174b.mail>
Subject: =?UTF-8?Q?=E3=83=86=E3=82=B9=E3=83=88=E3=83=A1=E3=83=BC=E3=83=AB=E4=BB=B6=E5=90=8D?=
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: base64
44OG44K544OI44Oh44O844Or5pys5paH44Gn44GZ44CCCgo=
=> #, , , >, , , , >
Below is a screenshot showing successful email reception.
You can directly open the SendGrid Dashboard page with the heroku command.
heroku addons:open sendgrid --app yourapp
That’s all from the Gemba, where I want to deliver emails with Rails + SendGrid on Heroku.
That’s all from the Gemba.