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

时间:2017-11-05 16:46:25

标签: email lumen mailgun

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

我在下面提供了完整而详细的步骤,以便让Lumen与Mailgun一起使用,就像我发现的那样简单明了。

如果您了解更简单,更清洁,更优雅的方式,请添加您的答案和评论。

1 个答案:

答案 0 :(得分:14)

  

更新:我正在开发一个开源的快速启动样板流明项目,其中包含一些开箱即用的有用功能   通过电子邮件发送以及用户注册和登录:   https://github.com/AMS777/ams-bl

     

它可以用作JSON API流明项目的起点,也可以作为所述步骤的参考   下面。在那里,您还可以找到一些注释,以便在浏览器上测试和预览电子邮件。

配置流明

在命令行上添加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驱动程序一起使用,可能会将其删除。

<强>配置/ 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

我在这里删除了不必要的行。

<强>配置/ 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个文件:

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

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

应用/邮件/ 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()函数。

<强>资源/视图/ RegisterConfirmationEmail.blade.php:

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

发送电子邮件的类必须导入邮件类。请记住,此示例使用Facade接口,必须在bootstrap/app.php上启用。

应用/ HTTP /控制器/ 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上,您必须使用您和您公司的一些数据创建一个帐户。

Mailgun会要求您提供手机号码以继续进行帐户验证:

  

“为了防止滥用行为,Mailgun要求您通过验证帐户   使用短信。“

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

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