laravel5.2.15_使用Laravel 5.3 Mailables轻松便捷地发送电子邮件

laravel5.2.15

Laravel 5.3 has just been released and there are a ton of great new features. One of the major improvements is in how you send mail in your applications.

Laravel 5.3刚刚发布,并且有很多很棒的新功能 。 主要改进之一是在应用程序中发送邮件的方式。

Let's take a look at sending emails. Before Laravel 5.3, sending emails in Laravel looked a lot like this.

让我们看一下发送电子邮件。 在Laravel 5.3之前,在Laravel中发送电子邮件看起来很像这样。

Mail::send('emails.send', ['title' => $title, 'message' => $message], function ($message)
 {
    $message->from('no-reply@scotch.io', 'Scotch.IO');
    $message->to('batman@batcave.io');
});

This method worked for a while, but after sending a couple of emails, the code base got messy. Since I didn't really like this method, I found myself using event listeners to build emails.

这种方法工作了一段时间,但是在发送了几封电子邮件之后,代码库变得混乱了。 由于我不太喜欢这种方法,因此我发现自己使用事件侦听器来构建电子邮件。

安装Laravel 5.3 ( Installing Laravel 5.3 )

At the time of this article, installing Laravel 5.3 with Laravel installer is as simple as.

在撰写本文时,使用Laravel安装程序安装Laravel 5.3非常简单。

laravel new project

邮件介绍 ( Introducing Mailables )

Mailables in Laravel abstracts building emails with a mailable class. Basically, mailables are responsible for collating data and passing them to views. Meanwhile, the API for sending emails got really simple.

Laravel中的Mailables抽象了使用mailable类构建电子邮件。 基本上,可邮寄邮件负责整理数据并将其传递给视图。 同时,用于发送电子邮件的API非常简单。

To send emails in Laravel, all we have to do now is.

要在Laravel中发送电子邮件,我们现在要做的就是。

Mail::to('batman@batcave.io')->send(new KryptoniteFound);

Don't get me wrong, the previous API will work just fine (and it will still work in your applications) — it's just that the Mail API got a whole lot simpler.

别误会,以前的API可以很好地工作(并且仍然可以在您的应用程序中工作)–只是Mail API变得简单得多。

创建一个可邮寄的 ( Creating a Mailable )

With artisan our super handy Laravel cli-tool, we can simply create a mailable like this.

使用artisan我们的超级方便的Laravel cli工具,我们可以简单地创建一个这样的邮件。

php artisan make:mail<NameOfMailable>

Since our mailable's name is KryptoniteFound, we can create our mailable using this command.

由于我们的邮件名称为KryptoniteFound ,因此可以使用此命令创建邮件。

php artisan make:mail KryptoniteFound

Creating a laravel 5.3 mailable

After we've created our mailable, in app/mail, we can see our newly created mailable class.

app / mail中创建了可发送邮件之后 ,我们可以看到我们新创建的可发送邮件类。

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class KryptoniteFound extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct()
    {
        //
    }

    public function build()
    {
        return $this->view('view.name');
    }
}

The created class should look like the snippet above (comments stripped).

创建的类应类似于上面的代码片段(注释已删除)。

As we can see, the build method builds the message. For our case, we can replace the view.name with the path to our email view email.kryptonite-found.

如我们所见, build方法将构建消息。 对于我们的情况,我们可以将view.name替换为电子邮件视图email.kryptonite-found的路径。

In resources/views create a new blade template in an email folder called kryptonite-found.blade.php.

资源/视图中,在名为kryptonite-found.blade.php电子邮件文件夹中创建一个新的刀片服务器模板。

将数据传递到电子邮件视图 ( Passing Data to Email Views )

Any public property on your mailable class is automatically made available to your view file. So passing data to your views is as simple as making the data public on the mailable class.

可邮递类的任何公共属性将自动提供给您的视图文件。 因此,将数据传递到视图就像在可邮寄类上公开数据一样简单。

Say for example I wanted to pass the weight of kryptonites found, all I need to do is expose the total on the mailable like this.

比如说我想传递所发现的k的重量,我要做的就是像这样在邮件中公开总量。

public $total = 30;

While in our view template, we access the data like a normal variable.

在视图模板中时,我们像普通变量一样访问数据。

<h1>Woot Woot!!</h1>

