Laravel Mail::send() 发送到多个 to 或 bcc 地址 [英] Laravel Mail::send() sending to multiple to or bcc addresses

查看:41
本文介绍了Laravel Mail::send() 发送到多个 to 或 bcc 地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Laravel 的 Mail::send() 回调时,我似乎无法成功发送到多个地址,但是当我只指定 时,代码确实有效>一个收件人.

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.

我试过链接:

// 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);
        }
});

并传递一个数组:

// 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);
});

但似乎两者都不起作用,而且我在返回 Mail::failures() 时收到失败消息,Mail::failures() 的 var_dump() 显示了我尝试发送到的电子邮件地址,例如:

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:

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

显然做错了什么,希望得到任何帮助,因为我也不了解 API:http://laravel.com/api/4.2/Illuminate/Mail/Message.html#method_to

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

我意识到我可以将 Mail::send() 方法放在 for/foreach 循环中,并将 Mail::send() 放在每个电子邮件地址中,但是这个在我看来,这不是最佳解决方案,我希望一旦一切正常,我也可以 ->bcc() 到所有地址,这样收件人就看不到还有谁邮件正在发送到.

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.

推荐答案

我已经使用以下代码对其进行了测试:

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.

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

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.

此外,使用这个简单的代码,我的电子邮件都没有发送到免费邮件帐户,我只收到了我付费托管帐户上的收件箱的电子邮件,因此可能它们被某些过滤器捕获了(这可能是简单的主题/内容问题,但我提到它只是为了防止您没有收到一些电子邮件).

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

这篇关于Laravel Mail::send() 发送到多个 to 或 bcc 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