How to send mail example in Laravel 5?

By Hardik Savani | April 25, 2016 | | 16075 Viewer | Category : Laravel


Share this post:

Laravel 5 provide several way to send email. You can also use core PHP method for send mail and you can also use some email service providers such as sendmail, smtp, mandrill, mailgun, mail, gmail etc. So you can choese any one and set configration. Laravel 5 provide Mail facade for mail send that have sevaral method for send email.In this example i going to show you how to send emails from gmail account example. This example is very simple you can use as requirement easily. It is very simple to configration with gmail account so first open your .env file and add bellow gmail configration.

.env

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com

MAIL_PORT=587

MAIL_USERNAME=harshadpathak1313@gmail.com

MAIL_PASSWORD=mypassword

MAIL_ENCRYPTION=tls

Now we are ready to send mail for test so first create test route for email sending.

app/Http/routes.php

Route::get('mail', 'HomeController@mail');

Ok, now add mail function in HomeController.php file so add this way :

app/Http/Controllers/HomeController.php

public function mail()

{

$user = User::find(1)->toArray();


Mail::send('emails.mailExample', $user, function($message) use ($user) {

$message->to($user->email);

$message->subject('E-Mail Example');

});


dd('Mail Send Successfully');

}

At last create email template file for send mail so let's create mailExample.blade.php file in emials folder.

resources/views/emails/mailExample.blade.php

 

Hello {{ $name }}, I am from Itsolutionstuff.com.

If you still found any error then follow this link : How to set gmail configration for mail in Laravel?.

Video



We are Recommending you: