如何将sendgrid电子邮件模板ID传递给邮件发送函数



我看到很多线程在那里解释使用sendgrid模板发送电子邮件。但我有电子邮件模板在第三方sendgrid服务器不在我的项目文件夹。只有模板ID与我。我正在使用这个包,我的邮件发送功能就像在自述文件中解释的那样。

Mail::send('view', $data, function (Message $message) {
$message
    ->to('foo@example.com', 'foo_name')
    ->from('bar@example.com', 'bar_name')
    ->embedData([
        'category' => 'user_group1',
        'unique_args' => [
            'user_id' => 123
        ]
    ], 'sendgrid/x-smtpapi');
});

当我通过一个视图在我的视图文件夹变量'view'它工作得很好。但是我的问题是如何在sendgrid中传递模板ID并从我的代码中使用该模板?

或任何其他方式与sendgrid模板与Laravel工作?请建议是否有比这个更好的库。

请帮。

在对如何做到这一点进行了大量研究之后,我为laravel 5.3创建了我自己的类(包括一个通知)。

这是类

<?php
    namespace AppNotifications;
    use IlluminateBusQueueable;
    use IlluminateNotificationsNotification;
    use IlluminateContractsQueueShouldQueue;
    use IlluminateNotificationsMessagesMailMessage;
    //This example use the official's sendgrid php helper (https://github.com/sendgrid/sendgrid-php)
    //You should install it before with composer require "sendgrid/sendgrid"
    class ExampleNotification extends Notification
    {
        use Queueable;
        public $api_key;
        public $sg;
        public $subs;
        public $template_id;
        public $to;
        public $from;
        public $subject;
        /**
         * Create a new notification instance.
         *
         * @return void
         */
        public function __construct( $data, $message )
        {
            $this->api_key = "Your Sendgrid Api Key";
            $this->sg = new SendGrid($this->api_key);
            $this->data = $data;
            $this->message = $message;
            $this->from = "example@gmail.com";
            //select message configuration method based on the message name
            $this->selectMessage();
        }
        /**
         * Get the notification's delivery channels.
         *
         * @param  mixed  $notifiable
         * @return array
         */
        public function via($notifiable)
        {
            //We should disable the automatic email provider from laravel
            // return ['mail'];
        }
        /**
         * Get the mail representation of the notification.
         *
         * @param  mixed  $notifiable
         * @return IlluminateNotificationsMessagesMailMessage
         */
        public function toMail($notifiable)
        {
            return (new MailMessage)
                        ->line('The introduction to the notification.')
                        ->action('Notification Action', 'https://laravel.com')
                        ->line('Thank you for using our application!');
        }
        /**
         * Get the array representation of the notification.
         *
         * @param  mixed  $notifiable
         * @return array
         */
        public function toArray($notifiable)
        {
            return [
                //
            ];
        }
        /**
         * Handler for different messages or templates that you use in sendgrid
         *
         * @return void
         */
        private function selectMessage(){
            if( $this->message == "test_message")
                $this->testMessage();
        }
        /**
         * Test message's configuration
         *
         * @return void
         */
        private function testMessage(){
            $this->template_id = "your sendgrid template id";
            $this->to = $this->data->email;
            $this->subs = [
                "-yoursubcode-" => "Hello"
            ];
            $this->subject = "Your Subject";
            $this->sendTransactionEmail();
        }
        /**
         * Setup personalizations for sendgrind v3 mail
         *
         * @param SendgridEmail Configured Email variable with the destination email
         *
         * @return SendgridPersonalization Configured Personalizations for the mail 
         */
        private function personalizeEmail( $to ){
            $personalization = new SendgridPersonalization();
            $personalization->addTo($to);
            foreach ($this->subs as $key => $value) {
                $personalization->addSubstitution($key,$value);    
            }
            return $personalization;
        }
        /**
         * Send the email via sendgrid 
         *
         * @return Object Http response from sendgrid
         */
        public function sendTransactionEmail(){
            $from = new SendGridEmail(null, $this->from);
            $to = new SendGridEmail(null, $this->to);
            //This should be at least 1 character even if it is empty
            $content = new SendGridContent("text/html", " ");
            $mail = new SendGridMail($from, $this->subject, $to, $content);
            $mail->setTemplateId($this->template_id);
            $personalization = $this->personalizeEmail($to);
            $mail->addPersonalization($personalization);
            //Sending the email via sengrid api v3
            $response = $this->sg->client->mail()->send()->post($mail);
        }
    }
    //Example usage of a notifiable model class
    //Remember to use use AppNotificationsExampleNotification 
    //in your controller or the class that you want to apply it
    $user->notify(new ExampleNotification( $user, "test_message" ));

希望对别人有所帮助

您只需遵循文档:https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html

可以在嵌入的数据中指定template_id:

Mail::send('emails.share', ['content' => 'foo'], function (Message $message) {
        $message
            ->subject('Test')
            ->to('me@jakubkratina.cz', 'foo_name')
            ->from('bar@example.com', 'bar_name')
            ->embedData([
                'custom_args' => [
                    'user_id' => "123" // Make sure this is a string value
                ],
                'template_id' => 'your-template-id'
            ], 'sendgrid/x-smtpapi');
    });

So easy:-)

编辑:视图的内容将被传递到模板编辑器中的<%body%>变量中。

我花了一些时间在这个问题上,让它工作。

确保使用"sendgrid/sendgrid": ">=5.0.1",

使用模板的示例代码可以在这里找到

最新更新