This post is also available in: English-US (英語)
RailsのAction Mailer を使って、Railsからメール送信しようとしていた時のエラーの対処法のメモです。エラーは以下のような内容です。
An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.
Mailerクラスの設定
まず、Mailerクラス(例としてSampleMailer)にdefault from(default toは必要に応じて)を設定して、実在するメールアドレスを設定する必要があります。
class SampleMailer < ActionMailer::Base # 送信元 default from: "sample@example.com" # 送信先 default to: "sample@example.com" end
SendgridのSMTPを使う場合(Heroku)
SendgridのSMTPを使う場合(Heroku)、development.rbやproduction.rbに以下のように追加します。
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
ローカルの開発環境では以下のコマンドで、実際のuser_nameとパスワードを置き換えます。
#ENV['SENDGRID_USERNAME'] heroku config:get SENDGRID_USERNAME #ENV['SENDGRID_PASSWORD'] heroku config:get SENDGRID_PASSWORD
GmailのSMTPを使う場合
GmailのSMTPを使う場合には「安全性の低いアプリの許可」を「有効」にする必要があります。
https://support.google.com/a/answer/6260879
そして、development.rbやproduction.rbに以下のように追加します。
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => '587',
:domain => 'smtp.gmail.com',
:authentication => 'plain',
:user_name => 'gmailのアドレス',
:password => 'gmailのパスワード'
}

