简体   繁体   中英

phpmailer SMTP connect() failed on localhost

I'm running php5.6 on a localhost using xampp, and I'm trying to send a message using phpmailer, but it gives me this error : SMTP connect() failed. Here's my code:

require("Photo Gallery/includes/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();  
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Username = "aldemeery@gmail.com";
$mail->Password = "*********";
$mail->From     = "aldemeery@gmail.com";
$mail->AddAddress("aldemeery@gmail.com");
$mail->Subject  = "Subject";
$mail->Body     = "Hi!";
if(!$mail->Send()) {
    echo 'Message was not sent.';
    echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent.';
}

I know there are tens of questions out there having the same title, but believe me none of them worked for me.Please help, I'v been trying to solve this all day long!

OK...so i'v made some changes to php.ini, my gmail account, and my PHP script...and then everything worked just fine....so here is what i changed:

  • In php.ini: I removed the semicolon in front of "sendmail_from = postmaster@localhost" and restarted xampp.
  • In my gmail account: in the security settings , I turned on "Allow less secure apps" .
  • And finally in my PHP script: I added two functions:
    • "date_default_timezone_set('Etc/UTC')"
    • "gethostbyname('ssl://smtp.gmail.com')" as a value for " $mail->Host"

And here is my modified php script

date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = gethostbyname('ssl://smtp.gmail.com');
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "username@gmail.com";
$mail->Password = "***********";
$mail->setFrom('username@gmail.com', 'First Last');
$mail->addAddress('username@yahoo.com', 'First Last');
$mail->Subject = 'PHPMailer SMTP test';
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

check your antivirus apps, in my case "Avast Anti Virus" block it. then when i disabled it, the error gone.

Have you gone into your GMail account and enabled the feature to allow for less secure apps to access it? If not, check this out:

https://support.google.com/accounts/answer/6010255?hl=en

Alternatively, try this as your SMTP:

ssl://smtp.googlemail.com

I have found the above to be the most useful.

You could also try and include the file instead of require it, thus try changing,

require("Photo Gallery/includes/PHPMailer/PHPMailerAutoload.php");

include 'Photo Gallery/includes/PHPMailer/PHPMailerAutoload.php';

This might work. If nothing above works, try moving the entire library to the root folder of your project. It might be the space in your URL that is causing the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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