Sending Mail

Updated on Jul 11, 2024

It is very easy to send mail with a Laravel application. Of course, before doing anything else, you must set up your mail driver and transport as per the instructions in the previous section of this tutorial. If you have not yet done that, please do so. 

With the mail driver and transport setup complete, we must first create a route, which will allow us to send mail when we visit the URL. Earlier in this tutorial, we mentioned that routes are defined in either the web.php or api.php files. For our purposes, we will use the former since we are not using an API. Add this route to web.php.

Route::get('/send-mail', [SendMailController::class, 'index']);

As you can see, we are defining the /send-mail URL, as well as the SendMailController controller, which we have not created yet. That is easy enough with this command.

php artisan make:controller SendMailController

By default, this is what the controller will look like.

However, let's make it look a bit more like an email. You can paste this code directly into the file. We are using the mailable we created earlier.

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\SampleMail;
class SendMailController extends Controller
{
    public function index()
    {
        $content = [
            'subject' => 'FastComet Laravel Test Email',
            'body' => 'Hello! Does this work?'
        ];
        Mail::to('[email protected]')->send(new FastCometInvoice($content));
        return "Email has been sent.";
    }
}

As you can see, we have specified the FastCometInvoice mailable within this controller. Since we already have a mailable ready, we needn't create one, but you can test it with different ones if you wish. If you remember, we even added a view to the mailable we also created earlier: invoice.unpaid.sent. For your convenience, this is what the whole mailable class looks like.

<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class FastCometInvoice extends Mailable
{
    use Queueable, SerializesModels;
    /**
     * Create a new message instance.
     */
    public function __construct()
    {
        //
    }
    /**
 * Get the message envelope.
 */
public function envelope(): Envelope
{
    return new Envelope(
        from: new Address('[email protected]', 'Billing Team'),
        subject: 'Fast Comet Invoice',
    );
}
    /**
 * Get the message content definition.
 */
public function content(): Content
{
    return new Content(
        view: 'invoice.unpaid.sent',
    );
}
    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [];
    }
}

All that is left is to test our code. Firstly, start the application.

php artisan serve

If everything has gone according to plan, when we visit the /send-mail URL of our application, we should receive the email message we defined in the controller earlier. One last detail is that if you would like to add "cc" and "bcc" fields to your email controller, you can do so like this.

Mail::to($request->user())
    ->cc($moreUsers)
    ->bcc($evenMoreUsers)
    ->send(new FastCometInvoice));

Feel free to experiment with sending emails. It should be straightforward to set up!

On this page...