5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

laravelのメール本文のテスト

Posted at

Mailtrapを使った方法と
メールの受け取り先を共通にする方法の2つを紹介します

Mailtrapを使った方法

.envファイルの修正

Mialtrapの設定を見ながらsmtpサーバの設定をenvファイルに反映する
mailtrap.png

MAIL_DRIVER=smtp
MAIL_USERNAME=xxxxxxxxxxxxxx
MAIL_PASSWORD=xxxxxxxxxxxxx
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525

phpunit.xml

一時的にphpunitを使ったメール送信をMailtrapでキャッチしたい場合はMAIL_DRIVERの値が上書きされてしまうので注意
MAIL_DRIVERの値をsmtpにする
ただ戻しておかないとCircleCIとかでテストを回すたびにMailtrapにメールが飛んでMailtrapの制限にいっちゃうので注意

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>

        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <server name="APP_ENV" value="local"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="redis"/>
        <server name="MAIL_DRIVER" value="smtp"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
    </php>
</phpunit>

宛先のメールアドレスを変えて自分のメールボックスでメールを受けて確認する方法

config/mail.phpにtoを設定すると全てのメールが指定されたアドレスに送信されます

'to' => [
    'address' => 'example@example.com',
    'name' => 'Example'
],

5
1
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
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?