Back to Homepage
Tuesday 13 August 2024
22

Laravel: How to Send Email

Learn how to send emails in Laravel with this comprehensive guide. Discover how to set up mail configurations, create email templates, and send emails using Laravel's built-in features.

How to Send Email in Laravel

Sending emails is a common task in web applications, and Laravel makes this process simple and efficient. Laravel provides a built-in Mail facade and a powerful email API that supports multiple drivers such as SMTP, Mailgun, and Amazon SES. This guide will walk you through the steps to send an email in Laravel.

Setting Up Email Configurations

Before sending emails, you need to configure your email settings. These settings are typically stored in the .env file.

1. Configure the .env File:

Open your .env file and set the following mail configurations:

MAIL_MAILER=smtp
   MAIL_HOST=smtp.mailtrap.io
   MAIL_PORT=2525
   MAIL_USERNAME=your_mailtrap_username
   MAIL_PASSWORD=your_mailtrap_password
   MAIL_ENCRYPTION=tls
   MAIL_FROM_ADDRESS=noreply@example.com
   MAIL_FROM_NAME="${APP_NAME}"

Replace these values with your actual mail server details. You can use services like Mailtrap for testing purposes.

2. Update config/mail.php:

Laravel reads the mail settings from the config/mail.php file. Ensure that it matches your .env configurations:

return [
       'driver' => env('MAIL_MAILER', 'smtp'),
       'host' => env('MAIL_HOST', 'smtp.mailtrap.io'),
       'port' => env('MAIL_PORT', 2525),
       'from' => [
           'address' => env('MAIL_FROM_ADDRESS', 'noreply@example.com'),
           'name' => env('MAIL_FROM_NAME', 'Example App'),
       ],
       'encryption' => env('MAIL_ENCRYPTION', 'tls'),
       'username' => env('MAIL_USERNAME'),
       'password' => env('MAIL_PASSWORD'),
       'sendmail' => '/usr/sbin/sendmail -bs',
       'markdown' => [
           'theme' => 'default',
           'paths' => [
               resource_path('views/vendor/mail'),
           ],
       ],
   ];

Sending an Email

1. Create a Mailable Class:

Use Artisan to generate a mailable class, which will handle the content and structure of your emails:

php artisan make:mail WelcomeEmail

This command creates a new file in app/Mail/WelcomeEmail.php.

2. Define the Email Content:

In the generated WelcomeEmail class, define the email content within the build method:

public function build()
   {
       return $this->view('emails.welcome')
                   ->with([
                       'name' => $this->name,
                   ]);
   }

You can pass data to the email view using the with method.

3. Create an Email Template:

Create a Blade view for your email in resources/views/emails/welcome.blade.php:

<h1>Welcome to Our Application, {{ $name }}!</h1>
   <p>We are excited to have you on board.</p>

4. Send the Email:

Finally, send the email from a controller or any part of your application:

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

   Mail::to('recipient@example.com')->send(new WelcomeEmail($name));

Examples of Sending Emails in Laravel

  • Sending a Simple Email:
Mail::raw('This is a plain text email', function ($message) {
      $message->to('recipient@example.com')
              ->subject('Simple Email');
  });
  • Sending an Email with Attachments:
Mail::to('recipient@example.com')->send(new WelcomeEmail($name));
  • Queueing an Email:
Mail::to('recipient@example.com')->queue(new WelcomeEmail($name));

Official References

Share:
Created by:
Author photo

Jorge García

Fullstack developer