[扩展推荐] Laravel Mailbox —— 使用 Laravel 来接收邮件

Laravel

Laravel Mailbox 是由 Marcel Pociot 开发的扩展包,用于处理 Laravel 应用程序中传入的电子邮件。Mailbox 具有一个连贯的API,允许自定义邮箱来捕获传入的电子邮件

来自文档documentation的一个入站电子邮件处理程序的快速示例:

Mailbox::from('{username}@gmail.com', function (InboundEmail $email, $username) {
    // Access email attributes and content
    $subject = $email->subject();

    $email->reply(new ReplyMailable);
});

Laravel Mailbox 的工作方式是侦听来自受支持的驱动程序(包括「mailgun」 、「SendGrid」 和 「log」 )传入的电子邮件,然后通过自定义邮箱类响应他们。下面是一个如何在服务提供者中定义邮箱的示例:

use BeyondCode\Mailbox\Facades\Mailbox;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Mailbox::from('sender@domain.com', function (InboundEmail $email) {
            // Handle the incoming email
        });
    }
}

上面的示例使用了一个闭包,当然了,第二个参数也可以是一个可调用的类:

Mailbox::from('sender@domain.com', MyMailbox::class);

Laravel

Laravel Mailbox is a package by Marcel Pociot for handling incoming emails in your Laravel application. Mailbox features a fluent API that allows you to define custom mailboxes to catch incoming emails.

Here’s a quick example of an inbound email handler from the documentation:

Mailbox::from('{username}@gmail.com', function (InboundEmail $email, $username) {
    // Access email attributes and content
    $subject = $email->subject();

    $email->reply(new ReplyMailable);
});

Laravel Mailbox works by listening for incoming emails from the supported drivers (options include “mailgun,” “sendgrid,” and “log”) and then responding to them through custom mailbox classes. At a basic level, here’s an example of how you can define a mailbox in a service provider:

use BeyondCode\Mailbox\Facades\Mailbox;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Mailbox::from('sender@domain.com', function (InboundEmail $email) {
            // Handle the incoming email
        });
    }
}

The above example uses a closure, but the second argument can also be an invocable class:

Mailbox::from('sender@domain.com', MyMailbox::class);

默认情况下,此包将所有入站电子邮件存储在数据库中,并具有可配置的保留时间。我们可以将 Laravel 的调度程序和包的 mailbox:clean 命令一起使用,该命令会从数据库中删除比可配置的持续时间更早的电子邮件。想要了解更多细节,请查看存储电子邮件

此包开箱即用,邮箱支持 Mailgun,Sendgrid 和本地开发驱动程序/日志。你也可以轻松扩展/添加自定义驱动程序-Laravel Mailbox 使用 Laravel 用户所熟悉的相同管理器模式(即数据库驱动程序)。

想了解 Laravel Mailbox 的更多信息 ,可以在 GitHub 查看beyondcode/laravel-mailbox. 至于如何安装和使用该软件包,请查看 Laravel Mailbox documentation.

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://laravel-news.com/laravel-mailbox

译文地址:https://learnku.com/laravel/t/41666

本文为协同翻译文章,如您发现瑕疵请点击「改进」按钮提交优化建议
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!