php rabbitmq 邮件发送,Laravel 使用 rabbitmq 异步发送邮件小案例.

一. rabbitmq环境配置

1.下载Erlang.rabbirmq的依赖环境

window官方下载地址官网:http://www.erlang.org/downloads

linux环境:rpm -Uvh http://www.rabbitmq.com/releases/erlang/erlang-18.1-1.el7.centos.x86_64.rpm

2.下载RabbitMQ Server

window官方下载地址官网:http://www.rabbitmq.com/download.html,我是windows,直接下载安装

linux环境: rpm -Uvh http://www.rabbitmq.com/releases/rabbitmq-server/v3.5.6/rabbitmq-server-3.5.6-1.noarch.rpm

3.安装成功后,window打开rabbitmq 命令行。

linux:进入rabbitmq安装目录,../sbin

添加web管理端

rabbitmq-plugins enable rabbitmq_management

几个常用命令:

service rabbitmq-server start

service rabbitmq-server restart

service rabbitmq-server stop

rabbitmqctl status

如下即安装成功:

php rabbitmq 邮件发送,Laravel 使用 rabbitmq 异步发送邮件小案例._第1张图片

4.浏览器输入 http://127.0.0.1:15672

默认登录用户:guest 密码:guest

php rabbitmq 邮件发送,Laravel 使用 rabbitmq 异步发送邮件小案例._第2张图片

二. laravel结合rabbitmq配置

1.laravel项目安装composer命令行

composer require vladimir-yuldashev/laravel-queue-rabbitmq

2.在 config/app.php 文件中,providers 中添加:

VladimirYuldashev\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider::class,

3.在 app/config/queue.php 配置文件中的 connections 数组中加入以下配置

'rabbitmq' => [

'driver' => 'rabbitmq',

'dsn' => env('RABBITMQ_DSN', null),

'factory_class' => Enqueue\AmqpLib\AmqpConnectionFactory::class,

'host' => env('RABBITMQ_HOST', '127.0.0.1'),

'port' => env('RABBITMQ_PORT', 5672),

'vhost' => env('RABBITMQ_VHOST', '/'),

'login' => env('RABBITMQ_LOGIN', 'guest'),

'password' => env('RABBITMQ_PASSWORD', 'guest'),

'queue' => env('RABBITMQ_QUEUE', 'default'),

'options' => [

'exchange' => [

'name' => env('RABBITMQ_EXCHANGE_NAME'),

/*

* Determine if exchange should be created if it does not exist.

*/

'declare' => env('RABBITMQ_EXCHANGE_DECLARE', true),

/*

* Read more about possible values at https://www.rabbitmq.com/tutorials/amqp-concepts.html

*/

'type' => env('RABBITMQ_EXCHANGE_TYPE', \Interop\Amqp\AmqpTopic::TYPE_DIRECT),

'passive' => env('RABBITMQ_EXCHANGE_PASSIVE', false),

'durable' => env('RABBITMQ_EXCHANGE_DURABLE', true),

'auto_delete' => env('RABBITMQ_EXCHANGE_AUTODELETE', false),

'arguments' => env('RABBITMQ_EXCHANGE_ARGUMENTS'),

],

'queue' => [

/*

* Determine if queue should be created if it does not exist.

*/

'declare' => env('RABBITMQ_QUEUE_DECLARE', true),

/*

* Determine if queue should be binded to the exchange created.

*/

'bind' => env('RABBITMQ_QUEUE_DECLARE_BIND', true),

/*

* Read more about possible values at https://www.rabbitmq.com/tutorials/amqp-concepts.html

*/

'passive' => env('RABBITMQ_QUEUE_PASSIVE', false),

'durable' => env('RABBITMQ_QUEUE_DURABLE', true),

'exclusive' => env('RABBITMQ_QUEUE_EXCLUSIVE', false),

'auto_delete' => env('RABBITMQ_QUEUE_AUTODELETE', false),

'arguments' => env('RABBITMQ_QUEUE_ARGUMENTS'),

],

],

/*

* Determine the number of seconds to sleep if there's an error communicating with rabbitmq

* If set to false, it'll throw an exception rather than doing the sleep for X seconds.

*/

'sleep_on_error' => env('RABBITMQ_ERROR_SLEEP', 5),

/*

* Optional SSL params if an SSL connection is used

* Using an SSL connection will also require to configure your RabbitMQ to enable SSL. More details can be founds here: https://www.rabbitmq.com/ssl.html

*/

'ssl_params' => [

'ssl_on' => env('RABBITMQ_SSL', false),

'cafile' => env('RABBITMQ_SSL_CAFILE', null),

'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),

'local_key' => env('RABBITMQ_SSL_LOCALKEY', null),

'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),

'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),

],

],

5.配置env文件

QUEUE_CONNECTION=rabbitmq

RABBITMQ_HOST=127.0.0.1

RABBITMQ_PORT=5672

RABBITMQ_VHOST=/

RABBITMQ_LOGIN=guest

RABBITMQ_PASSWORD=guest

RABBITMQ_QUEUE=blog

6.创建一个队列任务:php artisan make:job Queue

namespace App\Jobs;

use Illuminate\Bus\Queueable;

use Illuminate\Queue\SerializesModels;

use Illuminate\Queue\InteractsWithQueue;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Foundation\Bus\Dispatchable;

use Illuminate\Support\Facades\Mail;

use Illuminate\Support\Facades\Redis;

class Queue implements ShouldQueue

{

use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $email;

/**

* Create a new job instance.

*

* @return void

*/

public function __construct($email)

{

//

$this->email=$email;

}

/**

* Execute the job.

*

* @return void

*/

public function handle()

{

try{

$rand_num=$this->random(6,0);

Redis::set($this->email,$rand_num);

Mail::raw($rand_num, function ($message) {

$message ->to($this->email)->subject('cfun博客邮箱验证');

});

echo json_encode(['code'=>200,'msg'=>'Success']);

}catch (\Exception $exception) {

echo json_encode(['code'=>0,'msg'=>$exception->getMessage()]);

}

}

}

7.创建一个控制器将队列任务写进去

namespace App\Http\Controllers;

use App\Http\Requests\SendEmailRequest;

use App\Jobs\Queue;

class IndexController extends Controller

{

public function sendEmail(SendEmailRequest $request)

{

$email=$request->email;

//注册发送邮件

$this->dispatch(new Queue($email));

}

}

8.配置邮箱env服务

MAIL_DRIVER=smtp

MAIL_HOST=smtp.qq.com//使用qq邮箱服务

MAIL_PORT=587

[email protected]

[email protected]

MAIL_PASSWORD=xxxxxxx邮箱授权密码

[email protected]

MAIL_NAME=cfun

三. 执行命令,消费队列:

如果是线上可以配置supervisor管理,有兴趣的同学自己配置看看.

php artisan queue:work

如果修改队列任务代码,需要:PHP artisan cache:clear

四. 通过路由访问,效果如下:

本地访问:

php rabbitmq 邮件发送,Laravel 使用 rabbitmq 异步发送邮件小案例._第3张图片

线上linux环境访问:

php rabbitmq 邮件发送,Laravel 使用 rabbitmq 异步发送邮件小案例._第4张图片

最后注意:PHP需要开启ampq扩展。如果有不懂的欢迎评论交流

本作品采用《CC 协议》,转载必须注明作者和本文链接

cfun

你可能感兴趣的:(php,rabbitmq,邮件发送)