Blog Detail

  • 18 Jul, 2024
  • 821 Views
  • Source :https://goworkable.com/digital-products-ideas-list-to-sell-online/

Know How To Setup Gmail SMTP To Send Email with Laravel The Best Guide

In this tutorial, you'll learn how to use Gmail as your email sending service within a Laravel application. We'll guide you through the essential steps, from configuring SMTP settings to crafting custom email templates. By the end, you'll be equipped to send emails seamlessly from your Laravel project using your Gmail account. Let's get started to Send Email with Laravel!.

To use Gmail to send emails with Laravel, you'll need to configure your Laravel application to use the SMTP protocol and set up the necessary credentials for your Gmail account. Here's a step-by-step guide:

1. Install the guzzlehttp/guzzle package:

composer require guzzlehttp/guzzlece

 

2. Enable "Less secure app access" in your Gmail account settings:

  • - Select Security from the left, or Go to https://myaccount.google.com/security.
  • - Scroll down to "2 step Verification",  click "Enable" and follow the on-screen steps.
  • - This setting allows less secure applications to access your Gmail account.
1-Setup-Gmail-SMTP-To-Send-Email-with-Laravel


Tip: To create an app password, you need "2-Step Verification" on your Google Account, If you can’t setup or enable "2-Step Verification" for any reason, use your smart mobile device  to “Sign in with your Google account” and "Turn on 2-Step Verification" on your Smart phone. As you can setup other verification methods on your mobile device and it's easier to tap a prompt than enter a verification code!. 
 


Turn-on-2-Step-Verification

4-Iworkable.com Setup-Gmail-SMTP-To-Send-Email-with-Laravel


3. Now we generate the app password that we would be using in the Laravel Application.
  • - Go to https://myaccount.google.com/apppasswords.
  • - Type a name for your App and click 'Create".
  • - This would open a pop up with your 16 character Generatedapp passwordSave your password which will be used as a password for your Laravel configuration (Kindly copy this and keep it safe).
4-Setup-Gmail-SMTP-To-Send-Email-with-Laravel

5-Setup-Gmail-SMTP-To-Send-Email-with-Laravel

4. Configure the SMTP settings in your .env file:
MAIL_DRIVER=smtp
 MAIL_HOST=smtp.gmail.com
 MAIL_PORT=587
 MAIL_USERNAME=your_gmail_address@gmail.com
 MAIL_PASSWORD=your_app_password
MAIL_ENCRYPTION=tls

 

5. Replace your_gmail_address@gmail.com with your actual Gmail, and your_app_password with your 16 character "Generated app password" in steps above.

Important: Don't use  your google account password in .eve file. Instead you need to setup "app password" as shown in the steps above to generate new "app password" that we would be using in the Laravel Application.

 

6. We are all set to create a new email class in your Laravel application:

<?php

namespace App\Mail;

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


class ContactEmail extends Mailable
{
    use Queueable, SerializesModels;

    public $name;
    public $email;
    public $message;

    /**
     * Create a new message instance.
     *
     * @param  string  $name
     * @param  string  $email
     * @param  string  $message
     * @return void
     */
    public function __construct($name, $email, $message)
    {
        $this->name = $name;
        $this->email = $email;
        $this->message = $message;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('Contact Form Submission')
            ->view('emails.contact')
            ->with([
                'name' => $this->name,
                'email' => $this->email,
                'message' => $this->message,
            ]);
    }
}
 
7. Create a new email template in your resources/views/emails directory to Send Email with Laravel :
<html>
<head>
<title>Contact Form Submission</title>
</head>
<body>
<p>Name: {{ $name }}</p>
<p>Email: {{ $email }}</p>
<p>Message:</p> <p>{{ $message }}</p>
</body>
</html>
 
8. Send the email from your controller:
<?php

namespace App\Http\Controllers;

use App\Mail\ContactEmail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class ContactController extends Controller   

{
    public function store(Request $request)
    {
        // Validate the request data
        $request->validate([
            'name' => 'required',
            'email' => 'required|email',   

            'message' => 'required',
        ]);

        // Send the email
        Mail::to('your_recipient_email@example.com')->send(new ContactEmail(
            $request->input('name'),
            $request->input('email'),
            $request->input('message')
        ));

        // Return a success message or redirect
        return redirect()->back()->with('success', 'Email sent successfully!');
    }
}

Remember to replace your_recipient_email@example.com with the actual email address you want to send the email to. By following these steps, you'll be able to use Gmail to send emails Send Email  with Laravel application.

 

Conclusion:

In conclusion, integrating Gmail with Laravel provides a robust and efficient solution for Send Email with Laravel applications. By following the steps outlined in this article, you can easily configure your Laravel project to utilize Gmail's SMTP server, ensuring that your emails are delivered reliably and professionally. Whether you're sending notifications, confirmations, or marketing campaigns, Gmail's integration offers a seamless and effective way to enhance your Laravel application's communication capabilities.