phpmailer抛出间歇性错误:在检查是否连接时发现EOF


phpmailer throwing intermittent error : EOF found while checking if connected

我正在尝试使用PHPMailer类通过SMTP发送邮件。我的问题是,对于第一次尝试,邮件发送器工作正常,但对于所有后续尝试,它抛出错误:

SMTP -> NOTICE:
   EOF caught while checking if connected

我的邮件发送代码是:

function  sendEmail($toAddress,$fromAddress,$toName,$fromName,$subject,$emailContent,$content_type = false, $attach_path="", $cc = '', $cc_name="")
{
        require_once('phpmailer/class.phpmailer.php');
        if (empty($content_type)) $content_type = false;
        $mail = new PHPMailer();
        $mail->IsSMTP();            // set mailer to use SMTP
        $mail->SMTPAuth = MY_SMTP_AUTH;     // turn on SMTP authentication
        $mail->Host   = MY_SMTP_HOST_NAME;
        if (!empty($this->smtpEncryptionMode))
        {
                $mail->SMTPSecure= $this->smtpEncryptionMode;
        }
        if (!empty ($this->smtpPort))
        {
                $mail->Port = MY_SMTP_PORT;
        }
        else $mail->Port = 25;
        $mail->Username = $this->smtpUserName;
        $mail->Password = $this->smtpUserPassword;
        $mail->From     =$fromAddress;
        $mail->FromName = $fromName;
            if(is_array($toAddress))
                {
                        foreach($toAddress as $to)
                        {
                                $mail->AddAddress($to, "" );
                        }
                }
                else
                {
        $mail->AddAddress($toAddress, $toName );
                }
        $mail->AddReplyTo($fromAddress, $fromName );
        $mail->CharSet  = 'UTF-8';
        $mail->WordWrap = 80;  // set word wrap to 80 characters
        $mail->IsHTML($content_type);  // set email format to basic
        $mail->Subject = $subject;
        $mail->Body    = $emailContent;
        //Here it sets other parameters e.g attachment path etc.
        $mail->SMTPDebug = true;
        $result = $mail->Send();
        if($result == false ) { $result = $mail->ErrorInfo;echo $result; }// Switch   this on when debugging.
        return $result;

为什么对所有连续的尝试都抛出错误?

从,我可以从class.smtp.php中推断出来的是,它在函数Connected()中失败,该函数实际上检查smtp_connection实例的套接字状态,并且在那里它正在获得EOF。

我猜连接本身没有建立…但是在第一个例子中,什么是正确的呢?

是while循环中的函数,如果是,则需要关闭类。也许这样比较容易。

$result = $mail->Send(); 
$mail->close();

你不能一次发送邮件实例,例如你调用test_mail(),然后emailer()函数test_mail使用另一个连接,而另一个函数使用phpmailer并连接到端口465。现在emailer将抛出一个EOF错误,因为第一个函数的连接尚未退出。

/*function test_mail(){
    $to = 'emil.nrqz@gmail.com'; 
    $subject = 'Test email using PHP'; 
    $message = 'This is a test email message'; $headers = 'From: webmaster@example.com' . "'r'n" . 'Reply-To: webmaster@example.com' . "'r'n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers, '-fwebmaster@example.com'); 
}*/
function emailer($sendby,$subject,$body,$sendto,$cc){
    require("../obj/PHPMailer-master/class.phpmailer.php");
    $mail = new PHPMailer();                    // create a new object
    $mail->IsSMTP();                            // enable SMTP
    $mail->SMTPDebug = 1;                       // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;                     // authentication enabled
    $mail->Host = "relay-hosting.secureserver.net";
    $mail->Port = 25;                           // or 587 or 465
    /*
    $mail->SMTPAuth = true;                     // authentication enabled
    $mail->Host = "smtpout.secureserver.net";
    $mail->Port = 465;                      // or 587 or 465
    */
    //var_dump($mail);exit();
    $mail->IsHTML(true);                        // sends email as html
    $mail->Username = MAILER_USERNAME;  // mailserver username
    $mail->Password = MAILER_PASSWORD;              // mailserver password
    $mail->SetFrom("info@file-bird.com");                   // Seen as message from
    $mail->Subject = $subject;                  // Subject of the message
    $mail->Body = $body;                        // email body - can be html
    $mail->AddAddress($sendto);                 // where email will be sent
    $mail->addCC($cc);
    if(!$mail->Send()){
        return "Mailer Error: "; // . $mail->ErrorInfo
    }
    else{
        return "Message has been sent";
        $mail->close();
    }
}