Sending emails using SendGrid in Laravel

Sending emails using SendGrid in Laravel

Hello folks, here we ‘ll know how to send emails using sendgrid in laravel. We have many ways to send emails in laravel, we can send emails using plain php or we can use email service providers like SendGrid, mailgun, mandrill and many more. As one of the follower of this site has request me to post a tutorial on ‘sending emails using sendgrid’ am posting this here.

Follow these few simple steps,

Step 1 – Register SendGrid Account

Go to .
Click Try for Free button
Fill all the necessary details
Wait for a email from SendGrid conforming that your account is provisioned.

Cool,you are ready to go.

Step 2 – Change mail configurations in .env and mail.php files

Open .env located at root of the application, edit the file as below


Now open mail.php located in /config/mail.php, (for further reference on folder/application structure look at previous post on Laravel application structure)
edit the file as below,

return [ 
        'driver' => 'smtp',
        'host' => '',
        'port' => 587,
            'encryption' => 'tls', 
        'username' => 'sendgridUsername',
        'password' => 'sendgridPassword',
        'sendmail' => '/usr/sbin/sendmail -bs' 

if your website has ssl certificate, change the line 'encryption' => 'tls' to'encryption' => 'ssl' and 'port' => 465,

Step 3 – Send emails the way you want

We ‘ll create some textarea, and the text entered will be sent as a mail when an valid email is entered,
check the form below,

<form class="w3-container" action="sendemail" method="POST"
            {{ csrf_field() }}
                <label>Enter Some Text</label>
                <textarea class="w3-input" type="text" name="message"></textarea>
                <label>Email</label> <input class="w3-input" type="email"> <input
                    type="submit" name="toEmail" class="w3-btn w3-orange" value="Send">

and we create another form just with email field,

<form class="w3-container" action="sendemail" method="POST"
            {{ csrf_field() }}
                <label>Email</label> <input class="w3-input" name="toEmail" 
                <input type="submit" class="w3-btn w3-orange" value="Send">

When the send button is clicked we route it to the following function,

Route::any ( 'sendemail', function () {
    if (Request::get ( 'message' ) != null)
        $data = array (
                'bodyMessage' => Request::get ( 'message' ) 
        $data [] = '';
    Mail::send ( 'email', $data, function ($message) {
        $message->from ( '', 'Just Laravel' );
        $message->to ( Request::get ( 'toEmail' ) )->subject ( 'Just Laravel demo email using SendGrid' );
    } );
    return Redirect::back ()->withErrors ( [ 
            'Your email has been sent successfully' 
    ] );
} );

and the view for the emails which we are going to be send be like,

<!DOCTYPE html>
<html lang="en-US">
<meta charset="utf-8">
    <h2>Demo mail from JustLaravel</h2>

    <div class="w3-container w3-orange">

            <b>The data you have entered is :</b><span
                style="color: #e36c39; background: #EEE";> {{ $bodyMessage }}</span>



That’s it, look the below screen shots for reference,

Sending emails using SendGrid -
Sending emails using SendGrid –
Sending emails using SendGrid -
Sending emails using SendGrid –

14 thoughts on “Sending emails using SendGrid in Laravel”

  • 1
    Crecendia on June 9, 2016 Reply

    I’ll give it try. What the laravel version did you use?

  • 3
    Gabriel Piriz on August 2, 2016 Reply

    Hello, I’m having a problem when trying to send a mail. The laravel error is the following:

    ErrorException in StreamBuffer.php line 95:
    stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
    error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed

    I would appreciate if you can help me, thanks!

    • 4
      avinash on August 2, 2016 Reply

      That problem was related to SSL config, make sure SSL is enabled in your server and its configuration is right.

  • 5
    Richard Kalauli on August 16, 2016 Reply

    Exceptional post however I was wanting to know if you could write a litte more on this topic? I’d be very grateful if you could elaborate a little bit more. Appreciate it!|

    • 6
      avinash on August 16, 2016 Reply

      Sure, will try to edit this post.

      Cheers 🙂

  • 7
    Mat Velish on January 17, 2017 Reply

    Hi, thanks for upload this article.

    Im facing an annoying issue that I don’t know how to fix and maybe you can please drive me at least on the right direction:

    – Created my sendgrid account.
    – Created my apikey and gave mail permissions.
    – Setup .env values and also on config/mail — from SendGrid tutorial I understand username for smtp is always ‘apikey’ and the actual apikey value as password.

    no matter what I try I keep having this error message back:

    Expected response code 250 but got code “550”, with message “550 Unauthenticated senders not allowed”

    Im not sure what I’m missing.

    Thanks for your time.

    • 8
      avinash on January 17, 2017 Reply

      Hello Mat Velish,

      If you look at this post’s ‘Step 2’, you will notice that in `.env` file, you need to give sendGrid login credentials like username and password and not any ‘apikeys’ and even in `config/mail.php` you need to provide the same.

  • 9
    neha on November 28, 2017 Reply

    hi avinash, i provide the exact details to env file and mail file but still it’s giving me error Expected response code 250 but got code “550”, with message “550 Unauthenticated senders not allowed “. Can u please guide, where i am going wrong. Thanks for your help.

  • 10
    anilblog88 on December 13, 2017 Reply

    Hi i am also getting the same error.

  • 11
    ahmed on December 27, 2017 Reply

    this error might be due to the gmail security configuration.
    you can try by changing it as follows..
    goto your gmail
    My Account
    Sign-in & security
    at the bottom
    ‘Allow less secure apps’ is by default off ,turn it to on.

Leave a Reply