How to Send Email using Gmail in Laravel ?

**How to Send Email using Gmail in Laravel**

Sending emails is a common feature in web applications, whether it’s for user registration, password reset, newsletters, or notifications. In Laravel, sending emails is a breeze with its built-in mail class and support for various mail drivers. In this tutorial, we will focus on sending emails using Gmail SMTP in Laravel.

**Step 1: Setup Gmail SMTP**

Before you can start sending emails using Gmail in Laravel, you need to configure your Gmail account to allow less secure apps access. You can do this by going to your Gmail account settings and enabling “Allow less secure apps.”

Next, you need to configure your Laravel application to use Gmail SMTP. In your `.env` file, add the following SMTP configuration:

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=yourgmailpassword
MAIL_ENCRYPTION=tls

**Step 2: Create Email Template**

You can create an email template using Laravel’s mailables. Run the following command to create a new mailable:

php artisan make:mail WelcomeEmail

This will create a new mailable class in the `app/Mail` directory. Customize the `build` method in the mailable class to define the email content, subject, and recipient:



php
public function build()
{
    return $this->view('emails.welcome')
                ->subject('Welcome to our Website');
}

**Step 3: Send Email**

Now that you have configured Gmail SMTP and created an email template, you can send emails using the `Mail` facade in Laravel. Here’s an example of sending an email in a controller:

php
use App\Mail\WelcomeEmail;
use Illuminate\Support\Facades\Mail;

public function sendEmail()
{
    $email = '[email protected]';
    Mail::to($email)->send(new WelcomeEmail());
    
    return "Email sent successfully";
}

Make sure to import the `WelcomeEmail` class and the `Mail` facade at the top of your controller file.

**Step 4: Testing**

You can test your email sending functionality by triggering the `sendEmail` method in your controller. Once the email is successfully sent, you should receive it in your specified recipient email address.

**Conclusion**

In this tutorial, we have learned how to send emails using Gmail SMTP in Laravel. By following the steps outlined above, you can easily set up email functionality in your Laravel application and start sending personalized emails to your users. Email communication is a crucial aspect of any web application, and Laravel provides a robust and efficient way to handle it.

Post Your Questions on our forum

Post a question on Forum

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

Share with your friends:

Comments are closed.

How to integrate Paypal API in Laravel

Are you looking to integrate Paypal API in your Laravel project for seamless payment processing? Look no further! In this […]

April 3, 2024

How to integrate Razorpay API in Laravel

Integrating payment gateways into web applications has become an essential part of e-commerce websites. In this tutorial, we will discuss […]

April 3, 2024

Laravel 11 Ajax CRUD Operation Tutorial Example

**Mastering CRUD Operations with Laravel 11 Ajax: A Comprehensive Tutorial** In the world of web development, interaction between the front-end […]

April 3, 2024

Login as Client in Laravel – Login with user id

**Unlock the Power of Laravel with Login as Client – Login with User ID** Laravel, the popular PHP framework, offers […]

April 3, 2024

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now