addgod/laravel-mandrill-template

在 Laravel 中使用 Mandrill 模板。

v2.0.1 2024-05-21 16:23 UTC

This package is auto-updated.

Last update: 2024-09-21 17:09:30 UTC


README

通过 Mandrill 模板系统发送邮件的方式。

安装

需求

  • PHP 7.2
  • Laravel 6

您可以通过 composer 安装此包

composer require addgod/laravel-mandrill-template

要发布配置文件,请运行

php artisan vendor:publish --provider="Addgod\MandrillTemplate\MandrillTemplateServiceProvider"

用法

将您的 Mandrill 密钥添加到 .env 文件中。

MANDRILL_SECRET="YOUR SECRET KEY"

发送邮件

use Addgod\MandrillTemplate\Mandrill\Message;
use Addgod\MandrillTemplate\Mandrill\Template;
use Addgod\MandrillTemplate\Mandrill\Recipient;
use Addgod\MandrillTemplate\Mandrill\Attachment;
use Addgod\MandrillTemplate\Mandrill\Recipient\Type;
use Addgod\MandrillTemplate\MandrillTemplateFacade;
$template = new Template('template-name');
$message = new Message();
$message
    ->setSubject('Subjct')
    ->setFromEmail('from@example.com')
    ->setFromName('Example Name')
    ->setMergeVars(['greeting' => 'Hello to you']);

$message->addRecipient(new Recipient('to@example.com', 'Example Name', Type::TO));
$message->addAttachment(Attachment::createFromFile($file, 'name_of_file');

MandrillTemplateFacade::send($template, $message);

通过通知

创建一个新的通知

php artisan make:notification WelcomeNotification

via 设置为 MandrillTemplateChannel

use Addgod\MandrillTemplate\MandrillTemplateChannel;
/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return [MandrillTemplateChannel::class];
}

实现 toMandrillTemplate 方法并准备您的模板

use Addgod\MandrillTemplate\MandrillTemplateMessage;
public function toMandrillTemplate($notifiable)
{
    return (new MandrillTemplateMessage)
        ->template('notification')
        ->from('from@example.com', 'Example dot com')
        ->to('to@example.com', 'Frank Kornell') // This is an extra to, since the notifiable is also used.
        ->cc('cc@example.com', 'Charlott Kornell')
        ->bcc('bcc@example.com', 'Mr admin Dude')
        ->greeting('Hello there'),
        ->line('The introduction to the notification.')
        ->action('Notification Action', url('/'))
        ->line('Thank you for using our application!')
        ->salutation('Regards from us.')
        ->attach($file, 'name_of_file);
}