1) First Install Sendgrid/sendgrid package
composer install sendgrid/sendgrid
2) Add API key in .env file If you are using laravel like this
MAIL_DRIVER=sendgrid
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=sendgrid_username
MAIL_PASSWORD=sendgrid_password
MAIL_ENCRYPTION=tls
SENDGRID_API_KEY=sendgrid_api_key
3) In controller add sendgrid Mail() api
public function sendEmail(){
$email = new \SendGrid\Mail\Mail();
$email->setFrom("userfrom@mail.com", "My Website");
$email->setSubject("I'm replacing the subject tag");
//You can send variables values on sendgrid templates using addTO
//I've sent 2 variables in array with keys username, useremail
// In send grid template i used {{username}} and {{useremail}} to get dynamic values
$email->addTo(
'admin@mail.com',
"Example User1",
[
"username" => ucwords($user->name),
"useremail" => ucwords($user->email)
]
);
$email->setTemplateId("YOUT TEMPLATE ID");
$sendgrid = new \SendGrid('YOUR KEY HERE');
try {
$response = $sendgrid->send($email);
dd('Mail Sent');
//print $response->body() . "\n";
} catch (Exception $e) {
echo 'Caught exception: ' . $e->getMessage() . "\n";
}
}
That it thank you for more details check here ...
https://usingphp.com/post/send-transactional-email-with-sendgrid-in-laravel
No comments:
Post a Comment