使用Swiftmailer将多个地址加入BCC列表

5
我使用以下PHP代码向一个地址发送电子邮件并在密送2个其他地址。它成功地发送给收件人,但我只能让它发送到2个密送地址中的一个。(请参见代码中的注释,了解我尝试过什么)
奇怪的是,$result返回为3,因此似乎它正在尝试发送第二个密送电子邮件,但从未传递。
<?php


    $tracker='tracking@pnrbuilder.com';
    $subject = $_POST['subject'];
    $sender = $_POST['sender'];
    $toEmail=$_POST['toEmail'];
    $passedInEmail=stripslashes($_POST['message']);
    $passedInEmail=preg_replace('/&nbsp;/',' ',$passedInEmail);

    require_once('swiftLib/simple_html_dom.php');
    require_once('swiftLib/swift_required.php');
    $transport = Swift_MailTransport::newInstance();
    $mailer = Swift_Mailer::newInstance($transport);
    // Create the message
    $message = Swift_Message::newInstance();
    //turn the meesage into an object using simple_html_dom
    //so we can iterate through and embed each image
    $content = str_get_html($passedInEmail);

    // Retrieve all img src tags and replace them with embedded images
    foreach($content->find('img') as $e) 
        {
            if($e->src != "") 
                {
                    $value = $e->src;
                    $newValue = $message->embed(Swift_Image::fromPath($value)); 
                    $e->src = $newValue;
                }
        }

    $message->setSubject($subject);
    $message->setFrom($sender);
    $message->setTo($toEmail);



    //this is my problem
    $message->setBcc(array('tracking@pnrbuilder.com',$sender));
    //as it is above only "sender" gets the email

    //if I change it like this:

    //$message->setBcc($tracker,$sender);
    //only "tracker" gets the email


    //same if I change it like this:
    //$message->setBcc($sender);
//$message->addBcc($tracker);

    $message->setReplyTo(array('flights@pnrbuilder.com'));
    $message->setBody($content,'text/html');


    $result = $mailer->send($message);
    if ($result=3) {
        echo 'Email Sent!';
    } 
    else {
       echo 'Error!';
    }
?>

什么是正确的方法呢?

1
你使用AddBcc感到高兴吗? - allen213
1
尝试将pnrbuilder.com地址作为第二个电子邮件。也许是因为服务器配置的原因。 - Ziumin
@allen213 不是的,addBcc() 给出了相同的结果(已添加到我的问题上方)。 - Wesley Smith
@Ziumin 使用不同域名的电子邮件地址确实可以工作,并且两个都会发送,有什么办法可以让它在电子邮件相同时也能正常工作吗? - Wesley Smith
对于像我这样的其他人:一定要仔细检查,确保没有打错addBcc()而应该使用setBcc()。(在使用addBcc()之前,您必须至少通过setBcc设置第一个)。 - Hafenkranich
2个回答

9
您可以在此处找到SwiftMailer教程:这里 示例:
$message->setBcc(array(array('some@address.tld' => 'The Name'),array('another@address.tld' => 'Another Name')));

尝试为电子邮件地址设置名称,并查看是否有任何区别。

1
谢谢。在结尾处需要两个括号,必须是三个。 - manowar_manowar

1
这个问题实际上是服务器端的问题,我联系了我的托管服务提供商(GoDaddy),他们在自己的端口进行了一些更改,解决了这个问题。感谢所有试图帮助的人!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接