【问题标题】:Easiest way to send emails with Lumen 5.4 and Mailgun使用 Lumen 5.4 和 Mailgun 发送电子邮件的最简单方法
【发布时间】:2017-11-05 16:46:25
【问题描述】:

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

我在下面列出了完整而详细的步骤,以使 Lumen 与 Mailgun 一起工作,就像我发现的那样简单干净。

如果您知道更简单、更清洁、更优雅的方式,请添加您的答案和 cmets。

【问题讨论】:

标签: email lumen mailgun


【解决方案1】:

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

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

配置流明

在命令行中添加 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 个文件:

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

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

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>

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

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 将要求您提供手机号码以继续您的帐户验证:

“为防止滥用,Mailgun 要求您通过以下方式验证您的帐户 短信的使用。”

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

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

【讨论】:

  • 感谢您的信息。但是没有使用刀片...当我设置属性或使用方法时,{{$variable}} 按原样呈现...您能告诉(或添加到您的答案)如何配置模板吗?
  • @Fiftoine 我已经用一个开源项目的链接更新了答案,该项目实现了将电子邮件添加到 Lumen 的描述步骤。检查app/Mail/ContactMessageEmail.php 是否解决了您在构造函数上获取数据的问题。在接下来的几天里,我将推送其他方式,例如 `return $this->markdown('mail.RegisterSuccessEmail', ['loginUrl' => env('APP_LOGIN_URL')]);`。您还可以查看 Laravel 文档中的 View Data 部分:laravel.com/docs/5.6/mail#view-data.
猜你喜欢
  • 2017-07-02
  • 1970-01-01
  • 1970-01-01
  • 2014-04-14
  • 2017-10-12
  • 1970-01-01
  • 2021-01-01
  • 2015-09-15
  • 2018-09-05
相关资源
最近更新 更多