在smtp sendmail中向多个收件人发送邮件

Sending mail to multiple recipients in smtp sendmail

我正在使用以下代码发送邮件,

public function sendMail($mailTo, $subject) {   
       //$mailTo is an array like  array(test1@gmail.com,test2@gmail.com)  
        $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 = "mlemos@acm.org";                           /* Change this to your address like "me@mydomain.com"; */ $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);
    }

但是它不是发送电子邮件,而是发送单封邮件。 当我们将 $to 指定为 'test@gmail.com' 时。请帮助。

你为什么implode()收件人? SendMessage 似乎可以处理作为数组传递的多个收件人:

$smtp->SendMessage('sender@example.tld', array('test@gmail.com', 'test2@gmail.com') ... );

SendMessageSetRecipient$recipients 中的每个条目调用:

for($recipient=0;$recipient<count($recipients);$recipient++)
{
  if(!($success=$this->SetRecipient($recipients[$recipient])))
    break;
}

不过,当您处理多个收件人时,您应该避免像现在这样传递 "To" header。