buildcode/laravel-database-emails

此包已被废弃,不再维护。作者建议使用stackkit/laravel-database-emails包。

使用数据库存储和发送电子邮件

7.0.0 2024-03-24 11:29 UTC

README

Run tests Latest Version on Packagist Total Downloads

介绍

此包允许您使用数据库存储和发送电子邮件。

要求

此包需要Laravel 10或11。

安装

使用Composer要求此包。

composer require stackkit/laravel-database-emails

发布配置文件。

php artisan vendor:publish --tag=database-emails-config
php artisan vendor:publish --tag=database-emails-migrations

创建此包所需的数据库表。

php artisan migrate

将电子邮件cronjob添加到您的调度器

protected function schedule(Schedule $schedule)
{
     $schedule->command('email:send')->everyMinute()->withoutOverlapping(5);
}

使用方法

发送电子邮件

电子邮件的创建方式与mailables相同。

use Stackkit\LaravelDatabaseEmails\Email;
use Illuminate\Mail\Mailables\Content;
use Stackkit\LaravelDatabaseEmails\Attachment;
use Illuminate\Mail\Mailables\Envelope;

Email::compose()
    ->content(fn (Content $content) => $content
        ->view('tests::dummy')
        ->with(['name' => 'John Doe'])
    )
    ->envelope(fn (Envelope $envelope) => $envelope
        ->subject('Hello')
        ->from('johndoe@example.com', 'John Doe')
        ->to('janedoe@example.com', 'Jane Doe')
    )
    ->attachments([
        Attachment::fromStorageDisk('s3', '/invoices/john-doe/march-2024.pdf'),
    ])
    ->send();
])

向应用程序中的用户发送电子邮件

Email::compose()
    ->user($user)
    ->send();

默认情况下,将使用name列设置收件人姓名。如果您希望使用不同的内容,应在模型中实现preferredEmailName方法。

class User extends Model
{
    public function preferredEmailName(): string
    {
        return $this->first_name;
    }
}

默认情况下,将使用email列设置收件人的电子邮件地址。如果您希望使用不同的内容,应在模型中实现preferredEmailAddress方法。

class User extends Model
{
    public function preferredEmailAddress(): string
    {
        return $this->work_email;
    }
}

默认情况下,将使用应用程序的区域设置。如果您希望使用不同的内容,应在模型中实现preferredEmailLocale方法。

class User extends Model implements HasLocalePreference
{
    public function preferredLocale(): string
    {
        return $this->locale;
    }
}

使用mailables

您还可以将mailable传递给电子邮件创建器。

Email::compose()
    ->mailable(new OrderShipped())
    ->send();

附件

要开始将文件附加到电子邮件,您可以使用与在Laravel中通常使用的方式相同的attachments方法。但是,您必须使用此包的Attachment类。

use Stackkit\LaravelDatabaseEmails\Attachment;

Email::compose()
    ->attachments([
        Attachment::fromPath(__DIR__.'/files/pdf-sample.pdf'),
        Attachment::fromPath(__DIR__.'/files/my-file.txt')->as('Test123 file'),
        Attachment::fromStorageDisk('my-custom-disk', 'test.txt'),
    ])
    ->send();

注意

Attachment::fromData()Attachment::fromStorage()不受支持,因为它们与原始数据一起工作。

将模型附加到电子邮件

您可以将模型附加到电子邮件。这可以用于附加用户或其他属于电子邮件的模型。

Email::compose()
    ->model(User::find(1));

调度

您可以通过调用later而不是send来调度电子邮件。您必须提供一个Carbon实例或一个有效的strtotime日期。

Email::compose()
    ->later('+2 hours');

队列电子邮件

重要

当使用queue函数队列邮件时,不再需要调度email:send命令。

Email::compose()->queue();

// On a specific connection
Email::compose()->queue(connection: 'sqs');

// On a specific queue
Email::compose()->queue(queue: 'email-queue');

// Delay (send mail in 10 minutes)
Email::compose()->queue(delay: now()->addMinutes(10));

如果您需要更多灵活性,也可以传递自己的作业类

Email::compose()->queue(jobClass: CustomSendEmailJob::class);

它可能看起来像这样

<?php

namespace App\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Stackkit\LaravelDatabaseEmails\SendEmailJob;

class CustomSendEmailJob extends SendEmailJob implements ShouldQueue
{
    // Define custom retries, backoff, etc...
}

测试模式

当启用时,所有新创建的电子邮件都将发送到指定的测试电子邮件地址。默认情况下是关闭的。

DB_EMAILS_TESTING_ENABLED=true
DB_EMAILS_TESTING_EMAIL=your-test-recipient@example.com

每分钟发送的电子邮件数

配置每次命令应发送多少封电子邮件。

DB_EMAILS_LIMIT=20

立即发送电子邮件

在Laravel调度器未运行时,非常有用

要启用,设置以下环境变量

DB_EMAILS_IMMEDIATELY=true

修剪模型

use Stackkit\LaravelDatabaseEmails\Email;

$schedule->command('model:prune', [
    '--model' => [Email::class],
])->daily();

默认情况下,当电子邮件超过6个月时,会将其删除。

您可以通过将以下内容添加到AppServiceProvider.php来更改此设置

use Stackkit\LaravelDatabaseEmails\Email;

public function register(): void
{
    Email::pruneWhen(function (Email $email) {
        return $email->where(...);
    });
}