LoginSignup
2
0

More than 1 year has passed since last update.

Laravel email テスト設定メモ 私の備忘録 phpunit.xmlのMAIL_DRIVERの設定をsmtpにする

Posted at

Laravelはテスト環境ではメール送信をしない

ユニットテストでメール送信しても届かない。なんでかな、と思って調べてみたら、Laravelはテスト環境ではメール送信をしない様だ。それもそうだ、テストで本物のアドレスにバンバン送ってたら苦情が来そうだ。
ということで、公式のドキュメントでは以下のメールテストの方法が書かれていた。(現在Laravel6の作業をしているのでLaravel6のドキュメント参照しました。)
- ログに書き出す
- 特定のメールアドレスに転送する
- mailtrapを使う

.env_testingのドライバーをmailtrapにすれば動くかなぁと、思っていたのだが、ぜんぜん届かない。

結論

結局 phpunit.xml にMAIL_DRIVERの記述があって

<server name="MAIL_DRIVER" value="array"/>

<server name="MAIL_DRIVER" value="smtp"/>

に、したらunittestでもmailtrapにメールが届くようになった。
Laravelの設定ばかり探して、無駄に苦労してしまった。
とほほ。

以下ドキュメントを読んだときのメモ

When developing an application that sends email, you probably don't want to actually send emails to live email addresses. Laravel provides several ways to "disable" the actual sending of emails during local development.

DeepLで翻訳
メールを送信するアプリケーションを開発する際、実際に生きているメールアドレスにメールを送信することは、おそらくないでしょう。Laravelでは、ローカル開発中に実際にメールを送信しないようにする方法がいくつか用意されています。

Log Driverを使う方法

Instead of sending your emails, the log mail driver will write all email messages to your log files for inspection. For more information on configuring your application per environment, check out the configuration documentation.

DeepLで翻訳
ログメールドライバは、メールを送信する代わりに、すべてのメールメッセージをログファイルに書き出し、検査します。環境ごとのアプリケーションの設定についての詳細は、設定ドキュメントを参照してください。

Universal To

Another solution provided by Laravel is to set a universal recipient of all emails sent by the framework. This way, all the emails generated by your application will be sent to a specific address, instead of the address actually specified when sending the message. This can be done via the to option in your config/mail.php configuration file:

DeepLで翻訳
Laravelが提供するもう一つの解決策は、フレームワークによって送信されるすべての電子メールのユニバーサル受信者を設定することです。この方法では、アプリケーションによって生成されたすべての電子メールは、メッセージを送信するときに実際に指定されたアドレスの代わりに、特定のアドレスに送信されます。これは、config/mail.php設定ファイルのtoオプションで行うことができます。

Mailtrap

Finally, you may use a service like Mailtrap and the smtp driver to send your email messages to a "dummy" mailbox where you may view them in a true email client. This approach has the benefit of allowing you to actually inspect the final emails in Mailtrap's message viewer.

DeepLで翻訳
最後に、Mailtrapとsmtpドライバのようなサービスを使って、メールメッセージを「ダミー」のメールボックスに送信し、本当のメールクライアントで表示することができます。この方法は、Mailtrapのメッセージビューワーで最終的なメールを実際に検査することができるという利点があります。

2
0
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
2
0