laravel5 使用rabbitmq發送郵件小案例.window,linux環境不同配置

這是我的線上lnmp環境,按照命令一步一步來。接下來主要說一下wnmp環境安裝,略微麻煩。

1.下載Erlang.rabbirmq的依賴環境

官方下載地址官網:http://www.erlang.org/downloads

2.下載RabbitMQ Server

官方下載地址官網:http://www.rabbitmq.com/download.html,我是windows,直接下載安裝

3.安裝成功後,打開rabbitmq命令行。
添加web管理插件 :rabbitmq-plugins enable rabbitmq_management

4.瀏覽器輸入 http://127.0.0.1:15672

   默認登錄用戶:guest 密碼:guest

5. laravel項目安裝composer命令行

composer require vladimir-yuldashev/laravel-queue-rabbitmq

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

VladimirYuldashev\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider::class,

7.在 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),
            ],

        ],

8.配置env文件

QUEUE_CONNECTION=rabbitmq
RABBITMQ_HOST=127.0.0.1
RABBITMQ_PORT=5672
RABBITMQ_VHOST=/
RABBITMQ_LOGIN=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_QUEUE=blog

9.創建一個隊列任務: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()]);
        }

    }
}

10.創建一個控制器將隊列任務寫進去

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

11.配置郵箱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

12.執行命令:

php artisan queue:work
如果修改隊列任務代碼,需要:PHP artisan cache:clear

13.通過路由訪問,試試郵件發送效果:

最後注意:PHP需要開啓ampq擴展。如果有不懂的歡迎評論交流

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章