[扩展推荐] Laravel Mailbox —— 使用 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 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 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。