production環境でのメール認証でエラーが出現する際に試したこと

背景

herokuで作成中のwebアプリでユーザー登録を実行する際に、 sign upボタンを押すと、We're sorry, but something went wrong.が表示されるページにリダイレクトされてしまう。

heroku logs --tailを実行してログの収集を行う。

原因

ログを追った結果、以下の文言を発見。

Net::SMTPFatalError (550 Unauthenticated senders not allowed)

SendGridでのエラーに起因しているようだ。 この文章をググると、以下の公式サイトが出てきた。 https://sendgrid.com/docs/for-developers/sending-email/smtp-errors-and-troubleshooting/#550-unauthenticated-senders-not-allowed

解決するには、以下を実行すれば良さそう。

you’ll want to make sure that you’ve configured your setup to connect to smtp.sendgrid.net using authentication, and that the credentials you’re using are the same credentials you use to login to the SendGrid.

解決方法

config/environments/production.rb SMPTの:user_nameを'SENDGRID_USERNAME'に、:passwordを'SENDGRID_PASSWORD'にそれぞれ変更。 そうすると、設定した通りにユーザー登録の為の認証メールが届いた。

注意事項として、本番運用するアプリケーションでは、暗号化されていないIDやパスワードのような重要なセキュリティ情報はソースコードに直接書き込まない。環境変数に記述し、そこからアプリケーションに読み込む。今回の場合は、そうした変数はSendGridアドオンが自動的に設定してくれる。

技術的に学べたこと

  • herokuでのデバック方法

    • heroku logs
    • heroku logs --tail
    • heroku run rails c
  • 環境変数

    • まだよく理解できていない。