当应用程序已经注册或订阅用户时,发送邮件可能是必不可少的功能之一。 在开发过程中,我们倾向于使用 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 项目:

app\console\commands\examplesendmailcommand.php:

<?php

namespace app\console\commands;

use illuminate\console\command;

use app\mail\examplemail;
use illuminate\support\facades\mail;

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());
    }
}

  

app\mail\examplemail.php:

<?php

namespace app\mail;

use illuminate\bus\queueable;
use illuminate\contracts\queue\shouldqueue;
use illuminate\mail\mailable;
use illuminate\queue\serializesmodels;

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');
    }
}

  

resources\views\mails\example.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>

  

添加 / 注册命令到 app\console\kernel.php:

<?php

namespace app\console;

use illuminate\console\scheduling\schedule;
use illuminate\foundation\console\kernel as consolekernel;

use app\console\commands\examplesendmailcommand;

class kernel extends consolekernel
{
    /**
     * the artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        examplesendmailcommand::class,
    ];

    /**
     * define the application's command schedule.
     *
     * @param  \illuminate\console\scheduling\schedule  $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架构师教程目录大全,只要你看完保证薪资上升一个台阶(持续更新)