Send Email In Laravel with SendGrid

While Building any product we often need to notify our customers in various ways and hence we email, message, push notification to them. In this tutorial, we are going to see how we can send email in Laravel with our SendGrid account in simple steps

Laravel uses the popular SwiftMailer library and provides a simple API. Laravel 5.8 comes with SMTP, Mailgun, Postmark, SparkPost, Amazon SES and sendmail drivers.

Step 1 : Install Guzzle HTTP library´╗┐

All of the API drivers require the Guzzle HTTP library, so before starting to send mail we need to install this library via the composer.

composer require guzzlehttp/guzzle

Step 2 : Configure SendGrid Credential

If you don’t have an account on SendGrid yet then you can create one here https://sendgrid.com/ and make sure to activate that. After creating an account we are going to configure it in Laravel environment file.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME={YOURUSERNAMEOFSENDGRIDACCOUNT}
MAIL_PASSWORD={YOURPASSWORDOFSENDGRIDACCOUNT}
MAIL_ENCRYPTION=tls

Step 3 : Generating Mailables

In Laravel, each type of email sent by your application is represented as a “mailable” class. We are going to create NewPostPublished Mailable class to send email to subscribed user if there is new post in our blog published. To create this class run below command in your console.

php artisan make:mail NewPostPublished

This class will be stored in the app\Mail directory and it looks like as below by default.

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class NewPostPublished extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('view.name');
    }
}

Step 4 : Updating Mailables

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Post; //Model for Post

class NewPostPublished extends Mailable
{
    use Queueable, SerializesModels;

    public $post;  //Post Instance

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(Post $post)
    {
        //
        $this->post = $post;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.newpostpublished');
    }
}

Step 5 : Configure the View File

In our Mailable class build method we have used view method to render the template for our email content. Our Email content will be placed in our view newpostpublished.blade.php file located under resources/views/emails/ directory.

resources/views/emails/newpostpublished.blade.php

<div>
   <p>Our New Post is Published</p>
    <h1>{{ $post->title }}</h1>
</div>

Step 5 : Send Mail

Mail::to($request->user())
    ->cc($moreUsers)
    ->bcc($evenMoreUsers)
    ->send(new NewPostPublished($post));

References

https://laravel.com/docs/5.8/mail

Leave a Reply

Your email address will not be published. Required fields are marked *