简体   繁体   English

PHPMailer SMTP连接错误

[英]PHPMailer SMTP connection error

I've been trying to figure out how to get phpMailer to work for the last few hours. 我一直在试图弄清楚如何让phpMailer在最近几个小时内工作。 I keep getting errors. 我一直在收到错误。 Here is my code: 这是我的代码:

<?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";
}
?>

Here is the error I have been receiving: 这是我收到的错误:

SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed. SMTP错误:无法连接到服务器:(0)SMTP连接()失败。 Mailer Error: SMTP connect() failed. 邮件程序错误:SMTP连接()失败。

I have tried many different SMTP servers including Gmail and I always get a similar error about failing to connect. 我尝试了许多不同的SMTP服务器,包括Gmail,我总是得到一个关于无法连接的类似错误。

Please help, I have tried many other phpMailer example codes all with the same issue. 请帮助,我已经尝试了许多其他phpMailer示例代码都具有相同的问题。 If anyone can recommend any other PHP mailing programs that would be useful. 如果有人可以推荐任何其他有用的PHP邮件程序。 The reason I am not using the inbuilt mail() function. 我没有使用内置mail()函数的原因。

You didn't set the SMTP Host ( though you declare the variable $host ). 您没有设置SMTP主机(尽管您声明了变量$host )。 Set it via: 设置通过:

$mail->host = $host;

Thanks @Rikesh for reminding, $port is also the same case. 谢谢@Rikesh的提醒, $port也是一样的情况。

Side Note: I noticed you use gmail.com as reply email but your SMTP server is not gmail. 旁注:我注意到你使用gmail.com作为回复邮件,但你的SMTP服务器不是gmail。 This may cause some email servers to put your email to spam / junk folder. 这可能会导致某些电子邮件服务器将您的电子邮件放入垃圾邮件/垃圾文件夹

Try this by adding : 尝试添加以下内容:

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

Refer this link PHP Mailer help 请参阅此链接PHP Mailer帮助

Try to change the port number 尝试更改端口号

$port = 587;

to

$port = 465;

and check what new error you are getting , i am sure you will not get SMTP connect error again. 并检查你得到了什么新的错误,我相信你不会再次收到SMTP连接错误。

  1. Use SSL: 使用SSL:
$mail->SMTPSecure = 'ssl';                // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                        // ssl
  1. Add this in your php file: 在你的php文件中添加:
$mail->SMTPOptions = array(
'ssl' => array(
    'verify_peer' => false,
    'verify_peer_name' => false,
    'allow_self_signed' => true
    )
);
  1. In myaccount.gmail.com > Sign-in & security > Apps with account access set Allow less secure apps to ON . 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