Here, in this article, we are going to learn about how to send an email in laravel 5.8 using gmail smtp.
Step 1: Setup the Laravel 5.8 version as follows:
$ composer create-project --prefer-dist laravel/laravel blog "5.8.*"
Step 2: Now, Go to your gmail.com and Login in it, and then click on Mail icon & Choose Account.
Once you are in dashboard, click on Security and scroll to bottom and TURN ON "Less secure app access".
Step 3: Open the project which we created now and open .env file and give the credentials for it:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_email_address
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME=fundaofwebit
Step 4: Now, Let's create a mail class which is known as Generating Mailables in Laravel as follows:
$ php artisan make:mail ContactMailable
After Mail successfully created, you will find the file path: app/Mail/ContactMailable:
Step 5: Now, open the Mailable file at app/Mail/ContactMailable and paste the below code as follows:
<?php
namespaceApp\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
classContactMailableextendsMailable
{
useQueueable, SerializesModels;
public$contact_data;
/**
* Create a new message instance.
*
* @returnvoid
*/
publicfunction__construct($contact_data)
{
$this->contact_data = $contact_data;
}
/**
* Build the message.
*
* @return $this
*/
publicfunctionbuild()
{
// return $this->view('view.name');
$from_name = "Funda of Web IT";
$from_email = "youremail@gmail.com";
$subject = "Funda of Web IT: You have a new query";
return$this->from($from_email, $from_name)
->view('emails.contact')
->subject($subject)
;
}
}
Step 6: Now, we need to create a email template design, file named with contact.blade.php, in this path of resourse/views/emails/contact.blade.php where this template will be sent to email id, Template design given below:
<html>
<head>
<style>
.container {
padding: 10px;
}
#contact {
border-collapse: collapse;
width: 100%;
}
#contacttd,
#contactth {
border: 1pxsolid#ddd;
padding: 8px;
width: 30%;
}
#contactth {
width: 10%;
}
#contacttr:nth-child(even) {
background-color: #f2f2f2;
}
#contacttr:hover {
background-color: #ddd;
}
#contactth {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #8062c7;
color: white;
}
.heading {
background-color: lightgray;
padding: 10px;
}
</style>
</head>
<body>
<divclass="container">
<divclass="heading">
<h1>Funda of Web IT: You a new enquiry from Contact Form</h1>
</div>
<tableid="contact">
<tr>
<th>Full Name</th>
<td>{{$contact_data['fullname'] }}</td>
</tr>
<tr>
<th>Phone Number</th>
<td>{{$contact_data['phone'] }}</td>
</tr>
<tr>
<th>Email</th>
<td>{{$contact_data['email'] }}</td>
</tr>
<tr>
<th>Subject</th>
<td>{{$contact_data['subject'] }}</td>
</tr>
<tr>
<th>Message</th>
<td>{{$contact_data['message'] }}</td>
</tr>
</table>
</div>
</body>
</html>
Step 7: Now, create a file controller named ContactController with the following command:
$ php artisan make:controller ContactController
Step 8: go to your controller folder which we just created, path: app/Http/Controllers/ContactController and paste the below code: