themirror's avatar

How to get Laravel Mailer in Lumen 5.4?

So I've been battling at this for a while now, deep into the framework and can't figure out why it won't instantiate.

I ran composer and installer Illuminate\Mail.

I registered the MailServiceProvider like so (in app.php):

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

I added a services and mail.php config file and added them via $app->configure.

I can get the Mailer class if I do:

app('mailer');

However, I can't get the Mailer class via typehinting, nor Facades. After digging for a while, it looks like it's to do with aliasing:

protected function registerContainerAliases()
{
    $this->aliases = [
        'Illuminate\Contracts\Foundation\Application' => 'app',
        'Illuminate\Contracts\Auth\Factory' => 'auth',
        'Illuminate\Contracts\Auth\Guard' => 'auth.driver',
        'Illuminate\Contracts\Cache\Factory' => 'cache',
        'Illuminate\Contracts\Cache\Repository' => 'cache.store',
        'Illuminate\Contracts\Config\Repository' => 'config',
        'Illuminate\Container\Container' => 'app',
        'Illuminate\Contracts\Container\Container' => 'app',
        'Illuminate\Database\ConnectionResolverInterface' => 'db',
        'Illuminate\Database\DatabaseManager' => 'db',
        'Illuminate\Contracts\Encryption\Encrypter' => 'encrypter',
        'Illuminate\Contracts\Events\Dispatcher' => 'events',
        'Illuminate\Contracts\Hashing\Hasher' => 'hash',
        'log' => 'Psr\Log\LoggerInterface',
        'Illuminate\Contracts\Queue\Factory' => 'queue',
        'Illuminate\Contracts\Queue\Queue' => 'queue.connection',
        'request' => 'Illuminate\Http\Request',
        'Laravel\Lumen\Routing\UrlGenerator' => 'url',
        'Illuminate\Contracts\Validation\Factory' => 'validator',
        'Illuminate\Contracts\View\Factory' => 'view',
    ];
}

Illuminate\Mail\Mailer is not there (it has no reason to be). So how do I add it so that the class will resolve, and where? I tried adding $app->alias('Illuminate\Mail\Mailer', 'mailer'); in app.php, however it got overridden during bootstrapping...

Ideas?

Thanks

0 likes
1 reply
afrayedknot's avatar

I had similar issues. I managed to get it working like this for Mailgun:

In bootstrap\app.php place this in the register service providers section:

$app->register(App\Providers\AppServiceProvider::class);

In your AppServiceProviders:

$this->app->configure('services');

$this->app->singleton('mailer', function ($app) {
    return $app->loadComponent('mail', 'Illuminate\Mail\MailServiceProvider', 'mailer');
});

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

Then you need to add a config folder in your base folder.

config\services.php:

<?php

return [
   'mailgun' => [
       'domain' => env('MAILGUN_DOMAIN'),
       'secret' => env('MAILGUN_SECRET'),
   ],
];

and config\mail.php:

<?php

return [

    'driver' => env('MAIL_DRIVER'),
    'host' => env('MAIL_HOST'),
    'port' => env('MAIL_PORT'),
    'from' => [
        'address' => env('MAIL_FROM_ADDRESS'),
        'name' => env('MAIL_FROM_NAME'),
    ],
    'encryption' => env('MAIL_ENCRYPTION'),
    'username' => env('MAIL_USERNAME'),
    'password' => env('MAIL_PASSWORD'),
    'markdown' => [
        'theme' => 'default',
        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],
];

This allowed me to use the Laravel Notifications and use the Mail option.

Please or to participate in this conversation.