tightenco/mailthief

此包已被弃用,不再维护。未建议替代包。

为 Laravel 应用程序提供假邮件器,简化邮件测试。

v0.3.14 2018-02-10 04:17 UTC

README

Github Actions Status MailThief Logo

MailThief

MailThief 是为 Laravel 应用程序(5.0+)提供的假邮件器,可以在不发送任何电子邮件的情况下轻松测试邮件。

注意

由于 Laravel 处理邮件测试方式的变化;MailThief 对框架的最新版本不再需要。MailThief 将与 Laravel 5.5 及以下版本保持兼容。

快速入门

安装

composer require tightenco/mailthief --dev

示例路由

Route::post('register', function () {
    // <snip> Validation, create account, etc. </snip>

    Mail::send('emails.welcome', [], function ($m) {
        $email = request('email');
        $m->to($email);
        $m->subject('Welcome to my app!');
        $m->from('[email protected]');
        $m->bcc('[email protected]');
        $m->getHeaders()->addTextHeader('X-MailThief-Variables', 'mailthief');
    });

    // <snip> Return response </snip>
});

如果你在复制此示例测试,请记住在 resources/views/emails/welcome.blade.php 创建电子邮件视图。

示例测试

use MailThief\Testing\InteractsWithMail;

class RegistrationTest extends TestCase
{
    // Provides convenient testing traits and initializes MailThief
    use InteractsWithMail;

    public function test_new_users_are_sent_a_welcome_email()
    {
        $this->post('register', [
            'name' => 'John Doe',
            'email' => '[email protected]',
            'password' => 'secret',
        ]);

        // Check that an email was sent to this email address
        $this->seeMessageFor('[email protected]');

        // BCC addresses are included too
        $this->seeMessageFor('[email protected]');

        // Make sure the email has the correct subject
        $this->seeMessageWithSubject('Welcome to my app!');

        // Make sure the email was sent from the correct address
        $this->seeMessageFrom('[email protected]');

        // Make sure a given header is set on an email
        $this->seeHeaders('X-MailThief-Variables');

        // Make sure the header is set to a given value
        $this->seeHeaders('X-MailThief-Variables', 'mailthief');

        // Make sure the email contains text in the body of the message
        // Default is to search the html rendered view
        $this->assertTrue($this->lastMessage()->contains('Some text in the message'));
        // To search in the raw text
        $this->assertTrue($this->lastMessage()->contains('Some text in the message', 'raw'));
    }
}

MailThief 支持几乎与常规 Laravel MailerMessage 类所能做的一切。更详细的文档即将推出,但在此期间,可以通过探索 MailThiefMessage 类来了解可用功能。

如果你在 Laravel 5.3 中使用新的 Mailables 语法,可以使用 原生的邮件断言。但如果你在任何版本的 Laravel 中使用经典邮件语法,MailThief 仍然是你最佳的选择。