Laravel 在不使用 foreach 循环的情况下发送单独的多封邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26864374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:24:44  来源:igfitidea点击:

Laravel sending separate, multiple mail without using foreach loop

phpemaillaravellaravel-4swiftmailer

提问by Ronser

I am using Mailfunction in laravel under the SwiftMailerlibrary.

我在SwiftMailer库下的 laravel 中使用Mail函数。

Mail::send('mail', array('key' => $todos1), function($message) {
        $message->to(array('[email protected]','[email protected]','[email protected]','[email protected]'))->subject('Welcome!');
    });

The above function sends mail to several user, but the users know to who are all the mail is sent as its to address comprises of

上述函数向多个用户发送邮件,但用户知道所有邮件发送给谁,因为其收件人地址包括

To: [email protected], [email protected], [email protected], [email protected]

So inorder to rectify this I have used a foreachloop which sends the mails seperatly

所以为了解决这个问题,我使用了一个foreach单独发送邮件的循环

    foreach($to as $receipt){
        //Mail::queue('mail', array('key' => $todos1), function($message) use ($receipt)
        Mail::send('mail', array('key' => $todos1), function($message) use ($receipt)
        {
            $message->to($receipt)->subject('Welcome!');
        });
    }   

The above code works fine...

上面的代码工作正常...

My question is that in this advanced framework is there any function that could send mails to the users with unique toaddress(i.e.) without one user knowing to how-many others the same mail is sent without using a foreach...

我的问题是,在这个高级框架中,是否有任何功能可以向具有唯一to地址的用户发送邮件(即),而一个用户不知道在不使用foreach...的情况下向其他多少人发送相同的邮件。

回答by Steve

You can use bcc (blind carbon copy):

您可以使用 bcc(密件抄送):

Mail::send('mail', array('key' => $todos1), function($message) {
    $message->to('[email protected]')
    ->bcc(array('[email protected]','[email protected]','[email protected]','[email protected]'))
    ->subject('Welcome!');
});

回答by Muruga

You can use CC or BCC to send same html mail to N number of persons:

您可以使用 CC 或 BCC 向 N 个人发送相同的 html 邮件:

$content = '<h1>Hi there!</h1><h2 style="color:red">Welcome to stackoverflow..</h2>';
     $bcc = ['*****@gmail.com','******@gmail.com'];
     $sub = "Sample mail";
      Mail::send([], [], function($message) use ($content, $sub, $bcc) {
        $message->from('[email protected]','name');
        $message->replyTo('[email protected]', $name = 'no-reply');
        $message->to('******@domain.com', 'name')->subject($sub);
        $message->bcc($bcc, $name = null);
        // $message->attach('ch.pdf'); // if u need attachment
        $message->setBody($content, 'text/html');
      });

回答by Nabil Kadimi

SwiftMailer works like your normal email client (Outlook, Thunderbird...).

SwiftMailer 的工作方式与您普通的电子邮件客户端(Outlook、Thunderbird...)类似。

What you are doing is the only 100% correct to do it, but you can still do as Steve suggested, use BCC, but don't use a noreply or other non-important email address in the to, since all recipients will see that email address.

你正在做的是唯一 100% 正确的,但你仍然可以按照史蒂夫的建议去做,使用密件抄送,但不要在 to 中使用 noreply 或其他不重要的电子邮件地址,因为所有收件人都会看到电子邮件地址。

Note: The single function call will not make your code way faster or less resource hungry.

注意:单个函数调用不会使您的代码更快或更少的资源消耗。