Laravel5.8 使用 rabbitmq 傳送郵件小案例.window,Linux 環境不同配置

程式碼的壞味道發表於2019-07-05

先看下效果,這是我的線上lnmp環境,按照命令一步一步來。
接下來主要說一下wnmp環境安裝,略微麻煩。
Laravel5.8 使用 rabbitmq 傳送郵件小案例.window,Linux 環境不同配置

官方下載地址官網:http://www.erlang.org/downloads
官方下載地址官網:http://www.rabbitmq.com/download.html,我是windows,直接下載安裝
新增web管理外掛 :rabbitmq-plugins enable rabbitmq_management

如下即安裝成功:

Laravel5.8 使用 rabbitmq 傳送郵件小案例.window,Linux 環境不同配置

預設登入使用者:guest 密碼:guest
Laravel5.8 使用 rabbitmq 傳送郵件小案例.window,Linux 環境不同配置

composer require vladimir-yuldashev/laravel-queue-rabbitmq
VladimirYuldashev\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider::class,
 '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),
            ],

        ],
QUEUE_CONNECTION=rabbitmq
RABBITMQ_HOST=127.0.0.1
RABBITMQ_PORT=5672
RABBITMQ_VHOST=/
RABBITMQ_LOGIN=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_QUEUE=blog
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()]);
        }

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

}
MAIL_DRIVER=smtp
MAIL_HOST=smtp.qq.com//使用qq郵箱服務
MAIL_PORT=587
MAIL_FROM_ADDRESS=1606548133@qq.com
MAIL_USERNAME=1606548133@qq.com
MAIL_PASSWORD=xxxxxxx郵箱授權密碼
MAIL_FROM=1606548133@qq.com
MAIL_NAME=cfun
php artisan queue:work
如果修改佇列任務程式碼,需要:PHP artisan cache:clear

Laravel5.8 使用 rabbitmq 傳送郵件小案例.window,Linux 環境不同配置

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

相關文章