Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lumen 5.3 send email

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();
like image 271
Jayaram Venkat Avatar asked Nov 21 '16 19:11

Jayaram Venkat


People also ask

How do I send an email in Laravel?

Laravel provides a clean, simple email API powered by the popular Symfony Mailer component. Laravel and Symfony Mailer provide drivers for sending email via SMTP, Mailgun, Postmark, Amazon SES, and sendmail , allowing you to quickly get started sending mail through a local or cloud based service of your choice.

Is Lumen good for API?

Why build a REST API in Lumen. Lumen is blazing fast. It can handle more requests per second than Laravel. It is a faster version for creating a full web application framework.


2 Answers

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.

like image 93
GTCrais Avatar answered Oct 18 '22 06:10

GTCrais


If php 7.1 is installed, use Mail 5.7 version

composer require illuminate/mail 5.7.*
like image 1
Hemamalini Avatar answered Oct 18 '22 06:10

Hemamalini