- 修改 .evn 文件中的 QUEUE_CONNECTION=default 设置为 database
- 修改config目录下面的queue.php文
<?php return [ /* |-------------------------------------------------------------------------- | Default Queue Connection Name |-------------------------------------------------------------------------- | | Laravel's queue API supports an assortment of back-ends via a single | API, giving you convenient access to each back-end using the same | syntax for every one. Here you may define a default connection. | */ 'default' => env('QUEUE_CONNECTION', 'sync'), /* |-------------------------------------------------------------------------- | Queue Connections |-------------------------------------------------------------------------- | | Here you may configure the connection information for each server that | is used by your application. A default configuration has been added | for each back-end shipped with Laravel. You are free to add more. | | Drivers: "sync", "database", "beanstalkd", "sqs", "redis", "null" | */ 'connections' => [ 'sync' => [ 'driver' => 'sync', ], 'database' => [ 'driver' => 'database', 'table' => 'jobs', //根据你使用的表进行更改 'queue' => 'default', 'retry_after' => 90, ], 'sqs' => [ 'driver' => 'sqs', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'), 'queue' => env('SQS_QUEUE', 'your-queue-name'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), ], 'redis' => [ 'driver' => 'redis', 'connection' => 'default', 'queue' => env('REDIS_QUEUE', 'default'), 'retry_after' => 90, 'block_for' => null, ], ], /* |-------------------------------------------------------------------------- | Failed Queue Jobs |-------------------------------------------------------------------------- | | These options configure the behavior of failed queue job logging so you | can control which database and table are used to store the jobs that | have failed. You may change them to any database / table you wish. | */ 'failed' => [ 'database' => env('DB_CONNECTION', 'mysql'), 'table' => 'failed_jobs', ], ];
- 使用artisan命令生成任务文件 php artisan make:job SendEmail
<?php namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use PHPMailer\PHPMailer\PHPMailer; class SendEmail implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * Create a new job instance. * * @return void */ public function __construct() { //这里可以传递参数 } /** * Execute the job. * * @return void */ public function handle() { $mail = new PHPMailer(true); try { $mail->CharSet = "UTF-8"; $mail->isSMTP(); // Send using SMTP $mail->Host = 'smtp.qiye.163.com'; // Set the SMTP server to send through $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = '******'; // SMTP username $mail->Password = '*****'; // SMTP password $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted $mail->Port = 587; // TCP port to connect to //Recipients $mail->setFrom('******', '*****');//发送名称 $mail->addAddress('*****'); // 发送给给定的邮箱 $mail->addCC('*****');//抄送给相对应的邮箱 // 这个方法是发送附件内容的 $mail->addAttachment('C:/Users/Administrator/Desktop/图片资源/OK.jpg'); // 添加附件文件 // $mail->addAttachment('C:/Users/Administrator/Desktop/图片资源/OK.jpg', 'new.jpg'); // 重命名附件文件名 $mail->isHTML(true); // Set email format to HTML $mail->Subject = '这是一份测试邮件';//邮件标题 $mail->Body = 'This is the HTML message body <b>in bold!</b>';//邮件主体内容 // $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; $mail->send(); echo "邮件发送成功"; Log::info('邮件发送成功'); } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; Log::info('邮件发送失败'); } } }
-
生成队列任务计划表
php artisan queue:table php artisan queue:failed-table php artisan migrate
5.在需要调用队列的方法下面加入该类即可
$data = ['company'=>'****'];
$job=new SendEmail($data);
dispatch($job);
return ['code'=>1,'msg'=>'发送成功'];
6.在windows中 在项目根目录打开控制台分别 执行
php artisan queue:listen
php artisan queue:work --queue=default,mytask --tries=2