首页 > 解决方案 > 如果在laravel中TO地址为空,如何发送邮件

问题描述

我正在尝试使用抄送地址而不是收件人地址发送邮件。

我的代码如下。

$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');
                }
            });

我正在尝试上面的代码并得到以下错误。

local.ERROR:给定 [] 邮箱中的地址不符合 RFC 2822, 3.6.2。

标签: phplaravelemail

解决方案


您可以尝试使用数组参数。

$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');
   }
});

推荐阅读