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();
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.
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.
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.
If php 7.1 is installed, use Mail 5.7 version
composer require illuminate/mail 5.7.*
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With