当应用程序已经注册或订阅用户时,发送邮件可能是必不可少的功能之一。 在开发过程中,我们倾向于使用 SMTP 测试服务器,例如?mailtrap.io 。
Mailtrap 为单个收件箱提供了一个免费计划以进行测试,我们可以将邮件发送到该收件箱,但收件箱中存储的邮件数量有限。当我们使用此免费计划时,我们还限制了在一秒钟内可以发送多少封电子邮件,因此我们不能同时发送多封电子邮件,所以必须延迟或睡眠每个邮件过程。
?
上述问题的最优解是?mailhog 。 Mailhog 是在服务器 / 计算机本地运行的 SMTP 测试服务器,Laradock 拥有此服务。 让我们尝试一下。
运行 Mailhog 服务器和 web UI
我假设你已经知道并尝试过使用 Laradock,如果没有,那么你可以试试使用 Laradock?此处。
要运行 Mailhog 服务器和 web UI,只需运行这个?docker compose ?命令:
docker-compose up -d mailhog
这下容器应该就会处于工作状态,并且当你使用?docker-compose ps ?命令进行检查时,它的状态为?up :
Name Command State Ports
---------------------------------------------------------------------------------------------------------------------
laradock_mailhog_1 MailHog Mailhog Up 0.0.0.0:1025->1025/tcp,0.0.0.0:8025->8025/tcp
为 Laravel App 设置 Mailhog
在你的 laravel app 的.env , 添加 / 更改这些参数:
MAIL_DRIVER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=from@example.com
MAIL_FROM_NAME=Example
在 Laravel 发送邮件的例子
我们可以创建一个简单的 artisan 命令发送邮件,以下是你需要添加到你的 laravel 项目:
appConsoleCommandsExampleSendMailCommand.php :
<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
use AppMailExampleMail;
use IlluminateSupportFacadesMail;
class ExampleSendMailCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'example:send-mail';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command for exemplify the mail sending in laravel';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
Mail::to('example@mailinator.net')->send(new ExampleMail());
}
}
appMailExampleMail.php :
<?php
namespace AppMail;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;
class ExampleMail extends Mailable implements ShouldQueue
{
use Queueable,SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('mails.example');
}
}
resourcesviewsmailsexample.blade.php :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Example Mail Test</title>
</head>
<body>
<h1>Hello from the other side!</h1>
</body>
</html>
添加 / 注册命令到?appConsoleKernel.php :
<?php
namespace AppConsole;
use IlluminateConsoleSchedulingSchedule;
use IlluminateFoundationConsoleKernel as ConsoleKernel;
use AppConsoleCommandsExampleSendMailCommand;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
ExampleSendMailCommand::class,];
/**
* Define the application's command schedule.
*
* @param IlluminateConsoleSchedulingSchedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
最后,现在进入 laradock workspace bash (如果你还没有) 使用你最喜欢的 CLI 来执行这个命令:
?
docker-compose exec --user=laradock workspace bash
?
进入你的 laravel app root 目录,执行 artisan 命令:
php artisan example:send-mail
如果在执行命令时没有错误,那么让我们看看我们的收件箱!
?
访问 Mailhog Web UI
mailhog web UI 应该可以通过?http://localhost:8025?访问。你的示例邮件应该在那里

使电子邮件消息持久
Mailhog 将?caught ?消息存储在内存中,这意味着当你停止容器并再次运行时,你所有的消息都将消失 (永远)。因此,如果你想要保留它们,那么你必须通过配置 laradock/docker- composition .yml 来保持它们的持久性。在文件中找到 mailhog 配置,并将其更改为如下所示:
...
## Mailhog ################################################
mailhog:
build: ./mailhog
volumes:
- ${DATA_PATH_HOST}/mailhog/maildir:/maildir
command: ["-storage=maildir","-maildir-path=/maildir"]
ports:
- "1025:1025"
- "8025:8025"
networks:
- frontend
- backend
...
然后重新启动或停止运行容器。从这一点开始,你的所有消息都将被保存。
在 Laradock 探索 Mailhog 的乐趣。
laravel version used: 6.0 LTS
?
更多学习内容请访问:
腾讯T3-T4标准精品PHP架构师教程目录大全,只要你看完保证薪资上升一个台阶(持续更新)
? (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|