1
date_default_timezone_set('Etc/UTC');
require 'phpmailer/PHPMailerAutoload.php'; //Create a new PHPMailer     instance

$mail = new PHPMailer;

//Tell PHPMailer to use SMTP

$mail->IsSMTP();

//Enable SMTP debugging

// 0 = off (for production use)

// 1 = client messages

// 2 = client and server messages

$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output

$mail->Debugoutput = 'html';

//Set the hostname of the mail server

$mail->Host = 'smtp.gmail.com';

// use

// $mail->Host = gethostbyname('smtp.gmail.com');

// if your network does not support SMTP over IPv6

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission

$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls

$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication

$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail

$mail->Username = "username@gmail.com";

//Password to use for SMTP authentication

$mail->Password = "mypassword";

//Set who the message is to be sent from

$mail->setFrom('username@gmail.com', 'User');

//Set an alternative reply-to address


//Set who the message is to be sent to

$mail->addAddress($email,$name);

//Set the subject line

$mail->Subject = 'Subject';

//Read an HTML message body from an external file, convert referenced images to embedded,

//convert HTML into a basic plain-text alternative body

$body = "body";

$mail->msgHTML($body);

//Replace the plain text body with one created manually

$mail->AltBody = 'This is a plain-text message body';

//Attach an image file

//$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors

if (!$mail->send()) {

echo "Mailer Error: " . $mail->ErrorInfo;

} else {

echo "Message sent!";

I tried with ports 465 and 587. No access to php.ini file and also tried setting timeout limit. No go. openssl is enabled.

Checked with:

<?php echo (extension_loaded('openssl')?'SSL loaded':'SSL not loaded')."\n"; ?>

I always get the same error:

SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed. Mailer Error: SMTP connect() failed

Rao
  • 20,781
  • 11
  • 57
  • 77
  • If the error messages didn't include a link to [the troubleshooting guide](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting), you're running an old version, and you should read the guide anyway; a timeout suggests a network issue, probably outbound firewall blocking. – Synchro Jan 22 '16 at 19:54
  • I should mention probably that when I try it on my localhost, it works. But not on the hosting site. – KUNAL TOLANI Jan 23 '16 at 13:06
  • Well exactly - you're talking about two different networks, one of which has a problem. – Synchro Jan 23 '16 at 13:37
  • getting same problem but still could not find an answer – Ketan Lathiya Feb 11 '16 at 17:31

0 Answers0