从流明发送电子邮件

时间:2017-05-09 07:19:56

标签: laravel email lumen

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

.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;
    }

不幸的是,这不起作用。谁能告诉我哪里出错了?

1 个答案:

答案 0 :(得分:0)

在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);
                });
相关问题