使用 Lumen 5.4 和 Mailgun 发送电子邮件的最简单方法

Easiest way to send emails with Lumen 5.4 and Mailgun

我已将 Lumen 5.4.6 配置为使用 Mailgun 发送电子邮件。我看到了其他几个答案,但它们不完整或有不必要的陈述,或者它们比需要的更复杂。

我在下面给出了完整详细的步骤,让 Lumen 像我发现的那样简单干净地与 Mailgun 一起工作。

如果您知道更简单、更简洁、更优雅的方法,请添加您的答案和评论。

UPDATE: I'm developing an open source quick-start boilerplate Lumen project with some useful functionality out of the box like emailing as well as user register and login: https://github.com/AMS777/ams-bl

It can be used as a starting point for a JSON API Lumen project or as a reference for the steps described below. There you can find also some notes to test and preview emails on browsers.

配置流明

在命令行添加 Laravel 的 Composer 包:

composer require illuminate/mail
composer require guzzlehttp/guzzle

我使用的是 Lumen 5.4.6,所以我不得不安装一个旧的 mail 包:

composer require illuminate/mail:5.4.*

在文件 bootstrap/app.php 添加:

bootstrap/app.php: (在 Register Service Providers 部分。)

$app->register(\Illuminate\Mail\MailServiceProvider::class);

bootstrap/app.php: (文件末尾,return上方)

$app->configure('services');
$app->configure('mail');

如果您想使用简单的界面 Facades 发送电子邮件,您必须取消注释:

bootstrap/app.php: (取消注释。)

$app->withFacades();

在您的项目中创建以下文件:

  • config/mail.php
  • config/services.php

复制并整理Laravel的文件内容:

https://github.com/laravel/laravel/blob/master/config/mail.php

在这里,我将默认邮件驱动程序更改为 mailgunusernamepassword 不与 mailgun 驱动程序一起使用,它们可能会被删除。

config/mail.php:

<?php
return [
    /*
    |--------------------------------------------------------------------------
    | Mail Driver
    |--------------------------------------------------------------------------
    |
    | Laravel supports both SMTP and PHP's "mail" function as drivers for the
    | sending of e-mail. You may specify which one you're using throughout
    | your application here. By default, Laravel is setup for SMTP mail.
    |
    | Supported: "smtp", "sendmail", "mailgun", "mandrill", "ses",
    |            "sparkpost", "log", "array"
    |
    */
    'driver' => env('MAIL_DRIVER', 'mailgun'),
    /*
    |--------------------------------------------------------------------------
    | SMTP Host Address
    |--------------------------------------------------------------------------
    |
    | Here you may provide the host address of the SMTP server used by your
    | applications. A default option is provided that is compatible with
    | the Mailgun mail service which will provide reliable deliveries.
    |
    */
    'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
    /*
    |--------------------------------------------------------------------------
    | SMTP Host Port
    |--------------------------------------------------------------------------
    |
    | This is the SMTP port used by your application to deliver e-mails to
    | users of the application. Like the host we have set this value to
    | stay compatible with the Mailgun e-mail application by default.
    |
    */
    'port' => env('MAIL_PORT', 587),
    /*
    |--------------------------------------------------------------------------
    | Global "From" Address
    |--------------------------------------------------------------------------
    |
    | You may wish for all e-mails sent by your application to be sent from
    | the same address. Here, you may specify a name and address that is
    | used globally for all e-mails that are sent by your application.
    |
    */
    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
        'name' => env('MAIL_FROM_NAME', 'Example'),
    ],
    /*
    |--------------------------------------------------------------------------
    | E-Mail Encryption Protocol
    |--------------------------------------------------------------------------
    |
    | Here you may specify the encryption protocol that should be used when
    | the application send e-mail messages. A sensible default using the
    | transport layer security protocol should provide great security.
    |
    */
    'encryption' => env('MAIL_ENCRYPTION', 'tls'),
    /*
    |--------------------------------------------------------------------------
    | SMTP Server Username
    |--------------------------------------------------------------------------
    |
    | If your SMTP server requires a username for authentication, you should
    | set it here. This will get used to authenticate with your server on
    | connection. You may also set the "password" value below this one.
    |
    */
    'username' => env('MAIL_USERNAME'),
    'password' => env('MAIL_PASSWORD'),
    /*
    |--------------------------------------------------------------------------
    | Sendmail System Path
    |--------------------------------------------------------------------------
    |
    | When using the "sendmail" driver to send e-mails, we will need to know
    | the path to where Sendmail lives on this server. A default path has
    | been provided here, which will work well on most of your systems.
    |
    */
    'sendmail' => '/usr/sbin/sendmail -bs',
    /*
    |--------------------------------------------------------------------------
    | Markdown Mail Settings
    |--------------------------------------------------------------------------
    |
    | If you are using Markdown based email rendering, you may configure your
    | theme and component paths here, allowing you to customize the design
    | of the emails. Or, you may simply stick with the Laravel defaults!
    |
    */
    'markdown' => [
        'theme' => 'default',
        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],
];

