我正在尝试使用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
我猜连接本身并没有建立起来...但是在第一个实例中出现了什么呢?
答案 0 :(得分:0)
该函数是否在while循环中,如果是,则需要关闭该类。可能很容易尝试这个。
$result = $mail->Send();
$mail->close();
答案 1 :(得分:0)
你不能一次发送邮件实例,例如你调用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();
}
}