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

时间:2015-07-28 11:58:50

标签: php 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'时。请帮忙。

1 个答案:

答案 0 :(得分:1)

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

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

SendMessage中为SetRecipient中的每个条目调用$recipients for($recipient=0;$recipient<count($recipients);$recipient++) { if(!($success=$this->SetRecipient($recipients[$recipient]))) break; }

HTML

但是,当您处理多个收件人时,应该避免以您的方式传递“收件人”标题。

相关问题