Lumen 5.3 send email

Jayaram Venkat picture Jayaram Venkat · Nov 21, 2016 · Viewed 21.1k times · Source

I tried send a email from Lumen using gmail smtp config. I am using:

  • illuminate/mail, Version 5.3
  • lumen, Version 5.3

I can't send an email.

My router:

$app->get('/', function () use ($app) {
    $app->get('mail','mailcontroller@mail');
});

My AppServiceProvider.php:

namespace App\Providers;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider {

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

My .env configuration:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=*******@gmail.com
MAIL_PASSWORD=*********
MAIL_ENCRYPTION=tls

My mail controller:

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Mail;

class mailcontroller extends Controller {
    public function mail(){
        Mail::raw('Raw string email', function($msg) { 
            $msg->to(['****.com']); 
            $msg->from(['*****@gmail.com']); });
    }
}

Also i have enable following lines in app.php:

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

Answer

GTCrais picture GTCrais · Mar 6, 2017

A little late to the party, but here's how I've done it in Lumen 5.4 (and I know it might be a little clumsy and not suitable for everyone, but still):

1) pull in illuminate/mail:

composer require illuminate/mail

2) add the service provider to your bootstrap/app.php:

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

It's possible/likely the following can be achieved through .env but I haven't tried:

3) Install phanan's cascading config - https://github.com/phanan/cascading-config and follow the installation process for Lumen described there

4) create config folder in your application's root and copy-paste full Laravel's config/mail.php

5) add $app->configure('mail'); to bootstrap/app.php

6) make sure the actual config in mail.php is correct

Now you should be able to send mails the same way you do in full Laravel installation.