Send transactional email with Sendgrid in Laravel
Here I will show you how to send email by Sendgrid templates. In sendgrid transactional templates you can create template layout and design with the use of page builder, no need add custom css and alo no need create blade email template view. You need to create an account on sendgrid if you want to use sendgrid in your application or webiste. You can send 100 emails a day in free plan.
First you need install pacakage sendgrid/sendgrid using composer:
composer require sendgrid/sendgrid
After that need to create Sendgrid API key which is to be generated in your Sendgrid account. Add below variables in your .env file.
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
In you controller include sendgrin Mail() function using new \SendGrid\Mail\Mail();
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)
]
);
// if you want to send bcc
$email->addBcc(
'admin2@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";
}
}
Here I will show you how to send email by Sendgrid templates. In sendgrid transactional templates you can create template layout and design with the use of page builder, no need add custom css and alo no need create blade email template view. You need to create an account on sendgrid if you want to use sendgrid in your application or webiste. You can send 100 emails a day in free plan.
First you need install pacakage sendgrid/sendgrid using composer:
composer require sendgrid/sendgrid
After that need to create Sendgrid API key which is to be generated in your Sendgrid account. Add below variables in your .env file.
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
In you controller include sendgrin Mail() function using new \SendGrid\Mail\Mail();
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)
]
);
// if you want to send bcc
$email->addBcc(
'admin2@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";
}
}
Recommanded Articles
- How to create a multilevel category and subcategory in Laravel
- How to check YouTube video exist Laravel validation
- Multiple user roles authentication Laravel 8
- Deploy Laravel project from local to production server
- Make custom pagination URL in Laravel without query strings
- Web Scraping in Laravel using Goutte
- Insert values during migration run laravel
- Validation for string characters only with custom message in Laravel
- Add new columns in a table Laravel
- How to create foreign key constraints in Laravel
Latest Comments
Abrahim hussain
14 Dec 2020Admin
16 Dec 2020Abrahim hussain
19 Dec 2020Ricky Dev
20 Dec 2020Shakir
21 Dec 2020QIiRYCwoXntemp
11 Mar 2021VEhetqrodG
11 Mar 2021VvihmgaoHC
21 Mar 2021bQxNBZuw
21 Mar 2021