简体   繁体   English

如何在laravel5.5中发送多封电子邮件

[英]How to send multiple emails in laravel5.5

I want to send multiple emails. 我想发送多封电子邮件。 I am using below code to send multiple emails:- 我使用下面的代码发送多封电子邮件: -

$emails = ['ab.in@gmail.com', 'ka.in@gmail.com'];

    Mail::send('emails.email-marketing-template', [], function($message) use ($emails)
    {    
        $message->to($emails)->subject('This is test e-mail');    
    });
    var_dump( Mail:: failures());
    exit;

If i send it to single user, it is working, but not for multiple users. 如果我将其发送给单个用户,它可以正常工作,但不适用于多个用户。 How should i send email to multiple user? 我该如何向多个用户发送电子邮件?

From the 5.5 docs : 5.5文档

The to method accepts an email address, a user instance, or a collection of users . to方法接受电子邮件地址,用户实例或用户集合 If you pass an object or collection of objects, the mailer will automatically use their email and name properties when setting the email recipients, so make sure these attributes are available on your objects. 如果传递对象或对象集合,邮件程序将在设置电子邮件收件人时自动使用其emailname属性,因此请确保这些属性在对象上可用。

So, you can do this: 所以,你可以这样做:

$users = User::get();
Mail::to($users)->send(new OrderShipped($order));

Or you can pass an array with name and email keys: 或者您可以传递带有nameemail密钥的数组:

$users = [
    ['name' => 'John', 'email' => 'john@gmail.com'],
    ['name' => 'Jane', 'email' => 'jane@gmail.com'],
    ['name' => 'Max', 'email' => 'max@gmail.com'],
];

I prefer sending emails from Laravel and Symfony by queue/spool. 我更喜欢通过queue / spool从Laravel和Symfony发送电子邮件。 Read more about queueing emails in Laravel . 阅读有关在Laravel中排队电子邮件的更多信息。

Queue emails are better than sending messages immediately because main process which supports the request is not waiting for email sending process. 队列电子邮件比立即发送邮件更好,因为支持请求的主进程不等待电子邮件发送过程。

If you use Mail:queue you can send emails in the loop to many recipients 如果您使用Mail:queue ,则可以将循环中的电子邮件发送给许多收件人

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

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