Lumen 邮箱推送

获取邮箱授权码

找到qq邮箱中的设置 -> 账户 -> POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务,

如果是已开启状态,关闭重新打开就好了,拿到授权码

安装依赖包

如果框架已经下好依赖查看composer.json(框架所用到的依赖)文件是否有

 如果没有  就在这里添加上

!!!!  注意上边的是框架版本号下载的mail版本不要大于框架版本号(坑一)


        "illuminate/mail": "~5.6",

 添加上之后,执行命令 composer up ,  等待mail安装

框架没有composer.json   直接 composer require illuminate/mail  进行安装

配置文件

lumen框架安装好mail之后,目录中并不会有mail.php文件。这里需要我们手动做一个

放在config目录下

 这是mail文件内容,直接无脑粘贴

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Mail Driver
    |--------------------------------------------------------------------------
    |
    | Laravel supports both SMTP and PHP's "mail" function as drivers for the
    | sending of e-mail. You may specify which one you're using throughout
    | your application here. By default, Laravel is setup for SMTP mail.
    |
    | Supported: "smtp", "sendmail", "mailgun", "mandrill", "ses",
    |            "sparkpost", "log", "array"
    |
    */

    'driver' => env('MAIL_DRIVER', 'smtp'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Host Address
    |--------------------------------------------------------------------------
    |
    | Here you may provide the host address of the SMTP server used by your
    | applications. A default option is provided that is compatible with
    | the Mailgun mail service which will provide reliable deliveries.
    |
    */

    'host' => env('MAIL_HOST'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Host Port
    |--------------------------------------------------------------------------
    |
    | This is the SMTP port used by your application to deliver e-mails to
    | users of the application. Like the host we have set this value to
    | stay compatible with the Mailgun e-mail application by default.
    |
    */

    'port' => env('MAIL_PORT'),

    /*
    |--------------------------------------------------------------------------
    | Global "From" Address
    |--------------------------------------------------------------------------
    |
    | You may wish for all e-mails sent by your application to be sent from
    | the same address. Here, you may specify a name and address that is
    | used globally for all e-mails that are sent by your application.
    |
    */

    'from' => [
        'address' => env('MAIL_FROM_ADDRESS'),
        'name' => env('MAIL_FROM_NAME', 'Example'),
    ],

    /*
    |--------------------------------------------------------------------------
    | E-Mail Encryption Protocol
    |--------------------------------------------------------------------------
    |
    | Here you may specify the encryption protocol that should be used when
    | the application send e-mail messages. A sensible default using the
    | transport layer security protocol should provide great security.
    |
    */

    'encryption' => env('MAIL_ENCRYPTION', 'null'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Server Username
    |--------------------------------------------------------------------------
    |
    | If your SMTP server requires a username for authentication, you should
    | set it here. This will get used to authenticate with your server on
    | connection. You may also set the "password" value below this one.
    |
    */

    'username' => env('MAIL_USERNAME'),

    'password' => env('MAIL_PASSWORD'),

    /*
    |--------------------------------------------------------------------------
    | Sendmail System Path
    |--------------------------------------------------------------------------
    |
    | When using the "sendmail" driver to send e-mails, we will need to know
    | the path to where Sendmail lives on this server. A default path has
    | been provided here, which will work well on most of your systems.
    |
    */

    'sendmail' => '/usr/sbin/sendmail -bs',

    /*
    |--------------------------------------------------------------------------
    | Markdown Mail Settings
    |--------------------------------------------------------------------------
    |
    | If you are using Markdown based email rendering, you may configure your
    | theme and component paths here, allowing you to customize the design
    | of the emails. Or, you may simply stick with the Laravel defaults!
    |
    */

    'markdown' => [
        'theme' => 'default',

        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],

];

然后再env文件中配置一下

MAIL_DRIVER=smtp

MAIL_HOST=smtp.qq.com

MAIL_PORT=25  // 这里是你得端口号 建议刚开始写25,普通方式,425需要MAIL_ENCRYPTION进行加密

MAIL_USERNAME=********@qq.com   //  刚刚登录的邮箱

MAIL_PASSWORD=**********   //  把第一步获取到授权码放这里

MAIL_ENCRYPTION=null  // 加密方式  刚接触建议使用null  熟悉流程

MAIL_FROM_ADDRESS=*******@qq.com  // 发件人的邮箱

MAIL_FROM_NAME=发件人的名称

调用mail

在 bootstrap\app.php 中添加

$app->configure('mail');  // 调用到mail依赖
$app->singleton('mailer', function () use ($app) {  // 注册中间商,不写这会有问题
    return $app->loadComponent('mail', Illuminate\Mail\MailServiceProvider::class, 'mailer');
});

在控制器中写方法开始测试

注意:!!!!

不要用php artisan make:command 去生成控制器  Lumen对这个不友好需要配置很多东西

自己copy我给的代码就好

在路由文件中加上路由

// 邮箱测试
$router->get('mail/send','Api\MailController@send');

注意这里我是写在Controllers下的Api目录下的,这里根据自己的项目结构来

创建MailController.php 文件。写控制器

<?php
namespace App\Http\Controllers\Api;
use Laravel\Lumen\Routing\Controller;
use Illuminate\Http\Request;
use \App\Http\Services\Api\FaceService;
use Illuminate\Support\Facades\Mail;
class MailController extends Controller
{
    public function send() {
        $name = '我发的第一份邮件'; 
        // Mail::send()的返回值为空,所以可以其他方法进行判断 
        Mail::send('emails.test',['name'=>$name],function($message){ 
        $to = '******@qq.com'; $message ->to($to)->subject('邮件测试'); 
        }); 
        //  $to 是你要发送的邮箱地址
        // 返回的一个错误数组,利用此可以判断是否发送成功
         dd(Mail::failures());
       } 
}

写完控制请后需要一份邮箱内容模板

在resources\views\创建emails目录,创建test.php

内容随便写一下

访问路由测试,能成功发送邮件自己多看看这些配置都代表什么意思

如果不能成功参考一下

laravel 邮箱教程以及可能出现的问题

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值