Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPMailer error: SMTP -> ERROR: Failed to connect to server

I try to google all the morning and i think i need Stackoverflow now!

I wrote a simple script to send a mail (from hotmail to gmail) but i get this error:

SMTP -> ERROR: Failed to connect to server: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (10060)SMTP Connect() failed. Error

This is the code:

<?php
require_once("../includes/phpMailer/class.phpMailer.php");
require_once("../includes/phpMailer/class.smtp.php");


$to_name = "RECEIVER NAME";
$to = "[email protected]";


$subject = "Mail test at " . strftime("%T", time());

$message = "This is a test message";
$message = wordwrap($message, 70);


$from_name = "MY NAME";
$from = "[email protected]";


$mail = new PHPMailer();

$mail->IsSMTP();
$mail->SMTPDebug  = 2;
$mail->Host = "smtp.live.com";
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "MY USERNAME (hotmail)";
$mail->Password = "MY PASSWORD (hotmail)";


$mail->FromName = $from_name;
$mail->From = $from;
$mail->AddAddress($to, $to_name);
$mail->Subject = $subject;
$mail->Body = $message;

$result = $mail->Send();

echo $result ? 'Sent' : 'Error';

?>

Another info is that not even the standard mail() function worked, and checking php info i found this:

sendmail_from - MY PROPER MAIL (hotmail)

sendmail_path - no value

SMTP - localhost

smtp_port - 25

Thank you!!

like image 311
FrancescoMussi Avatar asked Jul 16 '13 09:07

FrancescoMussi


2 Answers

I believe port 25 is blocked on smtp.live.com. I cannot connect to smtp.live.com:25 from here either. Try using port 587 instead, with TLS. So, it would be:

$mail->Port = 587;
$mail->SMTPSecure = 'tls';   
like image 126
mti2935 Avatar answered Oct 25 '22 11:10

mti2935


I found a solution for this problem, try this

Check whether your PHP is using openSSL extension or not...!

  1. Edit your php.ini from your installed php folder
  2. Search for extension=php_openssl.dll
  3. The initial will look like this ;extension=php_openssl.dll
  4. Remove the ';' and it will look like this extension=php_openssl.dll
  5. If you can't find the extension=php_openssl.dll, add this line extension=php_openssl.dll.
  6. Then restart your Xampp or LAMP or APACHE server (depends upon which of these you're using).

Hope this method shall solve your problem...

like image 40
Sohail xIN3N Avatar answered Oct 25 '22 11:10

Sohail xIN3N