在Laravel中创建电子邮件队列

时间:2017-09-25 05:31:27

标签: laravel laravel-5 laravel-5.4

Newsletter.php

<?php

namespace App\Mail;
use App\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Http\Request;

class Newsletter extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */

    public $order;

    public function __construct($order)
    {
        $this->order = $order;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('mail', $this->order)->subject($this->order['subject']);
    }
}

SendReminderEmail.php

<?php

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 Mail;
use App\Mail\Newsletter;
use App\User;

class SendReminderEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */

    public $order, $email;

    public function __construct($order, $emails)
    {
        $this->order = $order;
        $this->email = $emails;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //for($i=0;$i<count($this->email);$i++)
        Mail::to($this->email)->queue(new Newsletter($this->order));
    }
}

控制器

public function sendSubMail(Request $request){
    $data = ['text' => $request->input('message'), 'subject' => $request->input('subject')];
    $emails = DB::table('emails')->get();
    for ($i=0; $i < count($emails); $i++) { 
        try {
            Mail::to($emails[$i])->queue(new Newsletter($data));
            //dispatch((new Job)->onQueue('high'))
            dispatch(new SendReminderEmail($data, $emails[$i]));
        } catch (Exception $e) {
        }
    }
    return view('emailSent', ['sub' => 'Emails successfully sent']);
}

任何人都可以解释如何在laravel中创建电子邮件队列。我尝试了很多方法,但似乎都没有。

我正在尝试使用队列发送电子邮件。我必须在请求中发送超过1500个。但我实施的队列似乎没有用。请帮忙。 提前致谢

1 个答案:

答案 0 :(得分:0)

使用Cron发送邮件一种方法