问题
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:
- Use SSL:
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // ssl
- Add this in your php file:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
- In
myaccount.gmail.com
>Sign-in & security
>Apps with account access
setAllow less secure apps
toON
.
来源:https://stackoverflow.com/questions/21088062/phpmailer-smtp-connection-error