【问题标题】:Send email from lumen从流明发送电子邮件
【发布时间】:2017-10-07 09:29:19
【问题描述】:

我使用vue.js 作为我的前端,使用 Lumen 作为我的 api 服务。现在我需要从 lumen 发送电子邮件。这就是我为此所做的。

.env 文件

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=********@gmail.com
MAIL_PASSWORD=**********
MAIL_FROM_ADDRESS=******@gmail.com
MAIL_FROM_NAME=Sample Email App
MAIL_ENCRYPTION=tls

然后编辑文件bootstrap\app.php 并取消注释以下行。

$app->register(App\Providers\AppServiceProvider::class);
$app->withFacades();
Dotenv::load(__DIR__.'/../');
$app->withEloquent();

在控制器中,我使用了以下代码

use Illuminate\Support\Facades\Mail;

private function sendActivationEmail( $email = null ){
        $email_sent = false;
        if( $email != null ){ 
            // send email
            Mail::raw('Raw string email', function($msg) { 
                $msg->to(['tismon@gmail.com']); $msg->from(['x@x.com']); 
            });

        }
        return $email_sent;
    }

很遗憾,这不起作用。谁能告诉我哪里出错了?

【问题讨论】:

    标签: laravel email lumen


    【解决方案1】:

    在 bootstrap/app.php 文件中注册MailServiceProvider

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

    尝试在注册函数中将以下代码添加到 AppServiceProvider.php

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

    如果还是不行,经过以上配置,试试邮件发送功能

    Mail::send('user.emails.registration' , $data, function($msg) use ($to,$from,$subject)
                    {
                        $msg->to($to)->from($from)->subject($subject);
                    });
    

    【讨论】:

    • 很遗憾,无法正常工作...[2017-10-20 18:55:25] lumen.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Class 'Illuminate\Mail\MailServiceProvider' not found in /api/vendor/laravel/lumen-framework/src/Application.php:164 Stack trace: #0 /api/bootstrap/app.php(267): Laravel\Lumen\Application->register('Illuminate\\Mail...')
    猜你喜欢
    • 2019-05-01
    • 2020-10-01
    • 2016-05-10
    • 2016-11-04
    • 2019-03-04
    • 2010-09-07
    • 2018-06-09
    • 2016-08-28
    • 2016-03-16
    相关资源
    最近更新 更多