Send transactional email with Sendgrid in Laravel

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();

Also Read: Get next and previous post link in Laravel
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";
    }
}

 

Also Read: API versioning in Laravel App

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();

Also Read: Get next and previous post link in Laravel
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";
    }
}

 

Also Read: API versioning in Laravel App

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Abrahim hussain
Abrahim hussain
14 Dec 2020

Great ! Is it free or paid?

Admin
Admin
16 Dec 2020

Abrahim hussain, there are different packages in Sendgrid. In free plan you can send 100 emails per day. You have to purchase a plan if you want to send emails more than 100 a day.

Abrahim hussain
Abrahim hussain
19 Dec 2020

Thank you buddy.

Ricky Dev
Ricky Dev
20 Dec 2020

Working fine. Thanks

Shakir
Shakir
21 Dec 2020

Or simply MAIL_USERNAME=apikey MAIL_PASSWORD=sendgrid_api_key

QIiRYCwoXntemp
QIiRYCwoXntemp
11 Mar 2021

sAubqgzLUN

VEhetqrodG
VEhetqrodG
11 Mar 2021

oQkKzORfweMZpAUI

VvihmgaoHC
VvihmgaoHC
21 Mar 2021

UcorEaHBWvdn

bQxNBZuw
bQxNBZuw
21 Mar 2021

BYDEUMtNnJcICrQ

Add your comment

Close