Laravel Mail send even if cc and bcc is null
Asked Answered
R

2

5

I have mail send function in 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. But mails aren't sent and I am getting error message

enter image description here

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 .

Roue answered 3/7, 2017 at 11:27 Comment(2)
Why don't you want to check? It's the easiest thing in the world and a lot faster than asking for a solution that probably doesn't even existHydrometeor
In this case , I think if laravel have checked whether cc or bcc is blank then it would have ignored that directly without notifying developer , so I thought there might be something done by laravel where it checks that cc or bcc is blank and I am missing thatRoue
R
3

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"]);

}
Rubble answered 3/7, 2017 at 11:30 Comment(0)
E
10

Those methods can all be called with an array instead of a plain string (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"] ?: []);
Emmie answered 3/7, 2017 at 11:38 Comment(2)
problem is that if we leave array empty then error message is thrown, even for your code it is throwing same error messageRoue
I guess then you found a bug in SwiftMailer. You could try to report that setting cc/bcc to an empty array is impossible (which is undocumented).Emmie
R
3

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"]);

}
Rubble answered 3/7, 2017 at 11:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.