https://github.com/laravel/laravel/blob/master/config/services.php

这里我去掉了不必要的行。

config/services.php:

<?php
return [
    /*
    |--------------------------------------------------------------------------
    | Third Party Services
    |--------------------------------------------------------------------------
    |
    | This file is for storing the credentials for third party services such
    | as Stripe, Mailgun, SparkPost and others. This file provides a sane
    | default location for this type of information, allowing packages
    | to have a conventional place to find your various credentials.
    |
    */
    'mailgun' => [
        'domain' => env('MAILGUN_DOMAIN'),
        'secret' => env('MAILGUN_SECRET'),
    ],
];

然后您需要在 .env 文件中设置环境变量:

.env:

MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your@email.es
MAIL_FROM_NAME="Your email sender name"
MAILGUN_DOMAIN=sandbox_EXAMPLE.mailgun.org
MAILGUN_SECRET=key-EXAMPLE

好了,到目前为止,Lumen 上的电子邮件功能已经准备就绪。

在 Lumen 中创建和发送电子邮件

设置好邮件功能后,在 Lumen 中创建和发送电子邮件的步骤与 Laravel 中的相同,可以在其文档中查看:

https://laravel.com/docs/5.4/mail

要在 Lumen 中发送电子邮件,您需要 3 个文件:

  • 邮件class。扩展 Mailable。从视图构建电子邮件。
  • 电子邮件模板。一个看法。可能是 html 或 Blade 引擎。
  • Class 使用邮件 class 并发送电子邮件。

此邮件示例 class 仅导入构建电子邮件所需的包。如果要使用队列,则需要导入更多包。

app/mail/RegisterConfirmationEmail.php:

<?php

namespace App\Mail;

use Illuminate\Mail\Mailable;

class RegisterConfirmation extends Mailable
{
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('RegisterConfirmationEmail');
    }
}

视图在 5.2 版本中从 Lumen 中删除,但很快就恢复了。从 Lumen 5.4 开始,视图可以工作,但文档中没有任何线索。

如果您不想使用视图,可以使用 raw() 功能。

resources/views/RegisterConfirmationEmail.blade.php:

<html>
    <body>
        <h1>Test</h1>
    </body>
</html>

发送邮件的class必须导入邮件class。请记住,此示例使用必须在 bootstrap/app.php.

上启用的接口 Facades

app/Http/Controllers/RegisterController.php:

<?php

namespace App\Http\Controllers;

use App\Mail\RegisterConfirmation;
use Illuminate\Support\Facades\Mail;

class RegisterController extends Controller
{
    public function sendRegisterConfirmationEmail()
    {
        Mail::to('test+receiver@email.es')->send(new RegisterConfirmation());
    }
}

邮枪

在 Mailgun 上,您必须使用您和您公司的一些数据创建一个帐户。

Mailgun 将要求您提供手机 phone 号码以继续您的帐户验证:

"To prevent abuse, Mailgun requires you to verify your account through the use of SMS."

对于开发帐户,不需要信用卡或域验证,但您需要授权最多 5 个电子邮件地址。

您可以使用 Mailgun 提供的沙箱域。