Knowledge Base

Send transactional email

Notification channel registration

    public function toTemplate($notifiable)
    {
        return [
            'template' => 'verify_account',
            'merge' => [
                'firstname' => $notifiable->firstName,
                'lastname' => $notifiable->lastName,
                'verifylink' => route('verify', ['user' => $notifiable->hash, 'code' => $notifiable->confirmation_code]),
            ],
        ];
    }

 

Transactional email channel using templates

<?php

namespace App\Channels;

use Illuminate\Notifications\Notification;
use GuzzleHttp\Client as Guzzle;
use Exception;

class EnhostTemplateChannel
{
    /**
     * Send the given notification.
     *
     * @param  mixed  $notifiable
     * @param Notification $notification
     * @return void
     */
    public function send($notifiable, Notification $notification)
    {
        if (!config('services.enhost.apikey')) {
            throw new Exception('ENHOST_API is not set!');
        }

        $data = $notification->toTemplate($notifiable);

        $client = new Guzzle();
        $client->post('https://mailsend.enhost.io/v4/emails/transactional', [
            'headers' => [
                'X-ElasticEmail-ApiKey' => config('services.enhost.apikey'),
                'Content-Type' => 'application/json'
            ],
            'body' => json_encode([
                "Recipients" => [
                    "To" => [
                        $notifiable->name . " <" . $notifiable->email . ">"
                    ]
                ],
                "Content"=> [
                    "Merge" => $data['merge'],
                    "From" => config('mail.from.name') . " <" . config('mail.from.address') . ">",
                    "TemplateName" => $data['template']
                ]
            ])
        ]);
    }
}
 

Please rate this article to help us improve our Knowledge Base.

0 0