当 'cc' 为空时 Swiftmailer 抱怨
Swiftmailer complain when 'cc' is empty
我使用一个功能在两个不同的地方发送电子邮件。在一个地方我需要抄送另一个电子邮件地址,而在另一个地方则没有必要,所以我想将抄送默认设置为空,并在必要时插入一个电子邮件地址。
这是我的功能;
private function notifyByMail($to, $from, $toName="", $fromName="", $body, $subject, $cc="", $ccName="")
{
//use custom data to send notification email
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setTo(array($to => $toName))
->setCc(array($cc => $ccName))
->setFrom(array($from => $fromName))
->setBody($body);
$this->get('mailer')->send($message);
}
像这样在两个不同的地方调用它;
$this->notifyByMail($to, $from, '', $name, $body, $subject);
和
$this->notifyByMail($to, $from, 'Jack', $name, $body, $subject, $cc, 'Jack');
第二个有效,因为 cc 不为空,但第一个有问题。这就是错误的原因;
An exception has occured: Address in mailbox given [] does not comply
with RFC 2822, 3.6.2.
我已经删除了 cc 并再次测试第一个,然后它就可以正常工作了。我需要那个 cc,因为我在另一个地方使用它。有人可以告诉我为什么 cc 不能为空吗?我应该如何解决这个问题?
首先,cc不是null而是空字符串""
。
第二,为什么不使用简单的解决方案?
private function notifyByMail($to, $from, $toName="", $fromName="", $body, $subject, $cc="", $ccName="")
{
//use custom data to send notification email
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setTo(array($to => $toName))
->setFrom(array($from => $fromName))
->setBody($body);
if(!empty($cc)){
$message->setCc(array($cc => $ccName))
}
$this->get('mailer')->send($message);
}
我使用一个功能在两个不同的地方发送电子邮件。在一个地方我需要抄送另一个电子邮件地址,而在另一个地方则没有必要,所以我想将抄送默认设置为空,并在必要时插入一个电子邮件地址。
这是我的功能;
private function notifyByMail($to, $from, $toName="", $fromName="", $body, $subject, $cc="", $ccName="")
{
//use custom data to send notification email
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setTo(array($to => $toName))
->setCc(array($cc => $ccName))
->setFrom(array($from => $fromName))
->setBody($body);
$this->get('mailer')->send($message);
}
像这样在两个不同的地方调用它;
$this->notifyByMail($to, $from, '', $name, $body, $subject);
和
$this->notifyByMail($to, $from, 'Jack', $name, $body, $subject, $cc, 'Jack');
第二个有效,因为 cc 不为空,但第一个有问题。这就是错误的原因;
An exception has occured: Address in mailbox given [] does not comply with RFC 2822, 3.6.2.
我已经删除了 cc 并再次测试第一个,然后它就可以正常工作了。我需要那个 cc,因为我在另一个地方使用它。有人可以告诉我为什么 cc 不能为空吗?我应该如何解决这个问题?
首先,cc不是null而是空字符串""
。
第二,为什么不使用简单的解决方案?
private function notifyByMail($to, $from, $toName="", $fromName="", $body, $subject, $cc="", $ccName="")
{
//use custom data to send notification email
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setTo(array($to => $toName))
->setFrom(array($from => $fromName))
->setBody($body);
if(!empty($cc)){
$message->setCc(array($cc => $ccName))
}
$this->get('mailer')->send($message);
}