How to send email via gmail smtp in laravel
How to send email via smtp in laravel
How to send email via any smtp in laravel
Here we will learn how to send mail via GMAIL smtp in laravel. Send email via gmail smtp. First if all create a new laravel project from command if you have not set up the laravel project. If you have project then skip this.
Step 1: Open the project root folder 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_ADDRESS="your_email_address"
MAIL_FROM_NAME="${APP_NAME}"
Step 2: Now, Let’s create a mail class in Laravel as follows. This class know as Generating Mailables class.
$ php artisan make:mail UserMail
After Mail successfully created, you will find the file path: app/Mail/UserMail:
Step 3: Now, open the Mailable file at app/Mail/UserMail and paste the below code as follows:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class UserMail extends Mailable
{
use Queueable, SerializesModels;
public $user_data;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($user_data)
{
$this->user_data = $user_data;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
// return $this->view('view.name');
$from_name = "Your name"; // You can change this
$from_email = "youremail@gmail.com"; // You can change this
$subject = "Test mail send from gmail "; // You can change this
return $this->from($from_email, $from_name)
->view('emails.usermail')
->subject($subject)
;
}
}
Step 4: Now, create a email template design, file named with usermail.blade.php, in this path of resourse/views/emails/usermail.blade.php.
<div class="container">
<div class="heading">
<h1>Email send from gmail. </h1>
</div>
<table id="contact">
<tr>
<th>Full Name</th>
<td>{{ $user_data['fullname'] }}</td>
</tr>
<tr>
<th>Email</th>
<td>{{ $user_data['email'] }}</td>
</tr>
<tr>
<th>Subject</th>
<td>{{ $user_data['subject'] }}</td>
</tr>
<tr>
<th>Message</th>
<td>{{ $user_data['message'] }}</td>
</tr>
</table>
</div>
Note — You change this file as per your requirement.
Step 5: Now, create a file controller named UserController with the following command:
$ php artisan make:controller UserController
Step 6: go to your controller folder, path: app/Http/Controllers/UserController and Change code like bellow:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Mail\UserMail;
use Illuminate\Support\Facades\Mail;
class UserController extends Controller
{
public function sendMail(Request $request)
{
$user_data = [
'fullname' => 'abc', // Change this as you need
'email' => 'abc@gmail.com', // Change this
'subject' => 'mail subject', // Change this
'message' => 'mail message', // Change this
];
Mail::to('yourGmailID@gmail.com')->send(new UserMail($user_data));
return redirect()->back()->with('status','Thank you for sign up. We will get back to asap.!');
}
}
Step 7: Make a route for calling the contact form and submitting the form as follows:
Route::post('send-mail','UserController@sendMail');
Step 8: 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”.
Note — If “Less secure app access” not work. Please generate app password in setting and use this password in .ENV file password field.
Thanks for reading.