Laravel 11 tutorial #64 Send Email | laravel send mail with smtp

3 min read 4 hours ago
Published on Oct 12, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through the process of sending emails using SMTP and Gmail in Laravel 11. By the end of this guide, you'll have a working email configuration in your Laravel application and be able to send test emails.

Step 1: Configure Mail Settings in the .env File

To start sending emails, you need to configure your mail settings in the .env file.

  1. Open your .env file located in the root of your Laravel project.
  2. Update the following lines with your Gmail SMTP settings:
    MAIL_MAILER=smtp
    MAIL_HOST=smtp.gmail.com
    MAIL_PORT=587
    MAIL_USERNAME=your_email@gmail.com
    MAIL_PASSWORD=your_generated_app_password
    MAIL_ENCRYPTION=tls
    MAIL_FROM_ADDRESS=your_email@gmail.com
    MAIL_FROM_NAME="${APP_NAME}"
    
  3. Replace your_email@gmail.com with your actual Gmail address.
  4. For your_generated_app_password, you need to generate an App Password from your Google account.

Practical Tip

  • Ensure that you have "Allow less secure apps" turned on in your Google account settings if you're not using App Passwords.

Step 2: Generate App Password in Gmail

To enhance security, Gmail requires generating an app password for sending emails.

  1. Go to your Google Account.
  2. Navigate to the "Security" section.
  3. Under "Signing in to Google," find "App passwords."
  4. Select "Generate App Password."
  5. Choose the app and device you want to generate the password for, then click "Generate."
  6. Copy the generated password and use it in your .env file.

Step 3: Create a Mailable Class

Next, you need to create a mailable class that will handle the email content.

  1. Run the following Artisan command in your terminal:
    php artisan make:mail TestEmail
    
  2. This command creates a new file in app/Mail/TestEmail.php.

Edit the Mailable Class

  1. Open TestEmail.php and modify the build method:
    public function build()
    {
        return $this->view('emails.test')
                    ->subject('Test Email from Laravel');
    }
    
  2. Create a new directory called emails inside the resources/views folder.
  3. Inside the emails directory, create a new Blade file named test.blade.php.

Example Email Template

Add the following HTML content to test.blade.php:

<h1>Hello from Laravel!</h1>
<p>This is a test email sent from your Laravel application.</p>

Step 4: Create a Controller for Sending Email

Now, create a controller that will handle the email sending logic.

  1. Run the following command:
    php artisan make:controller MailController
    
  2. Open MailController.php and add the following code:
    use App\Mail\TestEmail;
    use Illuminate\Support\Facades\Mail;
    
    public function sendEmail()
    {
        Mail::to('recipient@example.com')->send(new TestEmail());
        return 'Email sent successfully!';
    }
    

Step 5: Define a Route

To access the email sending functionality, define a route.

  1. Open routes/web.php and add the following line:
    Route::get('/send-email', [MailController::class, 'sendEmail']);
    

Step 6: Test Sending the Email

Now that everything is set up, you can test sending an email.

  1. Start your Laravel server using:
    php artisan serve
    
  2. Open your web browser and navigate to http://localhost:8000/send-email.
  3. You should see a message indicating that the email was sent successfully.

Conclusion

In this tutorial, you learned how to configure SMTP settings for Gmail in Laravel 11, create a mailable class, set up a controller for sending emails, and define routes. By following these steps, you can easily send emails from your Laravel application.

Next steps could include exploring email queueing for better performance or customizing your email templates further.