Laravel php mail

Laravel provides a simple and easy-to-use mail system that allows you to send emails from your application. Here's a step-by-step guide on how to use Laravel's mail system:

Step 1: Install the Mail Package

To use Laravel's mail system, you need to install the illuminate/mail package. You can do this by running the following command in your terminal:

composer require illuminate/mail

Step 2: Configure the Mail Driver

Laravel comes with several mail drivers, including smtp, sendmail, and mailgun. You need to configure the mail driver in your config/mail.php file. For example, if you want to use the smtp driver, you can configure it as follows:

'driver' => env('MAIL_DRIVER', 'smtp'),
'smtp_host' => env('MAIL_HOST', 'smtp.gmail.com'),
'smtp_port' => env('MAIL_PORT', 587),
'smtp_username' => env('MAIL_USERNAME'),
'smtp_password' => env('MAIL_PASSWORD'),
'smtp_encryption' => env('MAIL_ENCRYPTION', 'tls'),

Step 3: Set Environment Variables

You need to set the following environment variables in your .env file:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_email_address
MAIL_PASSWORD=your_email_password
MAIL_ENCRYPTION=tls

Step 4: Create a Mail Message

To send an email, you need to create a mail message using the Mail facade. For example:

use Illuminate\Support\Facades\Mail;

Mail::send('emails.welcome', ['name' => 'John Doe'], function ($message) {
    $message->from('your_email_address', 'Your Name');
    $message->to('recipient_email_address', 'Recipient Name');
    $message->subject('Welcome to our application!');
});

In this example, we're sending a welcome email to the recipient with the subject "Welcome to our application!".

Step 5: Create a Mail View

To create a mail view, you need to create a new file in the resources/views/emails directory. For example, you can create a welcome.blade.php file:

<h1>Welcome {{ $name }}!</h1>
<p>Thank you for joining our application!</p>

In this example, we're using Blade syntax to display the recipient's name.

Step 6: Send the Mail

Finally, you can send the mail using the Mail facade:

use Illuminate\Support\Facades\Mail;

Mail::send('emails.welcome', ['name' => 'John Doe'], function ($message) {
    //...
});

That's it! Laravel's mail system is now configured and ready to use.

Additional Tips