16.

【Laravelエラー】Failed to authenticate on SMTP server with username ...

編集
この記事の要点
  • Failed to authenticate on SMTP serverメール送信時の SMTP 認証失敗
  • 原因 ①: username / password 間違い
  • 原因 ②: Gmail / Yahoo はアプリパスワード必要(通常パスワード不可)
  • 原因 ③: ポート / 暗号化 (STARTTLS / SSL) 設定ミス
  • Laravel なら .envMAIL_* 設定確認

 

エラーの状況

Symfony\Component\Mailer\Exception\TransportException:
Failed to authenticate on SMTP server with username "myuser" using the following authenticators: "LOGIN", "PLAIN", "XOAUTH2".

# または Laravel 5 系
Swift_TransportException:
Failed to authenticate on SMTP server with username "myuser" using 3 possible authenticators

# Gmail 特有
535-5.7.8 Username and Password not accepted.

主要 SMTP プロバイダの設定

サービスホストポート暗号化認証
Gmailsmtp.gmail.com587STARTTLSアプリパスワード
Gmail (SSL)smtp.gmail.com465SSLアプリパスワード
Yahoosmtp.mail.yahoo.com587/465STARTTLS/SSLアプリパスワード
Outlook / Office 365smtp.office365.com587STARTTLS通常 PW or アプリパスワード
SendGridsmtp.sendgrid.net587STARTTLSAPI key
Mailgunsmtp.mailgun.org587STARTTLSAPI key
Amazon SESemail-smtp.region.amazonaws.com587STARTTLSSMTP 認証情報
Postmarksmtp.postmarkapp.com587STARTTLSAPI token

Gmail でアプリパスワード取得

  1. Google アカウント (myaccount.google.com)
  2. セキュリティ2 段階認証プロセス を有効化(必須)
  3. 同じセキュリティ画面 → アプリパスワード
  4. 「メール」+ デバイス名(例: "My Laravel App")
  5. 16 文字のパスワードが生成される → コピー
  6. このパスワードを SMTP 設定で使用

Laravel での設定

# .env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myuser@gmail.com
MAIL_PASSWORD=xxxx-xxxx-xxxx-xxxx   # アプリパスワード
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=myuser@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

# キャッシュクリア (重要)
$ php artisan config:clear
$ php artisan cache:clear

# テスト送信
$ php artisan tinker
>>> Mail::raw("テスト", function ($m) { $m->to("test@example.com")->subject("テスト"); });

各プロバイダの SMTP 設定

SendGrid (推奨、無料枠 100 通/日)

MAIL_MAILER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey     # ← 固定値 (実際の文字列 "apikey")
MAIL_PASSWORD=SG.xxxxx...  # API key
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=noreply@yourdomain.com

# API キー取得: SendGrid → Settings → API Keys → Create

Mailgun

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster@mg.yourdomain.com
MAIL_PASSWORD=xxx  # SMTP credentials
MAIL_ENCRYPTION=tls

Amazon SES

# SMTP 認証情報生成: AWS Console → SES → SMTP Settings
MAIL_MAILER=smtp
MAIL_HOST=email-smtp.us-east-1.amazonaws.com
MAIL_PORT=587
MAIL_USERNAME=AKIAxxx...   # SMTP username (not IAM access key)
MAIL_PASSWORD=xxx          # SMTP password (not IAM secret)
MAIL_ENCRYPTION=tls

Spring Boot での設定

# application.properties
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=myuser@gmail.com
spring.mail.password=xxxx-xxxx-xxxx-xxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000

SMTP 接続テスト

# telnet で接続テスト
$ telnet smtp.gmail.com 587
Trying 142.250.4.108...
Connected to smtp.gmail.com.
EHLO test
250-smtp.gmail.com at your service
250-PIPELINING
250-STARTTLS
QUIT

# openssl で SSL 接続
$ openssl s_client -starttls smtp -connect smtp.gmail.com:587 -crlf

# Python でテスト
python3 -c "
import smtplib, ssl
context = ssl.create_default_context()
with smtplib.SMTP('smtp.gmail.com', 587) as s:
    s.starttls(context=context)
    s.login('user@gmail.com', 'app-password')
    print('OK')
"

原因別の対処

① ユーザ名 / パスワード間違い

  • .env の値をコピペし直す
  • パスワードに特殊文字 ($ 等) が含まれる場合は引用符でくくる
  • config:clear で反映

② 2 段階認証で通常パスワード不可

  • Gmail / Yahoo: アプリパスワードを生成して使用
  • Outlook: 通常パスワードでも可能なケースもあるが、アプリパスワード推奨

③ ポート / 暗号化ミスマッチ

  • 587 → STARTTLS(推奨)
  • 465 → SSL
  • 25 → 暗号化なし(通常はブロック)

④ Less Secure Apps を許可(古い設定、非推奨)

Gmail はかつて「安全性の低いアプリ」を許可することで通常パスワード使えたが、2022 年に廃止。アプリパスワード一択

⑤ ファイアウォール / プロバイダ制限

# 一部 ISP がポート 25 をブロック (スパム対策)
# 対策: 587 を使う

# 企業ネットワークで 587 もブロックされている場合
# - プロキシ経由
# - VPN
# - 別のサービス (REST API 経由のメール送信)

本番運用ベストプラクティス

  • 専用メール配信サービス: SendGrid / Mailgun / SES / Postmark
  • SPF / DKIM / DMARC 設定: 迷惑メール扱い回避
  • 送信元アドレス: 認証済みドメインのみ
  • レート制限: 1 時間あたりの送信数を制限
  • キュー化: Laravel Queue / Spring @Async でバッチ送信
  • バウンス処理: 配信失敗を検知して対応

関連記事

編集
Post Share
子ページ

子ページはありません

同階層のページ
  1. SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost'
  2. Add [~] to fillable property to allow mass assignment on [App\~].
  3. PHP Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in ~
  4. Changing columns for table "~" requires Doctrine DBAL; install "doctrine/dbal"
  5. MethodNotAllowedHttpException No message
  6. Class 'Doctrine\DBAL\Driver\PDOMySql\Driver' not found
  7. production.ERROR: No application encryption key has been specified.
  8. Dotenv values containing spaces must be surrounded by quotes.
  9. Laravel \ Socialite \ Two \ InvalidStateException
  10. The page has expired due to inactivity. Please refresh and try again.
  11. Failed to clone https://github.com/symfony/thanks.git via https, ssh protocol
  12. Illegal offset type
  13. Cannot access protected property Illuminate\Http\Request::$...
  14. Emitted value instead of an instance of Error
  15. 画像保存時にInternal Server Error
  16. Failed to authenticate on SMTP server with username ...
  17. PostTooLargeException
  18. Database hosts array is empty.
  19. Invalid request (Unsupported SSL request)
  20. does not comply with psr-4 autoloading standard. Skipping.
  21. MySQLのSTR_TO_DATE関数を使用するとnullが返却される問題