Skip to content
Advertisement

How to send mail if TO address is blank in laravel

i am trying to send mail with CC address and without TO address.

My code is bellow.

$mail_param['msg'] = "demo";
            $mail_param['link'] = "xyz.com";
            $email = '';
            $name = "demo user";
            $cc = 'abc@gmail.com';

            Mail::send('view_template', $mail_param, function ($message) use ($email, $name, $cc) {
                if ($cc != '') {
                    $message->to($email, $name)->bcc($cc)->subject('Test Mail');
                } elseif ($email != '') {
                    $message->to($email, $name)->subject('Test Mail');
                }
            });

I am trying above code and get the bellow error.

local.ERROR: Address in mailbox given [] does not comply with RFC 2822, 3.6.2.

Advertisement

Answer

You Can try with array arguments.

$mail_param['msg'] = "demo";
$mail_param['link'] = "xyz.com";
$email = array();
$name = "demo user";
$cc = array('abc@gmail.com');

Mail::send('view_template', $mail_param, function ($message) use ($email, $name, $cc) 
{
   if (!empty($cc)) {
     $message->to($email, $name)->bcc($cc)->subject('Test Mail');
   } elseif (!empty($email)) {
     $message->to($email, $name)->subject('Test Mail');
   }
});
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement