Laravel provides a robust API over the popular SwiftMailer library with drivers for SMTP, PHP's mail, sendmail and more. In this example, we will be sending an email using the SMTP Driver. For more information, check out the docs for Laravel's Mail interface.


Add the following to your .env file:

[email protected]

You can find your SMTP username from your General Settings page.

Create a mailable

Next, you need to create a Mailable class. Laravel's CLI tool Artisan makes it easy. From the CLI:

php artisan make:mail MyTestEmail

This creates a new file under app/Mail/MyTestEmail.php and it should look something like this:


namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class MyTestEmail extends Mailable
    use Queueable, SerializesModels;

    public $data;

    public function __construct($data)
        $this->data = $data;

    public function build()
        $address = '[email protected]';
        $subject = 'This is a demo!';
        $name = 'John Smith';
        return $this->view('emails.test')
                    ->from($address, $name)
                    ->cc($address, $name)
                    ->bcc($address, $name)
                    ->replyTo($address, $name)
                    ->with([ 'message' => $this->data['message'] ]);

Create a view

In Laravel, Views are used as 'templates' when sending an email. Create a file under app/resources/views/emails/test.blade.php and insert this code:

<!DOCTYPE html>
<html lang="en-US">
    <meta charset="utf-8">
    <h2>Test Email</h2>
    <p>{{ $message }}</p>

Send an email

To send an email, run the following code:

use App\Mail\MyTestEmail;

$data = ['message' => 'This is a test!'];

Mail::to('[email protected]')->send(new MyTestEmail($data));

What’s Next