我使用以下PHP代码向一个地址发送电子邮件并在密送2个其他地址。它成功地发送给收件人,但我只能让它发送到2个密送地址中的一个。(请参见代码中的注释,了解我尝试过什么)
奇怪的是,$result返回为3,因此似乎它正在尝试发送第二个密送电子邮件,但从未传递。
什么是正确的方法呢?
奇怪的是,$result返回为3,因此似乎它正在尝试发送第二个密送电子邮件,但从未传递。
<?php
$tracker='tracking@pnrbuilder.com';
$subject = $_POST['subject'];
$sender = $_POST['sender'];
$toEmail=$_POST['toEmail'];
$passedInEmail=stripslashes($_POST['message']);
$passedInEmail=preg_replace('/ /',' ',$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!';
}
?>
什么是正确的方法呢?
addBcc()
而应该使用setBcc()
。(在使用addBcc()
之前,您必须至少通过setBcc
设置第一个)。 - Hafenkranich