How to send mail in laravel

In Laravel, you can send mail using the Mail facade or the Illuminate\Support\Facades\Mail class. Here are the steps to send a mail in Laravel:

Method 1: Using the Mail facade

  1. Open your controller or any other file where you want to send the mail.
  2. Use the Mail facade to send the mail:
    
    use Illuminate\Support\Facades\Mail;

Mail::send('emails.your_email_view', ['user' => $user], function($message) { $message->from('[email protected]', 'Your Name'); $message->to('[email protected]', 'Recipient Name'); $message->subject('Your Email Subject'); });

In this example, `emails.your_email_view` is the name of the email view that will be used to render the email content. The second argument is an array of data that will be passed to the view. The third argument is a closure that allows you to configure the email message.

**Method 2: Using the `Illuminate\Support\Facades\Mail` class**

1. Open your controller or any other file where you want to send the mail.
2. Use the `Illuminate\Support\Facades\Mail` class to send the mail:
```php
use Illuminate\Support\Facades\Mail;

$mail = new \Illuminate\Mail\Message();
$mail->from('[email protected]', 'Your Name');
$mail->to('[email protected]', 'Recipient Name');
$mail->subject('Your Email Subject');
$mail->setBody('Your email body');
Mail::send($mail);

In this example, we create a new Illuminate\Mail\Message object and configure it with the email details. Then, we pass the message object to the Mail::send() method to send the mail.

Configuring the mail settings

Before sending mail, you need to configure the mail settings in your Laravel application. You can do this by publishing the mail configuration file using the following command:

php artisan vendor:publish --provider="Illuminate\Mail\MailServiceProvider"

This will create a config/mail.php file in your Laravel project. You can configure the mail settings in this file, such as the mail driver, host, port, username, and password.

Mail drivers

Laravel supports several mail drivers, including:

You can configure the mail driver in the config/mail.php file.

Email views

Laravel uses email views to render the email content. You can create email views in the resources/views/emails directory. For example, you can create a welcome.blade.php file in this directory to render the email content.

That's it! With these steps, you should be able to send mail in Laravel.