简体   繁体   English

如何使用 Laravel 和 Mailgun 群发电子邮件

[英]How to mass email with Laravel & Mailgun

My mission is to mass emails the users of my site.我的任务是向我网站的用户群发电子邮件。

I want to take the emails of all the users of my site, and send them a message, aka bulk email.我想接收我网站所有用户的电子邮件,并向他们发送消息,也就是批量电子邮件。

I'm using mailgun service at the moment to send confirmation emails when a user signs up to my site.我目前正在使用 mailgun 服务在用户注册我的网站时发送确认电子邮件。 below is an example of some of the code I'm using.下面是我正在使用的一些代码的示例。

I want to know if I could use a similar code to send bulk email.我想知道是否可以使用类似的代码来发送批量电子邮件。

public function sendEmail($sub)
{
    $user = $this;
    Mail::send('mail.confirm',['user' => $user, $sub => $sub], function($mail) use ($user,$sub) {
     $mail->from('website@gmail.com', 'Website');
     $mail->to($user->email, $user->name)->subject($sub . ' Confirm Website Email');
    });
}

any ideas?有什么想法吗?

I'd suggest you to use Queues for this stuffs.我建议你使用队列来处理这些东西。

Make a queue file like below:制作一个队列文件,如下所示:

class SendEmail extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;
    public  $data , $email;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data,$email)
    {
        $this->data=$data;
        $this->email=$email;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

        Mail::send('mail.confirm', [
                    'title' => $this->data['body'] ,
                    'body' => $this->data['body'],
                    ,
                ], function ($message) {
                    $message->from('no-reply@testmail.com', 'Test Notification');
                    $message->to($this->email)->subject($this->data['subject']));
                }
                );


    }

}

Call it from the controller:从控制器调用它:

use App\Jobs\SendEmail;
class EmailController extends Controller {

    public function send() {
        $data = array
            (
            'title' => 'title',
            'body' => 'body',
            'subject' => 'subject'
        );

        $emails=array("email1@email.com","email2@email.com")
        foreach($emails as $val){
            $this->dispatch(new SendEmail($data, $val));
        }


}

Please check https://laravel.com/docs/5.4/queues and https://scotch.io/tutorials/why-laravel-queues-are-awesome请检查https://laravel.com/docs/5.4/queueshttps://scotch.io/tutorials/why-laravel-queues-are-awesome

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM