php 如何用'数组'邮件驱动程序发送邮件?

ryevplcw  于 2023-03-16  发布在  PHP
关注(0)|答案(4)|浏览(143)

从版本5.7开始,Laravel建议在测试期间使用array驱动程序:

不幸的是,the documentation对这个驱动程序一无所知,根据the source code,驱动程序将所有消息存储在内存中,而不是实际发送它们,如何在单元测试期间获得存储的“已发送”消息(以便检查它们)?

j9per5c4

j9per5c41#

编辑:使用Laravel 9+,用途:

$emails = app()->make('mailer')->getSymfonyTransport()->messages();
dd($emails);

确保在.envphpunit.xml文件中将邮件驱动程序设置为array
使用Laravel 7+或如果您收到错误Target class [swift.transport] does not exist,请使用此命令获取使用阵列驱动程序发送的电子邮件列表:

$emails = app()->make('mailer')->getSwiftMailer()->getTransport()->messages();

$count = $emails->count();
$subject = $emails->first()->getSubject();
$to = $emails->first()->getTo();
$body = $emails->first()->getBody();
ddrv8njm

ddrv8njm2#

调用app()->make('swift.transport')->driver()->messages()。返回值是Swift_Mime_SimpleMessage对象的集合。
完整PHPUnit测试的示例:

public function testEmail()
{
    Mail::to('user@example.com')->send(new MyMail);

    $emails = app()->make('swift.transport')->driver()->messages();
    $this->assertCount(1, $emails);
    $this->assertEquals(['user@example.com'], array_keys($emails[0]->getTo()));
}
mxg2im7a

mxg2im7a3#

我根据菲尼斯的回答自定义的Assert。

protected function assertMailSentTo($user, $expected = 1)
{
    $messages = app('swift.transport')->messages();

    $filtered = $messages->filter(function ($message) use ($user) {
        return array_key_exists($user->email, $message->getTo());
    });

    $actual = $filtered->count();

    $this->assertTrue(
        $expected === $actual,
        "Sent {$actual} messages instead of {$expected}."
    );
}
pw136qt2

pw136qt24#

下面是Laravel 9、10的完整邮件测试示例。(使用数组驱动程序)

public function it_can_track_sent_emails_and_content()
    {
        $email1 = 'john@example.com';
        $subject1 = 'Welcome to our App';
        $message1 = 'Thank you for joining our application!';
        $message2 = 'Contact us for support.';
        
        // send the message to array driver
        \Illuminate\Support\Facades\Mail::raw($message1 . ' ' . $message2, function ($message) use ($email1, $subject1) {
            $message->to($email1)
                ->subject($subject1);
        });

        // get the emails
        $emails = $this->app->make('mailer')->getSymfonyTransport()->messages();

        // verify mail count
        $this->assertCount(1, $emails);

        // get first email
        /** @var \Illuminate\Mail\SentMessage $email */
        $email = $emails->first();

        $to = $email->getEnvelope()->getRecipients();

        $recipients = [];
        foreach ($to as $address) {
            $recipients[$address->getAddress()] = $address->getName();
        }

        // verify recipient by email
        $this->assertArrayHasKey($email1, $recipients);

        // verify the subject line
        $this->assertEquals($subject1, $email->getOriginalMessage()->getSubject());

        // verify mail content
        $this->assertStringContainsString($message1, $email->getOriginalMessage()->getBody()->toString());
        $this->assertStringContainsString($message2, $email->getOriginalMessage()->getBody()->toString());
    }

相关问题