简体   繁体   English

即使cc和bcc为空,Laravel Mail也会发送

[英]Laravel Mail send even if cc and bcc is null

I have mail send function in laravel 我在laravel有邮件发送功能

public static function Compose($to,$cc,$bcc,$subject,$body)
    {

        // return $to;
        try
        {
            $data = [
                'body' => $body
            ];

            if(env('APP_ENV') == "local") 
            {
                $email["subject"] = $subject;
                $email["to"] = $to;
                $email["cc"] = $cc;
                $email["bcc"] = $bcc;
                Mail::send('email.composeMail', $data, function ($message) use ($email) {

                    $message
                        ->subject($email["subject"])
                        ->to($email["to"]);
                        ->cc($email["cc"]);
                        ->bcc($email["bcc"]);
                });
            }
            else
            {
                $email["subject"] = $subject;
                $email["to"] = $to;
                $email["cc"] = $cc;
                $email["bcc"] = $bcc;

                Mail::send('email.composeMail', $data, function ($message) use ($email) {

                    $message
                        ->subject($email["subject"])
                        ->to($email["to"]);
                        ->cc($email["cc"]);
                        ->bcc($email["bcc"]);
                });
            }
        } 
        catch (\Exception $e) 
        {
            Log::critical('Critical error occurred upon processing the transaction in Email.php -> Email class -> RevertPropertyToInbox method');
            throw new CustomErrorHandler($e->getMessage(),Constant::LogLevelCritical);
        }
    }

In many cases CC and BCC is Null. 在许多情况下,CC和BCC是空的。 But mails aren't sent and I am getting error message 但是邮件没有发送,我收到错误消息

在此输入图像描述

Here , I want to use code as it is without checking if CC or BCC is null, Is there any thing missed by me so that I can achieve what I am planning to . 在这里,我想使用代码,而不检查CC或BCC是否为空,是否有任何我错过的东西,以便我可以实现我的计划。

Those methods can all be called with an array instead of a plain string ( docs ). 这些方法都可以用数组而不是普通字符串( docs )来调用。 In that case, you should be able to just leave the array empty. 在这种情况下,您应该能够将数组留空。 Try this: 试试这个:

$message
    ->subject($email["subject"])
    ->to($email["to"]);
    ->cc($email["cc"] ?: []);
    ->bcc($email["bcc"] ?: []);

you cannot send email if the email address is blank, it will always throw error 如果电子邮件地址为空,则无法发送电子邮件,它将始终出错

instead you need to check and then send email accordingly 相反,你需要检查,然后相应地发送电子邮件

try this 试试这个

if($email["cc"] =='' && $email["bcc"] == ''){
 $message
                        ->subject($email["subject"])
                        ->to($email["to"]);
}
elseif($email["cc"] ==''){
$message
                        ->subject($email["subject"])
                        ->to($email["to"])
                        ->bcc($email["bcc"]);
}
else{

$message
                        ->subject($email["subject"])
                        ->to($email["to"])
                        ->cc($email["cc"]);

}

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

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