<p>Alfred just found <strong>{{ $total }}lbs</strong> of kryptonite</p>

We can also explicitly set data using the with method.

我们还可以使用with方法显式设置数据。

public function build()
{
    return $this->view('emails.kryptonite-found')
                ->with($key, $value);
}

配置我们的邮件程序 ( Configuring our Mailer )

To send emails, we will use mailtrap. For other service configuration like Amazon SES, Chris wrote an article on the topic.

要发送电子邮件,我们将使用mailtrap 。 对于其他服务配置(如Amazon SES), Chris撰写了有关该主题的文章

Next, we move to our .env file and configure our mail credentials.

接下来,我们移至.env文件并配置我们的邮件凭据。

MAIL_DRIVER="smtp"
MAIL_HOST="mailtrap.io"
MAIL_PORT=2525
MAIL_USERNAME=31230cade12007610
MAIL_PASSWORD=24280ed5ee934c
MAIL_ENCRYPTION=null

You replace MAIL_USERNAME and MAIL_PASSWORD with your mailtrap details. Using the credentials above won't work.

您用MAIL_USERNAME陷阱详细信息替换MAIL_USERNAMEMAIL_PASSWORD 。 使用上面的凭据将不起作用。

Still, on the issue of configuring mail, we also need to configure the mailers from details. In config/mail.php, look for an array from key and configure the address and name. If you are satisfied with the defaults, you can just leave it. But, under no condition can it be null.

不过,在配置邮件的问题上,我们还需要详细信息配置邮件程序。 在config / mail.php中 key中查找一个数组并配置地址名称 。 如果您对默认设置感到满意,则可以保留它。 但是,在任何情况下都不能为null。

发送额外的参数 ( Sending Extra Parameters )

Adding bcc, cc and the rest can be called on $this in the build method.

添加bcccc和其余的可以在build方法的$this上调用。

public function build()
{
    $address = 'ignore@batcave.io';
    $name = 'Ignore Me';
    $subject = 'Krytonite Found';

    return $this->view('emails.kryptonite-found')
                ->from($address, $name)
                ->cc($address, $name)
                ->bcc($address, $name)
                ->replyTo($address, $name)
                ->subject($subject);
}

发送邮件 ( Sending Emails )

Previously, our routes were located in app/Http/routes.php, but with this release routes are now in app/routes. Now we can use routes based on the interface (web, api or console). For this tutorial, we only need the web.php routes file.

以前,我们的路由位于app / Http / routes.php中 ,但在此版本中,路由现在位于app / routes中 。 现在,我们可以使用基于接口(Web,API或控制台)的路由。 对于本教程,我们只需要web.php路由文件。

use App\Mail\KryptoniteFound;

Route::get('/', function () {
    // send an email to "batman@batcave.io"
    Mail::to('batman@batcave.io')->send(new KryptoniteFound);

    return view('welcome');
});

Now, we can start our Laravel server by running the following artisan command.

现在,我们可以通过运行以下artisan命令来启动Laravel服务器。

php artisan serve

We can trigger an email by visiting http://localhost:8080.

我们可以通过访问http://localhost:8080触发电子邮件。

排队电子邮件 ( Queuing Emails )

To queue emails, instead of calling send on the Mail::to, just call the queue method and pass it the mailable.

要使电子邮件排队,而不是在Mail::to上调用send,只需调用queue方法并将其传递给mailable即可。

Mail::to('batman@batcave.io')->queue(new KryptoniteFound);

延迟消息队列 (Delay Message Queueing)

To delay a queued email, we use the later method. This method takes in two parameters. The first is a DateTime object that represents the time to wait. The second parameter is mailable.

要延迟排队的电子邮件,我们使用later方法。 此方法采用两个参数。 第一个是DateTime对象,它代表等待时间。 第二个参数是可邮寄的。

$when = Carbon\Carbon::now()->addMinutes(10);

Mail::to('batman@batcave.io')->later($when, new KryptoniteFound);

结论 ( Conclusion )

Laravel 5.3 offers a lot of promising features like notifications, oAuth, search etc. Watch out for them.

Laravel 5.3提供了许多有希望的功能,例如通知,oAuth,搜索等。请当心。

翻译自: https://scotch.io/tutorials/easy-and-fast-emails-with-laravel-5-3-mailables

laravel5.2.15

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值