Mail mailer laravel

In Laravel, you can use the Mail facade to send emails. Here's a step-by-step guide on how to use it:

Step 1: Install the Mail Package

If you haven't already, install the illuminate/mail package by running the following command in your terminal:

composer require illuminate/mail

Step 2: Configure the Mail Driver

In your config/mail.php file, configure the mail driver you want to use. For example, you can use the smtp driver:

'driver' => env('MAIL_DRIVER', 'smtp'),

You'll also need to configure the SMTP settings:

'smtp' => [
    'host' => env('MAIL_HOST', 'smtp.gmail.com'),
    'port' => env('MAIL_PORT', 587),
    'username' => env('MAIL_USERNAME'),
    'password' => env('MAIL_PASSWORD'),
    'encryption' => env('MAIL_ENCRYPTION', 'tls'),
],

Step 3: Create a Mail Message

Create a new mail message by using the Mail::raw method:

use Illuminate\Support\Facades\Mail;

Mail::raw('Hello, world!', function ($message) {
    $message->from('[email protected]', 'From Name');
    $message->to('[email protected]', 'To Name');
    $message->subject('Hello, world!');
});

This will send a plain text email with the subject "Hello, world!" and the body "Hello, world!".

Step 4: Send the Mail

To send the mail, use the send method:

Mail::raw('Hello, world!', function ($message) {
    //...
})->send();

This will send the email using the configured mail driver.

Step 5: Use a Mail Template

You can also use a mail template by creating a new mail class that extends the Illuminate\Mail\Mailable class:

namespace App\Mail;

use Illuminate\Mail\Mailable;

class WelcomeEmail extends Mailable
{
    public function build()
    {
        return $this->view('emails.welcome');
    }
}

Then, you can send the mail using the Mail::to method:

Mail::to('[email protected]')->send(new WelcomeEmail());

This will send an email using the welcome view in the emails directory.

Step 6: Customize the Mail

You can customize the mail by adding attachments, CC/BCC recipients, and more. For example:

Mail::raw('Hello, world!', function ($message) {
    $message->from('[email protected]', 'From Name');
    $message->to('[email protected]', 'To Name');
    $message->subject('Hello, world!');
    $message->attach('path/to/file.pdf');
    $message->cc('[email protected]');
    $message->bcc('[email protected]');
});

This will send an email with an attachment, CC recipient, and BCC recipient.

That's it! With these steps, you should be able to send emails using the Mail facade in Laravel.