laravel Lumen 5.3 发送邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40728207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lumen 5.3 send email
提问by Jayaram Venkat
I tried send a email from Lumen using gmail smtp config. I am using:
我尝试使用 gmail smtp 配置从 Lumen 发送电子邮件。我在用:
illuminate/mail
, Version5.3
lumen
, Version5.3
illuminate/mail
, 版本5.3
lumen
, 版本5.3
I can't send an email.
我无法发送电子邮件。
My router:
我的路由器:
$app->get('/', function () use ($app) {
$app->get('mail','mailcontroller@mail');
});
My AppServiceProvider.php
:
我的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:
我的.env
配置:
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.php
:
$app->register(App\Providers\AppServiceProvider::class);
$app->withFacades();
回答by GTCrais
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):
聚会有点晚了,但这是我在 Lumen 5.4 中的做法(我知道它可能有点笨拙,并不适合所有人,但仍然如此):
1)pull in illuminate/mail
:
1)拉入illuminate/mail
:
composer require illuminate/mail
composer require illuminate/mail
2)add the service provider to your bootstrap/app.php
:
2)将服务提供商添加到您的bootstrap/app.php
:
$app->register(\Illuminate\Mail\MailServiceProvider::class);
and uncomment $app->withFacades();
$app->register(\Illuminate\Mail\MailServiceProvider::class);
并取消注释 $app->withFacades();
It's possible/likely the following can be achieved through .env
but I haven't tried:
有可能/很可能可以通过以下方式实现,.env
但我还没有尝试过:
3)Install phanan's cascading config - https://github.com/phanan/cascading-configand follow the installation process for Lumen described there
3)安装 phanan 的级联配置 - https://github.com/phanan/cascading-config并按照那里描述的 Lumen 安装过程进行操作
4)create config
folder in your application's root and copy-paste full Laravel's config/mail.php
4)config
在应用程序的根目录中创建文件夹并复制粘贴完整的 Laravelconfig/mail.php
5)add $app->configure('mail');
to bootstrap/app.php
5)添加$app->configure('mail');
到bootstrap/app.php
6)make sure the actual config in mail.php
is correct
6)确保实际配置mail.php
正确
Now you should be able to send mails the same way you do in full Laravel installation.
现在你应该能够像在完整的 Laravel 安装中一样发送邮件。
回答by Hemamalini
If php 7.1 is installed, use Mail 5.7 version
如果安装了 php 7.1,请使用 Mail 5.7 版本
composer require illuminate/mail 5.7.*