ejunker / mailthief
为 Laravel 应用程序提供虚假的 Mailer,简化邮件测试的痛苦。
v0.3.18
2023-04-05 22:13 UTC
Requires
- illuminate/mail: >=5.0
- illuminate/view: >=5.0
Requires (Dev)
- laravel/framework: >=5.0
- mockery/mockery: ^0.9.5
- phpunit/phpunit: ^5.5
This package is auto-updated.
Last update: 2024-09-06 01:25:37 UTC
README
MailThief
MailThief 是一个针对 Laravel 应用程序(5.0+)的虚假邮件发送者,可以在不实际发送任何电子邮件的情况下轻松测试邮件。
注意
从 tightenco/mailthief 分支而来,并支持 Laravel 的新版本。
快速开始
安装
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('noreply@example.com'); $m->bcc('notifications@example.com'); $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' => 'john@example.com', 'password' => 'secret', ]); // Check that an email was sent to this email address $this->seeMessageFor('john@example.com'); // BCC addresses are included too $this->seeMessageFor('notifications@example.com'); // 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('noreply@example.com'); // 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 Mailer
和 Message
类一样的所有功能。更详细的文档即将推出,但在那之前,请探索 MailThief 和 Message 类以了解可用性。
如果你正在使用 Laravel 5.3 中的新 Mailables 语法,你可以使用 原生邮件断言。但如果你在任何版本的 Laravel 中使用经典邮件语法,MailThief 仍然是你的最佳选择。