Sending Email Via Gmail SMTP Server In Laravel

Do you want to use the Gmail SMTP server for sending emails in Laravel? Sometimes your server does not send emails due to its limitations or misconfigurations. In such scenarios, you can use the SMTP server for sending emails. In this article, we discuss how to send email using the Gmail SMTP server in Laravel.

Using the SMTP server, your emails will not end up in the spam folder. In general, it will be treated as a genuine email and goes inside the user’s inbox.

One more benefit of using the SMTP server is, you can send emails from your local server also. It will be helpful to test the email functionality on the local server itself.

Setup of Gmail SMTP Server in Laravel

Laravel uses config/mail.php file to store the details related to email providers. This file contains settings like MAIL_HOST, MAIL_PORT, MAIL_ENCRYPTION, etc. The user should provide this information which will be used by Laravel and send your emails.

To add this information, you don’t need to edit config/mail.php. Instead, you should store these details in the .env file.

Open your .env file which is located in your root directory and you will find below code related to email settings.

MAIL_MAILER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

Edit the above details as follows.

MAIL_MAILER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=GMAIL_USERNAME
MAIL_PASSWORD=GMAIL_PASSWORD
MAIL_ENCRYPTION=ssl

Here, I set the driver as smtp, host for Gmail as smtp.googlemail.com, SMTP port for Gmail as 465, and encryption method to ssl. Make sure to replace the placeholders GMAIL_USERNAME, GMAIL_PASSWORD with your actual Gmail username and password.

In order to use the Gmail SMTP server, you need to change some settings on your Google account. Login to your Google account and click on ‘Account’. Once you are on the ‘Account’ page, click on ‘Security’. Scroll down to the bottom and you will find ‘Less secure app access’ settings. Set it to ON.

less-secure-apps

Code for Sending Email in Laravel

At this stage, you are completed with all basic setup. Now, you need to write a Laravel code that will send an email.

For this tutorial, I am going to use a ‘Mail’ class to write code. The user can also write the code using Laravel Mailable. It will not make any difference. In both cases, Laravel uses the Gmail SMTP server in the background.

When you use the ‘Mail’ class your code will be something like as written below.

$to_name = 'TO_NAME';
$to_email = 'TO_EMAIL_ADDRESS';
$data = array('name'=>"Sam Jose", "body" => "Test mail");
    
Mail::send('emails.mail', $data, function($message) use ($to_name, $to_email) {
    $message->to($to_email, $to_name)
            ->subject('Artisans Web Testing Mail');
    $message->from('FROM_EMAIL_ADDRESS','Artisans Web');
});

In the above code, I am using the view ’emails.mail’. It means you have to create a folder and file as resources->views->emails->mail.blade.php.

Your mail.blade.php will contain code as follows.

Hi <strong>{{ $name }}</strong>,
 
<p>{{ $body }}</p>

That’s it! Now, in the background Laravel will automatically use the Gmail SMTP server and send your emails.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

44 thoughts on “Sending Email Via Gmail SMTP Server In Laravel

  1. hi sir ,
    i ahve got errro like this after connecting to smtp
    Expected response code 220 but got an empty response , Could you please help

  2. Hello
    I need to explain my situation.

    Localhost (php artisan serve) : First I got some problem and I needed to add following code in config/mail.php and everything worked fine :
    ‘stream’ => [
    ‘ssl’ => [
    ‘allow_self_signed’ => true,
    ‘verify_peer’ => false,
    ‘verify_peer_name’ => false,
    ],
    ],

    But when I hosted a website to 000webhost I got the following error :
    Expected response code 250 but got code “550”, with message “550 5.7.1 Relaying denied ”

    I would be very grateful for an answer. Thank You

  3. hello, dear!
    Thanks for your article.

    $to_name = ‘TO_NAME’;
    $to_email = ‘TO_EMAIL_ADDRESS’;
    $data = array(‘name’=>”Sam Jose”, “body” => “Test mail”);

    To_NAME, TO_EMAIL_ADDRESS
    what is that?
    What can I write here?

  4. Is it required ssl certificate for shared hosting?
    I followed all setting that you are are followed.
    It show “Connection could not be established with host smtp.googlemail.com [Network is unreachable #101]”

  5. Hi Sajid,

    Thank you for the tutorial! It was really helpfull.

    The end of july, I received an email from Google Gsuite that they will drop support for apps with ‘less secure access’. They will remove the option the 30th of October.

    This means we will have to authenticate differently. I don’t know what options there are , but keep in mind, this will probably not being not supported in the future.

    regards,
    tom

  6. Hi i got this error please help me out
    Address in mailbox given [TO_EMAIL_ADDRESS] does not comply with RFC 2822, 3.6.2.

  7. Hello good evening, I would like to realy appreciate this post thank you worked well on me. Now is that we pushed the files through GIT and my other members get to open this page where email is needed. They received the error of 250 but received 530. Hope you see this asap need help. Thank you

  8. sir

    please help me

    MAIL_DRIVER=sendmail
    MAIL_HOST=smtp.gmail.com
    MAIL_PORT=465
    MAIL_USERNAME=email@domain.com
    MAIL_PASSWORD=123456
    MAIL_ENCRYPTION=ssl
    MAIL_FROM_ADDRESS= What is here????????
    MAIL_FROM_NAME= What is here????????
    I do not understand what I want to write here

    1. You don’t need to add these details as GMAIL server overrides it. But when you don’t use GMAIL SMTP then you can put your admin address and name there.

  9. i had this problem InvalidArgumentException
    View [emails.mail] not found.
    i verified the path of mail.blade.php inside the views folder and its correct

    1. It should not be the case. There must be something wrong in your code or file path. Can you share a screenshot of filesystem and code you have written?

  10. Hi,
    I’m new to Laravel. Can you tell me which file should the following code be written on? Thank you.

    $to_name = ‘TO_NAME’;
    $to_email = ‘TO_EMAIL_ADDRESS’;
    $data = array(‘name’=>”Sam Jose”, “body” => “Test mail”);

    Mail::send(’emails.mail’, $data, function($message) use ($to_name, $to_email) {
    $message->to($to_email, $to_name)
    ->subject(‘Artisans Web Testing Mail’);
    $message->from(‘FROM_EMAIL_ADDRESS’,’Artisans Web’);
    });

    Regards.

  11. Hello,

    This works just fine, thanks so much for sharing!

    I just have one question, how can i do to set the subject and the to_email_address by using variables??

    Something like:

    Mail::send($template, $data, function($message) {
    $message->to($to_email_adrres, $to_name)
    ->subject($subject);
    });

    My $data variable has that info, but is not available inside the send function, and if I pass an object through the function it throws me an error.

  12. (1/1) Swift_TransportException
    Process could not be started [The system cannot find the path specified.
    ]

    how to fix this?

    1. Daniel, saving email in the outbox is up to your email client. This tutorial is only focused on sending emails to users.

Leave a Reply

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