2

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 = 'myemail@gmail.com'; 
$mail->Password = 'mypassword';        
$mail->SMTPSecure = 'tls';             
$mail->From = 'myemail@gmail.com';
$mail->FromName = 'Mailer';
$mail->addAddress('myemail@gmail.com', 'Joe User');
$mail->addAddress('myemail@gmail.com'); 
$mail->addReplyTo('myemail@gmail.com', 'Information');
$mail->addCC('myemail@gmail.com');
$mail->addBCC('myemail@gmail.com');
$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

Trushar Narodia
  • 366
  • 4
  • 19
  • the .ini settings don't matter. phpmailer isn't using them. you need to check if the gmail server is accessible from the machine you're testing this on. – Marc B Jul 07 '16 at 16:49
  • Add your phpmailer errorinfo to your question. – Progrock Jul 07 '16 at 17:11
  • See: http://stackoverflow.com/questions/3675897/how-to-check-by-php-if-my-script-is-connecting-to-smtp-server – Progrock Jul 07 '16 at 17:11
  • Do you not need to set the port? `$mail->Port=587`. – Progrock Jul 07 '16 at 17:37
  • See also: http://stackoverflow.com/questions/16048347/send-email-using-gmail-smtp-server-through-php-mailer – Progrock Jul 07 '16 at 17:41
  • You could of course try following the link to the troubleshooting guide, or perhaps reading some of the many duplicates of this question. – Synchro Jul 07 '16 at 17:47
  • Possible duplicate of [SMTP error with PHPMailer](http://stackoverflow.com/questions/4185942/smtp-error-with-phpmailer) – Synchro Jul 07 '16 at 17:49

3 Answers3

1

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

Trushar Narodia
  • 3,511
  • 2
  • 11
  • 17
0

You must first of all activate gmail accepting non secure apps

Ulrich Dohou
  • 1,509
  • 14
  • 25
0

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 :)