【问题标题】:Sending mail to multiple recipients in smtp sendmail在 smtp sendmail 中向多个收件人发送邮件
【发布时间】:2015-07-28 20:05:10
【问题描述】:

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

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' 时。请帮忙。

【问题讨论】:

    标签: php smtp sendmail


    【解决方案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;
    }
    

    但是,当您与多个收件人打交道时,您应该避免以您的方式传递“To”标头。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 2015-10-21
      • 1970-01-01
      • 2020-09-15
      相关资源
      最近更新 更多