How to send email using gmail in PHP Laravel 5?


Laravel is very popular framework of php in todays. Laravel is famous because they provide several goods things like MVC, routing, mail etc. In this article we will learn how to send email using SMTP driver. Laravel 5 provide several drivers options like SMTP, Mail, SendMail and services supported include Mailgun, Sendgrid, Mandrill, Amazon SES, etc.

So, here i will let you know how to send email from your gmail account using smtp driver. Laravel provide configuration file for email. you can see that file here : config/mail.php , you can also make change manually but laravel is smart so they provide .env file for configuration as like bellow:

.env

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com

MAIL_PORT=587

MAIL_USERNAME=addYourEmail

MAIL_PASSWORD=AddYourEmailPassword

MAIL_ENCRYPTION=tls

You can simply configuration here as above env variable. now i are going to provide simple example so add new route in your route file as like bellow added.

routes/web.php

Route::get('send-main', 'HomeController@sendMail');

Next, i will added new sendMail() in HomeController like as bellow i added, here in this method we write code of send email with title. So let's see bellow controller method.

Controller Method:

public function sendMail()

{

$data['title'] = "Test it from HDTutu.com";

Mail::send('emails.email', $data, function($message) {

$message->to('aatmaninfotech@gmail.com', 'Receiver Name')

->subject('HDTuto.com Mail');

});

dd("Mail Sent successfully");

}

At Last, we need to create email blade file in emails folder of view. So if you have't created "emails" folder then create and create new email.blade.php file, put bellow code:

resources/views/emails/email.blade.php

<h1>{{ $title }}</h1>

<p>Thank you</p>

Now, we are ready to run above example....

I hope you found your great solution...


July 7, 2017 | 1788 Viewer | Category : PHP Laravel

Related Posts: