Rens's avatar
Level 1

Using Mailable and views in Lumen

I'd like to use the Mailable class from Illuminate\Mail in a Lumen project but it relies on views.

I sort of understand why views are no longer supported in Lumen, but then what is the recommended approach to dealing with sending mail (for example, user registration confirmations) from an API? Localization support would be a pro for example, and to me seems easiest using views.

Could anybody point me in the right direction?

0 likes
8 replies
NickeyGod's avatar

Well the thing is that. Lumen is a framework which you should use for building simple api's not sending out mails or other stuff. Why not just stick to laravel and just add the code you need. You can disable unneeded features in your /config/app.php providers group.

Rens's avatar
Level 1

What are then considered simple APIs? I wouldn't say sending some simple e-mails really adds complication or am I wrong here

NickeyGod's avatar

You are not wrong its just that lumen does not support it.

envision's avatar

It seems if you need view or email templates or sessions, then you should choose Laravel instead of Lumen.

matthew.erskine's avatar

You can indeed use Lumen for sending out emails. I don't think its accurate at all to claim it's "only for building simple APIs...."

Here's how to get it working

composer require illuminate/mail

If you'd like to use mailable templating, I'd recommend bringing in this package to publish vendor assets:

composer require laravelista/lumen-vendor-publish

Next, let's set up the services...

bootstrap/app.php

$app->withFacades();

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

You'll have to publish the mail vendor if you'd like to use mailables:

php artisan vendor:publish

Now you're good to go! Define your mailable view as normal...

<?php

namespace App\Mail;

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

class TestMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('mail.test');
    }
}

And finally, here's an example route of your mail in action:

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

$router->get('/mail', function() {
    Mail::to(['[email protected]'])->send(new TestMail);

    return new TestMail;
});
3 likes
mindwar's avatar

I've also tried using mailables with lumen. followed the above instruction and im getting this error

#24 {main} {"exception":"[object] (Illuminate\Contracts\Container\BindingResolutionException(code: 0): Target [Illuminate\Contracts\Translation\Translator] is not instantiable. at /var/www/api.myplanner.ro/vendor/illuminate/container/Container.php:933)

what am i doing wrong?

mindwar's avatar

nevermind, adding this to app/Providers/AppServiceProvider.php solved it

    public function register()
    {
        $this->app->bind('Illuminate\Contracts\Translation\Translator', function ($app) {
                return $app['translator'];
        });
        //
    }
2 likes
dele's avatar

If you are using Notifications. You will need to add this line to the boostrap file as well for Lumen

$app->alias('mailer', \Illuminate\Contracts\Mail\Mailer::class);

Please or to participate in this conversation.