Laravel Mail :: send()发送到多个或bcc地址

时间:2022-10-23 18:08:56

I can't seem to successfully send to multiple addresses when using Laravel's Mail::send() callback, the code does however work when I only specify one recipient.

在使用Laravel的Mail :: send()回调时,我似乎无法成功发送到多个地址,但是当我只指定一个收件人时,代码可以正常工作。

I've tried chaining:

我试过链接:

// for example
$emails = array("myemail1@email.com", "myemail2@email.com");
$input = Input::all();

Mail::send('emails.admin-company', array('body' => Input::get('email_body')), 
function($message) use ($emails, $input) {
    $message
    ->from('admin@admin.org', 'Administrator')
    ->subject('Admin Subject');

        foreach ($emails as $email) {
            $message->to($email);
        }
});

and passing an array:

并传递一个数组:

// for example
$emails = array("myemail1@email.com", "myemail2@email.com");
$input = Input::all();

Mail::send('emails.admin-company', array('body' => Input::get('email_body')), 
    function($message) use ($emails, $input) {
        $message
        ->from('admin@admin.org', 'Administrator')
        ->subject('Admin Subject');

        $message->to($emails);
});

but neither seem to work and I get failure messages when returning Mail::failures(), a var_dump() of Mail::failures() shows the email addresses that I tried to send to, for example:

但似乎没有工作,我在返回Mail :: failures()时收到失败消息,Mail :: failures()的var_dump()显示我尝试发送到的电子邮件地址,例如:

array(2) {
  [0]=>
  string(18) "myemail1@email.com"
  [1]=>
  string(18) "myemail2@email.com"
}

Clearly doing something wrong, would appreciate any help as I'm not understanding the API either: http://laravel.com/api/4.2/Illuminate/Mail/Message.html#method_to

显然做错了什么,会感谢任何帮助,因为我不理解API:http://laravel.com/api/4.2/Illuminate/Mail/Message.html#method_to

I realise I could put the Mail::send() method in a for/foreach loop and Mail::send() for each email address, but this doesn't appear to me to be the optimal solution, I was hoping I would also be able to ->bcc() to all addresses once everything was working so the recipients wouldn't see who else the mail is being sent to.

我意识到我可以将Mail :: send()方法放在for / foreach循环中,并将Mail :: send()放入每个电子邮件地址,但这对我来说似乎不是最佳解决方案,我希望我能一旦一切正常工作,也可以 - > bcc()到所有地址,这样收件人就不会看到邮件被发送到的其他人。

6 个解决方案

#1


72  

I've tested it using the following code:

我使用以下代码测试了它:

$emails = ['myoneemail@esomething.com', 'myother@esomething.com','myother2@esomething.com'];

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

Result - empty array for failures.

结果 - 失败的空数组。

But of course you need to configure your app/config/mail.php properly. So first make sure you can send e-mail just to one user and then test your code with many users.

但是,您当然需要正确配置app / config / mail.php。因此,首先要确保您只能向一个用户发送电子邮件,然后与许多用户一起测试您的代码。

Moreover using this simple code none of my e-mails were delivered to free mail accounts, I got only emails to inboxes that I have on my paid hosting accounts, so probably they were caught by some filters (it's maybe simple topic/content issue but I mentioned it just in case you haven't received some of e-mails) .

此外,使用这个简单的代码我的电子邮件都没有发送到免费邮件帐户,我只收到了我收费主机帐户收件箱的电子邮件,所以可能是他们被一些过滤器捕获了(这可能是简单的主题/内容问题但是我提到它只是为了你没有收到一些电子邮件)。

#2


11  

If you want to send emails simultaneously to all the admins, you can do something like this:

如果您想同时向所有管理员发送电子邮件,您可以执行以下操作:

In your .env file add all the emails as comma separated values:

在.env文件中,将所有电子邮件添加为逗号分隔值:

ADMIN_EMAILS=admin1@site.com,admin2@site.com,admin3@site.com

so when you going to send the email just do this (yes! the 'to' method of message builder instance accepts an array):

所以当你要发送电子邮件时,就这样做(是的!消息构建器实例的'to'方法接受一个数组):

So,

所以,

$to = explode(',', env('ADMIN_EMAILS'));

and...

和...

$message->to($to);

will now send the mail to all the admins.

现在将邮件发送给所有管理员。

#3


9  

the accepted answer does not work any longer with laravel 5.3 because mailable tries to access ->email and results in

lalavel 5.3不再使用接受的答案因为mailable尝试访问 - >电子邮件并导致结果

ErrorException in Mailable.php line 376: Trying to get property of non-object

Mailable.php第376行中的ErrorException:尝试获取非对象的属性

a working code for laravel 5.3 is this:

laravel 5.3的工作代码是这样的:

$users_temp = explode(',', 'first@example.com,second@example.com');
    $users = [];
    foreach($users_temp as $key => $ut){
      $ua = [];
      $ua['email'] = $ut;
      $ua['name'] = 'test';
      $users[$key] = (object)$ua;
    }
 Mail::to($users)->send(new OrderAdminSendInvoice($o));

#4


3  

With Laravel 5.6, if you want pass multiple emails with names, you need to pass array of associative arrays. Example pushing multiple recipients into the $to array:

使用Laravel 5.6,如果要传递多个带有名称的电子邮件,则需要传递关联数组数组。将多个收件人推送到$ to数组的示例:

$to[] = array('email' => $email, 'name' => $name);

Fixed two recipients:

修复了两个收件人:

