繁体   English   中英

PHPMailer SMTP连接错误

[英]PHPMailer SMTP connection error

我一直在试图弄清楚如何让phpMailer在最近几个小时内工作。 我一直在收到错误。 这是我的代码:

<?php
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

require("class.phpmailer.php");
$mail = new PHPMailer(); 
$mail->IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "******@gmail.com"; // SMTP username
$mail->Password = "******"; // SMTP password
$host = "smtp.mandrillapp.com";
$port = 587;
$mail->SMTPSecure = 'ssl';  
$mail->SMTPDebug = 1;
$webmaster_email = "****@gmail.com"; //Reply to this email ID
$email= $email; // Recipients email ID
$name= $name; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "University of Life Experiences";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "ULE";
$mail->Body = $message; //HTML Body
$mail->AltBody = $message; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

这是我收到的错误:

SMTP错误:无法连接到服务器:(0)SMTP连接()失败。 邮件程序错误:SMTP连接()失败。

我尝试了许多不同的SMTP服务器,包括Gmail,我总是得到一个关于无法连接的类似错误。

请帮助,我已经尝试了许多其他phpMailer示例代码都具有相同的问题。 如果有人可以推荐任何其他有用的PHP邮件程序。 我没有使用内置mail()函数的原因。

您没有设置SMTP主机(尽管您声明了变量$host )。 设置通过:

$mail->host = $host;

谢谢@Rikesh的提醒, $port也是一样的情况。

旁注:我注意到你使用gmail.com作为回复邮件,但你的SMTP服务器不是gmail。 这可能会导致某些电子邮件服务器将您的电子邮件放入垃圾邮件/垃圾文件夹

尝试添加以下内容:

$mail->Mailer   = "SMTP"; // SMTP Method

请参阅此链接PHP Mailer帮助

尝试更改端口号

$port = 587;

$port = 465;

并检查你得到了什么新的错误,我相信你不会再次收到SMTP连接错误。

  1. 使用SSL:
$mail->SMTPSecure = 'ssl';                // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                        // ssl
  1. 在你的php文件中添加:
$mail->SMTPOptions = array(
'ssl' => array(
    'verify_peer' => false,
    'verify_peer_name' => false,
    'allow_self_signed' => true
    )
);
  1. myaccount.gmail.com > Sign-in & security > Apps with account access Allow less secure apps设置Allow less secure apps ON

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM