概要
SendGrid で Net::SMTPAuthenticationError: 535 Authentication failed: Bad username / password というエラーが発生したときの解決方法をご紹介します。
エラーメッセージ
Net::SMTPAuthenticationError: 535 Authentication failed: Bad username / password
解決方法 SendGrid API の場合 user_name は apikey
APIキーで送信するときにハマったのですが、APIキーを使って送信する場合にはユーザー名にapikeyを指定しないとSMTPAuthenticationErrorのような認証エラーがでます。
引用元: SendGridを使ってSMTPでメールを送信する方法 | Simple is Beautiful.
smtp_settings が下記のような場合 SMTP_USER は SendGrid API key を発行したページにある情報ではなく apikey という文字列を指定する必要がありました。
ActionMailer::Base.smtp_settings = {
user_name: ENV['SMTP_USER'],
password: ENV['SMTP_PASS'],
domain: ENV['SMTP_DOMAIN'],
address: 'smtp.sendgrid.net',
port: 587,
authentication: :plain,
enable_starttls_auto: true
}
以上、SendGrid で Net::SMTPAuthenticationError: 535 Authentication failed: Bad username / password エラーが発生して困り果てた、現場からお送りしました。