How To Send Email In Laravel Tutorial

18 Oct

Sending Email in laravel is pretty straighforward but looks little tricky.
Here I am explaining about sending email in laravel using gmail smtp server.

In order to send email using gmail smtp server. you need a gmail account and app password.

1. Obtain Google App Password

An App Password is a 16-digit passcode that gives a non-Google app or device permission to access your Google Account. App Passwords can only be used with accounts that have 2-Step Verification turned on.

Go to your Google Account and Click on security from left menu.

To generate app password you have to enable two step Verification. If you have already enabled the two step Verification then you can see the link to generate app password.

Click on app password .
Now you will be asked to choose app and device,  here you can choose Mail and custom Option then you can type the name website.
Now google will display your 16 digit app password.

2. Put Email setting in .env file

Now open you .env file in laravel  and paste or replace below code like this.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your-gmail-address
MAIL_PASSWORD=you-app-passowrd
MAIL_ENCRYPTION=TLS
MAIL_FROM_ADDRESS=your-gmail-address
MAIL_FROM_NAME=your-name

Now save the file and clear the laravel cache.

3. Creating mailable class

Now we  create a Mailable class. we are assuming that we want to send email on contact form submission. To create Mailable class Execute the following commands.

php artisan make:mail ContactMail

After executing this command a mailable class will be created in the folder app/Mail

Open app/Mail/ContactMail.php file and make the changes like this.

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ContactMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
protected $Contact;

public function __construct($Contact)
{
$this->Contact = $Contact;
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view(’emails.contact’)->with([
‘name’ => $this->Contact[‘name’],
’email’ => $this->Contact[’email’],
‘mobile’ => $this->Contact[‘mobile’],
‘message’ => $this->Contact[‘message’],
]);
}
}

4. Creating Email template file

Now we will create email template file. so we can send html emails. Email template file resides in resources/views folder like our other views files.

In resources/views folder create a new folder emails and in this folder create a new file called contact.blade.php and paste the below code as given below.

<h1>Dear Admin</h1>
New Inquiry received.
<table width="500px" cellspacing="0" cellpadding="5" border="1&quot;">
<tbody>
<tr>
<td>Name</td>
<td>{{$name}}</td>
</tr>
<tr>
<td>Email</td>
<td>{{$email}}</td>
</tr>
<tr>
<td>mobile</td>
<td>{{$mobile}}</td>
</tr>
</tbody>
</table>

5. Writing function in Controller to send email

Add the following line in the use section of your controller, At the top of controller.
use App\Mail\ContactMail;

Now write the function to save contact form data in database and then send email to admin.

public function contacts(Request $request) {
if ($request->isMethod('post')) {
$validatedData = $this->validate($request,[
'name' => 'required',
'email' => 'required|email',
'mobile' => 'required',
'message' => 'required',
]);
$fields = array();
$Contact = new Contact();
$Contact->name = $request->name;
$Contact->email = $request->email;
$Contact->mobile = $request->mobile;
$Contact->message = $request->message;
$Contact->save();
Mail::to('receiveremail@email.com')->send(new ContactMail($Contact->toArray()));
return redirect('/contact');
}
}

Now our email setup is ready. when you will execute this function a mail will be fired to given email address.

Leave a Reply

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

eleven − 8 =