简体   繁体   English

如何发送带有 Laravel 的模板 Mailgun email?

[英]How do you send a templated Mailgun email with Laravel?

I'm trying to figure out if there's a way to send a Mailgun template through a Laravel mailable.我试图弄清楚是否有办法通过 Laravel 可邮寄的方式发送 Mailgun 模板。

The following code sends my email fine using a blade view:以下代码使用刀片视图发送我的 email 罚款:

return $this->from(['address'=>'no-reply@domain.com', 'name'=>'Domain'])
        ->subject("subject")
        ->replyTo(['address'=>'sales@domain.com'])
        ->view('emails.deliverReport')
        ->withSwiftMessage(function($message){
            $headers = $message->getHeaders();
            $headers->addTextHeader("X-Mailgun-Variables", '{"type": "asset-delivery"}');
            $headers->addTextHeader("X-Mailgun-Tag", "asset-delivery");
        });

I'd like to send a template that I've created on Mailgun as opposed to using a blade template.我想发送一个我在 Mailgun 上创建的模板,而不是使用刀片模板。

You are able to do this with CURL by sending the template as form data:您可以通过将模板作为表单数据发送来使用 CURL 执行此操作:

curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
-F from='Sender Bob <sbob@YOUR_DOMAIN_NAME>' \
-F to='alice@example.com' \
-F subject='Hello' \
-F template='template.test' \
-F h:X-Mailgun-Variables='{"title": "API documentation", "body": "Sending messages with templates"}'

Has anyone had any luck sending a Mailgun template via a Laravel mailable?有没有人通过 Laravel 可邮寄的方式发送 Mailgun 模板?

Most probably this doesn't concern you anymore, but it answers the question.很可能这不再与您有关,但它回答了问题。 I've created a library that adds a new notification channel to send templated messages to Laravel;我创建了一个 库,该库添加了一个新的通知通道以将模板化消息发送到 Laravel; this breaks down to something like the following:这分解为以下内容:

  1. Create a new channel:创建一个新频道:

     class MailgunTemplatesChannel { public function __construct(private Mailgun $mailgun) public function send($notifiable, Notification $notification): void { [$template, $params] = $notification->toMailgun($notifiable); // Route the notification to the mail recipient address $params['to'] = $notifiable->routeNotificationFor('mail'); $this->mailgun->messages()->send([ 'template' => $template, ...$params, ]); } }
  2. Setup Mailgun and register the channel in a service provider:设置 Mailgun 并在服务提供商中注册频道:

     class AppServiceProvider extends ServiceProvider public function register(): void { //... // Configure Mailgun $this->app->bind(Mailgun::class, fn() => Mailgun::create( config('services.mailgun.secret'), config('services.mailgun.endpoint', 'https://api.mailgun.net'), )); // Register the channel Notification::resolved(fn(ChannelManager $service) => $service->extend( 'mailgun', // This is the notification channel identifier fn(Application $app) => $app->make(MailgunTemplatesChannel::class) )); }
  3. Configure notifications to send via the new Mailgun channel:配置通知以通过新的 Mailgun 通道发送:

     class MyTestNotification extends Notification { public function toMailgun(): array { return [ 'subject' => 'Your subject', 'template' => 'name_of_the_template', 'v:some_variable' => 'a template variable value', ]; } public function via(): array { return [ 'mailgun' ]; } }
  4. Send notifications via Mailgun:通过 Mailgun 发送通知:

     $user->notify(new MyTestNotification());

Please don't just copy-paste this into your app!请不要只是将其复制粘贴到您的应用程序中! It's a rough outline of how to build a custom channel for templated messages, but it leaves a lot to desire.这是如何为模板化消息构建自定义通道的粗略概述,但它还有很多不足之处。
If you want a production-ready package, take a look at matchory/laravel-mailgun-templates-channel .如果您想要生产就绪的 package,请查看matchory/laravel-mailgun-templates-channel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM