Menu Close

Enable Mail on Lumen Framework

lumen

Since version 5.2, a lot of functionality was cut down from Lumen PHP micro-framework in an effort to make improvements in speed and a shift towards a stateless API service builder. However, the good news is that you can still utilize a lot of Laravel’s underlying power in Lumen without essentially including all the unnecessary overhead, by installing only the required Laravel components. In this post, I intend to discuss how to import the Laravel Mail component and email functionality to a Lumen application.

Import the Laravel Mail component

Run the following composer command on the terminal from your Lumen project folder

composer require illuminate/mail

Configuration for Mail

Copy the default configuration file for mail from Laravel’s official repository to your application’s config folder. Create the folder if it does not exist. You can use the file as it is or change it to suit your needs.

Open your application’s .env file and provide your mail server configuration like the sample shown below.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=587
MAIL_USERNAME=my_email_address
MAIL_PASSWORD=my_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=my_email_address
MAIL_FROM_NAME="Firstname Lastname"

Register the Mail Facade

Now open bootstrap/app.php file and uncomment the following lines:

$app->withFacades();

Note: The withFacades() method registers the following Facades in your Lumen app: Auth, Cache, DB, Event, Gate, Log, Queue, Route, Schema, URL, Validator. But you can load additional Facades by providing optional parameters to the method as shown below.

$app->withFacades(true, [
    'Illuminate\Support\Facades\Mail' => 'Mail',
]);

Then, under the “Register Service Providers” section, add

$app->configure('mail');
$app->register(Illuminate\Mail\MailServiceProvider::class);

Writing Mailables

At this point, your application is ready to send emails, you can do that just as you would in a Laravel application. Create a Mailable class in your app/Mail folder.

<?php
namespace App\Mail;

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

class MyEmail extends Mailable {

    use Queueable,
        SerializesModels;

    //build the message.
    public function build() {
        return $this->view('my-email');
    }
}

Now create a View called my-email.blade.php in your resources/views folder.

<p>Hello recipient,</p>
<p>This is a sample email. Please do not reply!</p>
<p>
Regards,<br>
Sender.
</p>

Now you can send this email from your controller.

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Mail;
use App\Mail\MyEmail;

class MyController extends Controller {
    //send email
    public function sendEmail() {
        Mail::to('somebody@example.org')->send(new MyEmail());
    }
}

Conclusion

Hope you enjoyed reading this article. If you have any comments, please use the comment option below. Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *