9951 explained code solutions for 126 technologies


phpmailerHow can I send emails using PHPMailer in Laravel 8?


Sending emails using PHPMailer in Laravel 8 is achieved by using the send method of the Mail facade. This method accepts a Message instance, which is created by using the message method of the Mail facade.

The message method accepts a Closure that contains the message configuration such as the subject, from address, to address, and the message body.

Example code

Mail::send(
    ['text'=>'mail'],
    $data,
    function($message) use ($data)
    {
       $message->to($data['email'], $data['name'])
               ->subject('Welcome to My Site!');
    }
);

The send method also accepts a Mailable instance, which is created by extending the Mailable class. This class contains methods to configure the message such as from, subject, view, attach, etc.

Example code

use App\Mail\WelcomeMail;

Mail::to($data['email'], $data['name'])->send(new WelcomeMail());

The send method returns a Swift_Message instance, which is an object representation of the message.

Helpful links

Edit this code on GitHub