Trushar Narodia
Trushar Narodia

Reputation: 366

smtp connect failed in smtp mail

i have a issue sending mail using smtp mail class when i use my send email code using smtp in my local system at that time is working prefect but when i try that code in live server i got every time smtp connect failed error

i use this code for send mail using smtp mailer class

require 'mail/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); 
$mail->SMTPDebug = 2; 
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;  
$mail->Username = '[email protected]'; 
$mail->Password = 'mypassword';        
$mail->SMTPSecure = 'tls';             
$mail->From = '[email protected]';
$mail->FromName = 'Mailer';
$mail->addAddress('[email protected]', 'Joe User');
$mail->addAddress('[email protected]'); 
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
$mail->WordWrap = 50; 
$mail->isHTML(true); 
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';}

that is my code it working in local but not working in live server

i did all setting in my php.ini file like smtp and user password but still not working so please give me proper solution or code that working in live server

this is error

2016-07-07 17:23:09 SMTP ERROR: Failed to connect to server: Network is unreachable (101) 2016-07-07 17:23:09 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Upvotes: 2

Views: 1222

Answers (3)

Mouaad BELLOUCHE
Mouaad BELLOUCHE

Reputation: 1

Use real email for sender and receiver 'Make sure of the password'

  1. change

$mail->SMTPSecure = 'tls';

to

$mail->SMTPSecure = 'ssl';

   //tls for localhost and ssl for host server 'onligne'
  1. add port

$mail->Port = 465 ; // or 587

may work for you :)

Upvotes: 0

Ulrich Dohou
Ulrich Dohou

Reputation: 1559

You must first of all activate gmail accepting non secure apps

Upvotes: 0

Trushar Narodia
Trushar Narodia

Reputation: 3873

when you are in local at that time smtp.gmail.com its working but in live you need your server host address like mail.servername.com

Upvotes: 1

Related Questions