gpt4 book ai didi

php - Swiftmailer 在 'cc' 为空时提示

转载 作者:行者123 更新时间:2023-12-04 02:10:22 27 4
gpt4 key购买 nike

我使用一个函数在两个不同的地方发送电子邮件。在一个地方我需要抄送另一个电子邮件地址,而在另一个地方则没有必要,所以我会考虑将抄送默认设置为空,并在必要时插入一个电子邮件地址。

这是我的职责;

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

关于php - Swiftmailer 在 'cc' 为空时提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39165741/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com