简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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