Use Docker to develop PHP, Laradock series 3: Mailhog

When the application has registered or subscribed users, sending emails may be one of the essential functions. During development, we tend to use SMTP test servers, for example  mailtrap.io.

Mailtrap provides a free plan for a single inbox to test, we can send mail to the inbox, but the number of messages stored in the inbox is limited. When we use this free plan, we also limit how many emails we can send in one second, so we cannot send multiple emails at the same time, so we must delay or sleep each mail process.

 

The optimal solution to the above problem is  mailhog. Mailhog is an SMTP test server running locally on the server / computer, and Laradock has this service. Let's try it.

Run Mailhog server and web UI

I assume you already know and tried using Laradock, if not, then you can try using Laradock  here .

To run the Mailhog server and web UI, just run this  docker compose command:

docker-compose up -d mailhog

The container should be in working state now, and when you docker-compose ps check it with the  command, its state is  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

Set up Mailhog for Laravel App

In your laravel app .env, add / change these parameters:

MAIL_DRIVER=smtp
MAIL_HOST=mailhog
MAIL_PORT = 1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
[email protected]
MAIL_FROM_NAME=Example

  

Example of sending mail in Laravel

We can create a simple artisan command to send mail, here is what you need to add to your laravel project:

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('[email protected]')->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>

  

Add / register commands to  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');
    }
}

  

Finally, now enter the laradock workspace bash (if you haven't already) to execute this command using your favorite CLI:

 

docker-compose exec --user=laradock workspace bash

  

 

Go to your laravel app root directory and execute the artisan command:

php artisan example:send-mail

  

If there are no errors when executing the command, then let's take a look at our inbox!

 

Access Mailhog Web UI

The mailhog web UI should be accessible via  http: // localhost: 8025  . Your example mail should be there

Make email messages persistent

Mailhog stores  caught messages in memory, which means that when you stop the container and run again, all your messages will disappear (forever). Therefore, if you want to keep them, then you must configure laradock / docker-composition.yml to keep them persistent. Find the mailhog configuration in the file and change it to look like this:

...

## 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

...

  

Then restart or stop the container. From this point on, all your messages will be saved.

Explore the fun of Mailhog in Laradock.

laravel version used: 6.0 LTS

  

 

For more learning content, please visit:

Tencent T3-T4 standard boutique PHP architect tutorial directory Daquan, as long as you read it to ensure that the salary rises a step (continuous update)

 

Guess you like

Origin www.cnblogs.com/a609251438/p/12691320.html