laravel 使用阿里云【邮件推送】服务发送邮件通知

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/running8/article/details/84312630

使用这个包:https://github.com/HyanCat/aliyun-direct-mail

1、安装:

composer require hyancat/aliyun-direct-mail:dev-master

2、在 config/services.php 中添加如下配置:

    'directmail' => [
        'app_key'    => env('DIRECT_MAIL_APP_KEY'),
        'app_secret' => env('DIRECT_MAIL_APP_SECRET'),
        'region'     => env('DIRECT_MAIL_REGION'),
        'account'    => [
            'alias' => env('DIRECT_MAIL_ACCOUNT_ALIAS'),
            'name' => env('DIRECT_MAIL_ACCOUNT_NAME'),
        ]
    ],

3、修改 config/app.php,在providers字段中添加:

'providers' => [
    ...
    HyanCat\DirectMail\AliyunDirectMailServiceProvider::class,
    ...
],

4、在.env中创建环境配置:

MAIL_DRIVER=directmail
DIRECT_MAIL_APP_KEY=          //_APP_KEY
DIRECT_MAIL_APP_SECRET=         //APP_SECRET
DIRECT_MAIL_REGION=             //REGION,比如:cn-hangzhou
DIRECT_MAIL_ACCOUNT_ALIAS=      //mail tag
DIRECT_MAIL_ACCOUNT_NAME=          //mail from

发个邮件试试:

5、建个邮件通知:

php artisan make:notification EmailTestNotification

app/Notifications/EmailTestNotification.php

.
.
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->greeting($notifiable->name.'您好:')
                    ->subject('邮件测试成功')
                    ->line('请点击以下链接')
                    ->action('访问网站首页', url('/'));
    }
.
.

创建控制器:

artisan make:controller MailTestController

需要是登录用户。

app/Http/Controllers/MailTestControll.php

<?php

namespace App\Http\Controllers;


use App\Notifications\EmailTestNotification;

use Auth;
class MailTestController extends Controller
{
  public function mailtest(){


//$user=$request->user();

      Auth::user()->notify(new EmailTestNotification());
      //$user->

  }
}

路由:

routes/web.php

Route::get('mailtest','MailTestController@mailtest');

当然,也可以省略掉控制器app/Http/Controllers/MailTestControll.php,直接把代码写在路由器里也是可以的:

routes/web.php:

<?php
use App\Notifications\MailTestNotification;

 Route::get('mailtest',function (){
     Auth::user()->notify(new MailTestNotification());
 });

猜你喜欢

转载自blog.csdn.net/running8/article/details/84312630