发送邮件给多个收件人的SMTP的sendmail
问题描述:
我使用下面的代码来发送邮件,发送邮件给多个收件人的SMTP的sendmail
public function sendMail($mailTo, $subject) {
//$mailTo is an array like array([email protected],[email protected])
$mailto = implode(', ', $mailTo);
$subject = empty($subject) ? 'Testing Manuel Lemos SMTP class' : $subject;
require("smtp.php");
/* Uncomment when using SASL authentication mechanisms */
require("sasl.php");
$from = "[email protected]"; /* Change this to your address like "[email protected]"; */ $sender_line = __LINE__;
$to = "$mailto";
if ($smtp->SendMessage(
$from,
array($to)
, array(
"From: $from",
"To: $to",
"Subject: TESTMAIL NOTIFICATION",
"Date: " . strftime("%a, %d %b %Y %H:%M:%S %Z")
), "Hello $to,$subject \n Bye .")) {
$str = "Message sent to $to OK.\n";
return $str;
} else
$str = "Could not send the message to $to.\nError: " . $smtp->error . "\n";
}
我在smtp.php SendMessage函数如下
Function SendMessage($sender,$recipients,$headers,$body)
{
if(($success=$this->Connect()))
{
if(($success=$this->MailFrom($sender)))
{
for($recipient=0;$recipient<count($recipients);$recipient++)
{
if(!($success=$this->SetRecipient($recipients[$recipient])))
break;
}
if($success
&& ($success=$this->StartData()))
{
for($header_data="",$header=0;$header<count($headers);$header++)
$header_data.=$headers[$header]."\r\n";
$success=($this->SendData($header_data."\r\n")
&& $this->SendData($this->PrepareData($body))
&& $this->EndSendingData());
}
}
$error=$this->error;
$disconnect_success=$this->Disconnect($success);
if($success)
$success=$disconnect_success;
else
$this->error=$error;
}
return($success);
}
,但它不是然而发送电子邮件,但它发送单个邮件。 当我们指定$为'[email protected]'。请帮忙 。
答
为什么你的收件人是implode()
? 似乎SendMessage
可以处理作为数组传递多个收件人:
$smtp->SendMessage('[email protected]', array('[email protected]', '[email protected]') ...);
在SendMessage
SetRecipient
被要求在$recipients
每个条目:
for($recipient=0;$recipient<count($recipients);$recipient++)
{
if(!($success=$this->SetRecipient($recipients[$recipient])))
break;
}
应避免穿过头“To”你的方式尽管如此,当你在处理多个收件人的时候。