Laravel/Lumen 5.4 发送邮件

栏目: PHP · 发布时间: 8年前

内容简介:Laravel/Lumen 5.4 发送邮件

这里使用163邮箱,个人邮箱需要开启smtp服务

Laravel/Lumen 5.4 发送邮件

当勾选设置POP3/SMTP/IMAP时提示需要设置授权码,需要跟登录密码区分开。

Laravel/Lumen 5.4 发送邮件

修改配置文件

编辑 .env 文件

MAIL_DRIVER=smtp
MAIL_HOST=smtp.163.com
MAIL_PORT=465
MAIL_USERNAME=test@163.com
MAIL_PASSWORD=******   //这里填写授权码
MAIL_FROM_ADDRESS=test@163.com
MAIL_FROM_NAME=test
MAIL_ENCRYPTION=ssl

如果是 Lumen 需要装mail模块

修改 composer.json 文件中 require 部分配置如下:

"require": {
        "php": ">=5.6.9",
        "laravel/lumen-framework": "5.4.*",
        "vlucas/phpdotenv": "~2.2",
        "guzzlehttp/guzzle": "^6.2",
        "predis/predis": "^1.1",
        "illuminate/redis": "^5.4",
        "illuminate/mail":"5.4.*"
    }

并运行 composer install 来安装 mail

创建发送邮件命令

如果是 laravel 直接执行命令,如果是 lumen 自己创建目录和文件

$ php artisan make:command sendMailCommand
Console command created successfully.

创建后生成此文件 app/Console/Commands/sendMailCommand.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;

class sendMailCommandextends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'mail:sendMailCommand';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '发送邮件命令';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $content = '这是一封来自 Laravel 的测试邮件.';
        $toMail  = 'cenhuqing@qq.com';

        Mail::raw($content, function ($message)use($toMail){
            $message->subject('[ 测试 ] 测试邮件SendMail - ' .date('Y-m-d H:i:s'));
            $message->to($toMail);
        });
    }
}

将命令加入到 app/Console/Kernel.php

protected $commands = [
        sendMailCommand::class
];

执行命令测试

$ php artisan |grep send*
  mail:sendMailCommand  发送邮件命令
$ php artisan mail:sendMailCommand                                             
  [Swift_TransportException]                                                                            
  Failed to authenticate on SMTP server with username "test@163.com" using 2 possible authenticators

注意: 上述执行命令报错,由于验证不通过导致此问题。跟代码没有关系。所以要检查下配置。我在这里找了很久,仍然没有发现错误,最后重置了下授权码后正常。不知道是啥问题。

Laravel/Lumen 5.4 发送邮件

问题: 我线上的版本是lumen,每次修改.env配置文件后不会生效,而是使用之前的配置。需要重启后才会生效新配置,不知道是什么原因,也没有配置缓存。如果哪位大神指导请告知下,我的邮箱cenhuqing@gmail.com。 谢谢!


以上所述就是小编给大家介绍的《Laravel/Lumen 5.4 发送邮件》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Uberland

Uberland

Alex Rosenblat / University of California Press / 2018-11-19 / GBP 21.00

Silicon Valley technology is transforming the way we work, and Uber is leading the charge. An American startup that promised to deliver entrepreneurship for the masses through its technology, Uber ins......一起来看看 《Uberland》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具