LoginSignup
6
4

More than 1 year has passed since last update.

Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted. Learn more at)の倒し方

Last updated at Posted at 2022-04-14

Action_Mailerを使い、gmail経由でメール送信したかったが、Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted. Learn more at)が連発したのでメモリます。

結論

user_name と password は環境変数におくべし。

実際のコード

ガイドにある通りconfig/environments/development.rb に以下を追加

  # MailCatcher Configuration
  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.perform_caching = false

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = { address: 'smtp.gmail.com',
                                         port: 587,
                                         domain: 'gmail.com',
                                         user_name: ENV['GMAIL_ADDRESS'],
                                         password: ENV['GMAIL_PASSWORD'],
                                         authentication: 'plain',
                                         enable_starttls_auto: true, }

  config.action_mailer.asset_host = 'http://localhost:3100'
  config.action_mailer.default_url_options = { host: 'localhost:3100' }

▼パスワードの取得方法は以下の記事が参考になります。
【Rails】メール送信設定 〜gmail利用〜

この時、user_nameとpasswordを直書きしていたのですが、認証が通らない。
そこで、環境変数として記載したところ、メールの送信ができました。
(僕は.envファイルに記載し、ENVコマンドで呼び出す方法を取りました。)

.env
GMAIL_PASSWORD=gmailで設定した固有のパスワード
GMAIL_ADDRESS=gmailのメールアドレス

参考

gmail用のaction-mailer設定

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4