Laravel SMTP send mail using gmail SMTP

Many Web server dose not enable Mail service by defaule. So, Today we are going to learn Laravel SMTP send mail using Gmail SMTP. Also, Let's see how we can send mail using Laravel Gmail SMTP. We can easily set up the SMTP driver for the Laravel application.

Laravel SMTP send mail using Gmail SMTP

Once we are ready with Laravel let's start the Laravel SMTP setting using Gmail SMTP step by step. Let's open the ".env" file then follow the instruction

  1. First, we need to change the MAIL_DRIVER to smtp
  2. We need to set up the MAIL_HOST like "smtp.gmail.com" or "mail.domainname.com"
  3. We need to set up the MAIL_PORT correct port for TLS and SSL like 587, 445, 25 etc...
  4. provide the username for the authentication MAIL_USERNAME
  5. provide the password for the authentication MAIL_PASSWORD
  6. set up the encryption MAIL_ENCRYPTION for the authentication SMTP detail

After, setting up the server detail ".env" look like

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremail@gmail.com
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=TLS

Finally, We are ready with the SMTP If you are using Gmail as SMTP server then make sure don't forget to enable Less Secure app.

Now, let's check Laravel SMTP is working on not by sending the mail using Laravel like.

Route::get('/send-mail', function () {
    Mail::send('emails.laravelhive',array('name' => 'Laravel Hive'), function ($message) {
        $message->from('info@laravelhive.com','Laravel Hive');
$message->to('help@laravelhive.com','Laravel Hive');
$message->subject('Laravel SMTP send mail using gmail SMTP');
    });
    print 'Mail sent';
});

Now, let's create the email template laravelhive.blade.php into the "resources/views/emails" directory.

<html>
    <body>
<div>
        <h1>Welcome to {!! $name !!}!</h1>
</div>
    </body>
</html>

Finally, We are ready for sending the testing email by following "http://localhost:8000/send-mail" URL.

Tags:

Related Posts: