PHPMailer SMTP connection error

扶醉桌前 提交于 2019-12-10 18:12:36

问题


I've been trying to figure out how to get phpMailer to work for the last few hours. 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. Mailer Error: SMTP connect() failed.

I have tried many different SMTP servers including Gmail and I always get a similar error about failing to connect.

Please help, I have tried many other phpMailer example codes all with the same issue. If anyone can recommend any other PHP mailing programs that would be useful. The reason I am not using the inbuilt mail() function.


回答1:


You didn't set the SMTP Host ( though you declare the variable $host ). Set it via:

$mail->host = $host;

Thanks @Rikesh for reminding, $port is also the same case.

Side Note: I noticed you use gmail.com as reply email but your SMTP server is not gmail. This may cause some email servers to put your email to spam / junk folder.




回答2:


Try this by adding :

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

Refer this link PHP Mailer help




回答3:


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.




回答4:


  1. Use SSL:
$mail->SMTPSecure = 'ssl';                // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                        // ssl
  1. Add this in your php file:
$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.


来源:https://stackoverflow.com/questions/21088062/phpmailer-smtp-connection-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!