-
-
Save musaid/fefca23799e1beba9ab78b7510de3f14 to your computer and use it in GitHub Desktop.
Send email with Sendgrid (including template id and substitutions) with Laravel's notifications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Notifications; | |
use Illuminate\Bus\Queueable; | |
use Illuminate\Notifications\Notification; | |
use Illuminate\Contracts\Queue\ShouldQueue; | |
use Illuminate\Notifications\Messages\MailMessage; | |
//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 = "[email protected]"; | |
//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 \Illuminate\Notifications\Messages\MailMessage | |
*/ | |
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 \Sendgrid\Email Configured Email variable with the destination email | |
* | |
* @return \Sendgrid\Personalization Configured Personalizations for the mail | |
*/ | |
private function personalizeEmail( $to ){ | |
$personalization = new \Sendgrid\Personalization(); | |
$personalization->addTo($to); | |
foreach ($this->subs as $key => $value) { | |
$personalization->addSubstitution( $key, (string) $value ); | |
} | |
return $personalization; | |
} | |
/** | |
* Send the email via sendgrid | |
* | |
* @return Object Http response from sendgrid | |
*/ | |
public function sendTransactionEmail(){ | |
$from = new \SendGrid\Email(null, $this->from); | |
$to = new \SendGrid\Email(null, $this->to); | |
//This should be at least 1 character even if it is empty | |
$content = new \SendGrid\Content("text/html", " "); | |
$mail = new \SendGrid\Mail($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 App\Notifications\ExampleNotification | |
//in your controller or the class that you want to apply it | |
$user->notify(new ExampleNotification( $user, "test_message" )); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment