2015-07-28 113 views
1

我使用下面的代碼來發送郵件,發送郵件給多個收件人的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]'。請幫忙 。

回答

1

爲什麼你的收件人是implode()? 似乎SendMessage可以處理作爲數組傳遞多個收件人:

$smtp->SendMessage('[email protected]', array('[email protected]', '[email protected]') ...); 

SendMessageSetRecipient被要求在$recipients每個條目:

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

應避免穿過頭「To」你的方式儘管如此,當你在處理多個收件人的時候。

相關問題