In this tutorial, we will look at how to send email GMAIL with Laravel 5.2, which is very useful in developing application and very easy way. I want to share my experience to configure email and a little bit about the code.

INTRO

Laravel is a free, open-source PHP web framework, created by Taylor Otwell. Some of the features of Laravel are a modular packaging system with a dedicated dependency manager, different ways for accessing relational databases, utilities that aid in application deployment and maintenance and model–view–controller (MVC) architectural pattern.

As of March 2015, This framework is regarded as one of the most popular PHP frameworks, together with Symfony2, Nette, CodeIgniter, Yii2 and other frameworks.

For this tutorial, all you need is a Gmail Account.

Configuring Gmail

We need the app password for using our app to sending email through their SMTP.

IMPORTANT!! If we use our account password, Google will suspend our account 🙁

And here the step:

Go To My Account

Go To My Account

Go to Sign in & Security

Go to Sign in & Security

Turn On 2-Step Verification

Turn On 2-Step Verification

Create App Password

Create App Password

Generate App Mail Password - Select the device with your custom name app

Generate App Mail Password – Select the device with your custom name app

 

After that,  Google will generate your app password, Copy and Paste to your Laravel Configuration.

Code in Laravel

  1. Edit your app/config/mail.php
    return array(
    
    'driver' => 'smtp',
    'host' => 'smtp.gmail.com',
    'port' => 587,
    'from' => array('address' => 'senderemail@gmail.com', 'name' => 'Sender Name'),
    'encryption' => 'tls',
    'username' => 'senderemail@gmail.com',
    'password' => '123456',   // it's use your google app password 
    'sendmail' => '/usr/sbin/sendmail -bs',
    'pretend' => false,
    
    );

    OR edit your .env file

    MAIL_DRIVER=smtp
    MAIL_HOST=smtp.gmail.com
    MAIL_PORT=587
    MAIL_USERNAME=senderemail@gmail.com
    MAIL_PASSWORD=123456  // it's use your google app password 
    MAIL_ENCRYPTION=tls
    
  2. Create your email view file. Create a new file in app/resources/view/emails named email.blade.php
     <h1>It's Your Email</h1>
  3. ROUTES – Email Sending function. put this code wherever you want (in routes or controller).
    Mail::send('emails.email', $data, function($message) {
        $message->to('receiveremail@gmail. com', 'Receiver Name')->subject('This is the subject');
    });

Other Links

in Bahasa :

https://mul14.wordpress.com/2015/06/03/mengirim-email-menggunakan-laravel/

in English :

http://stackoverflow.com/questions/32515245/how-to-to-send-mail-using-gmail-in-laravel-5-1

Documentation :

https://laravel.com/docs/5.2

Share This

Share This

Share this post with your friends!