$to = [['email' => 'user.one@example.com', 'name' => 'User One'], 
       ['email' => 'user.two@example.com', 'name' => 'User Two']];

The 'name' key is not mandatory. You can set it to 'name' => NULL or do not add to the associative array, then only 'email' will be used.

“名称”键不是必需的。您可以将其设置为'name'=> NULL或不添加到关联数组,然后只使用'email'。

#5


0  

This works great - i have access to the request object and the email array

这很好用 - 我可以访问请求对象和电子邮件数组

        $emails = ['tester@blahdomain.com', 'anotheremail@blahdomian.com'];
        Mail::send('emails.lead', ['name' => $name, 'email' => $email, 'phone' => $phone], function ($message) use ($request, $emails)
        {
            $message->from('no-reply@yourdomain.com', 'Joe Smoe');
//            $message->to( $request->input('email') );
            $message->to( $emails);
            //Add a subject
            $message->subject("New Email From Your site");
        });

#6


-8  

it works for me fine, if you a have string, then simply explode it first.

它对我很有用,如果你有一个字符串,那么首先简单地爆炸它。

$emails = array();

$ emails = array();

Mail::send('emails.maintenance',$mail_params, function($message) use ($emails) {
    foreach ($emails as $email) {
        $message->to($email);
    }

    $message->subject('My Email');
});

#1


72  

I've tested it using the following code:

我使用以下代码测试了它:

$emails = ['myoneemail@esomething.com', 'myother@esomething.com','myother2@esomething.com'];

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

Result - empty array for failures.

结果 - 失败的空数组。

But of course you need to configure your app/config/mail.php properly. So first make sure you can send e-mail just to one user and then test your code with many users.

但是,您当然需要正确配置app / config / mail.php。因此,首先要确保您只能向一个用户发送电子邮件,然后与许多用户一起测试您的代码。

Moreover using this simple code none of my e-mails were delivered to free mail accounts, I got only emails to inboxes that I have on my paid hosting accounts, so probably they were caught by some filters (it's maybe simple topic/content issue but I mentioned it just in case you haven't received some of e-mails) .

此外,使用这个简单的代码我的电子邮件都没有发送到免费邮件帐户,我只收到了我收费主机帐户收件箱的电子邮件,所以可能是他们被一些过滤器捕获了(这可能是简单的主题/内容问题但是我提到它只是为了你没有收到一些电子邮件)。

#2


11  

If you want to send emails simultaneously to all the admins, you can do something like this:

如果您想同时向所有管理员发送电子邮件,您可以执行以下操作:

In your .env file add all the emails as comma separated values:

在.env文件中,将所有电子邮件添加为逗号分隔值:

ADMIN_EMAILS=admin1@site.com,admin2@site.com,admin3@site.com

so when you going to send the email just do this (yes! the 'to' method of message builder instance accepts an array):

所以当你要发送电子邮件时,就这样做(是的!消息构建器实例的'to'方法接受一个数组):

So,

所以,

$to = explode(',', env('ADMIN_EMAILS'));

and...

和...

$message->to($to);

will now send the mail to all the admins.

现在将邮件发送给所有管理员。

#3


9  

the accepted answer does not work any longer with laravel 5.3 because mailable tries to access ->email and results in

lalavel 5.3不再使用接受的答案因为mailable尝试访问 - >电子邮件并导致结果

ErrorException in Mailable.php line 376: Trying to get property of non-object

Mailable.php第376行中的ErrorException:尝试获取非对象的属性

a working code for laravel 5.3 is this:

laravel 5.3的工作代码是这样的:

$users_temp = explode(',', 'first@example.com,second@example.com');
    $users = [];
    foreach($users_temp as $key => $ut){
      $ua = [];
      $ua['email'] = $ut;
      $ua['name'] = 'test';
      $users[$key] = (object)$ua;
    }
 Mail::to($users)->send(new OrderAdminSendInvoice($o));

#4


3  

With Laravel 5.6, if you want pass multiple emails with names, you need to pass array of associative arrays. Example pushing multiple recipients into the $to array:

使用Laravel 5.6,如果要传递多个带有名称的电子邮件,则需要传递关联数组数组。将多个收件人推送到$ to数组的示例:

$to[] = array('email' => $email, 'name' => $name);

Fixed two recipients:

修复了两个收件人:

$to = [['email' => 'user.one@example.com', 'name' => 'User One'], 
       ['email' => 'user.two@example.com', 'name' => 'User Two']];

The 'name' key is not mandatory. You can set it to 'name' => NULL or do not add to the associative array, then only 'email' will be used.

“名称”键不是必需的。您可以将其设置为'name'=> NULL或不添加到关联数组,然后只使用'email'。

#5


0  

This works great - i have access to the request object and the email array

这很好用 - 我可以访问请求对象和电子邮件数组

        $emails = ['tester@blahdomain.com', 'anotheremail@blahdomian.com'];
        Mail::send('emails.lead', ['name' => $name, 'email' => $email, 'phone' => $phone], function ($message) use ($request, $emails)
        {
            $message->from('no-reply@yourdomain.com', 'Joe Smoe');
//            $message->to( $request->input('email') );
            $message->to( $emails);
            //Add a subject
            $message->subject("New Email From Your site");
        });

#6


-8  

it works for me fine, if you a have string, then simply explode it first.

它对我很有用,如果你有一个字符串,那么首先简单地爆炸它。

$emails = array();

$ emails = array();

Mail::send('emails.maintenance',$mail_params, function($message) use ($emails) {
    foreach ($emails as $email) {
        $message->to($email);
    }

    $message->subject('My Email');
});