0

I have been facing trouble with phpmailer on live server as it is giving me error smtp connect() failed (Netword is unreachable 101) but working good on localhost please someone guide me how to fix it i have been tried different approaches but nothing worked...

phpmailer code

require '/home/schoswiy/public_html/assets/PHPMailer-master/src/PHPMailer.php';
require '/home/schoswiy/public_html/assets/PHPMailer-master/src/SMTP.php';
require '/home/schoswiy/public_html/assets/PHPMailer-master/src/Exception.php';



set_time_limit(0);



if(isset($_POST['send_message'])){
  $name = trim($_POST['name']);
  $subject = trim($_POST['subject']);
  $email = trim($_POST['email']);
  $message = trim($_POST['message']);

$mail = new PHPMailer\PHPMailer\PHPMailer(true);
$mail->isSMTP();

$mail->Host = 'smtp.gmail.com';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);

$mail->SMTPAuth = true;

$mail->Port= 465;
$mail->SMTPDebug = 4;

$mail->Username ='myemail@gmail.com';
$mail->Password = 'pass';

$mail->SMTPSecure = 'ssl';


$mail->setFrom('myemail@gmail.com', 'xyz');
$mail->addReplyTo($email, $name);
$mail->addAddress('myemail@gmail.com');



$mail->Subject = $subject;


$mail->Body = $message;

if($mail->send()){
  $_SESSION['msg'] = 'Message Send Successfully';
  header("location:contact-us.php");
  echo "messsage Send";
}
else {
echo "<script type='text/javascript'> alert('Failed to send')</script>";
}

}


 ?>
Inam
  • 241
  • 2
  • 6
  • 20
  • Possible duplicate of [PHPMailer, SMTP connect() failed error with Gmail](https://stackoverflow.com/questions/25924651/phpmailer-smtp-connect-failed-error-with-gmail) – Synchro Mar 01 '19 at 20:37
  • Search before you post - there are many duplicates of this question; don't disable certificate verification; base your code on the gmail example provided with PHPMailer; don't suppress error output; read the docs that the error message points you at. – Synchro Mar 01 '19 at 20:39
  • i have been searching for almost 3 or 4 hours and tried each and every solution but nothing worked ,, it is working on localhost but don't know why it is not working on live server strange – Inam Mar 01 '19 at 20:42
  • Trying random things in a disorganised way is not debugging. Scrap your existing code, go back to the gmail example. Read the troubleshooting guide and do what it says, one thing at a time. It is *very* likely that your live server's hosting provider is blocking outbound SMTP, but your local one is not, which is the same answer posted many times on here and discussed in the docs. – Synchro Mar 01 '19 at 20:44
  • do you know how to unblock outbound SMTP in namecheap hosting service sir ? – Inam Mar 01 '19 at 20:47
  • 1
    Yes: ask them. If they won't unblock it, use a better hosting provider. – Synchro Mar 01 '19 at 20:48
  • ok thank you sir for helping :) – Inam Mar 01 '19 at 20:52

1 Answers1

0

Try to catch the error. maybe that answer could be useful. PHP - Error handling

you can make a more comfortable search

selçuk doğan
  • 406
  • 4
  • 12
  • i wrote the code in try catch block and still giving me the same error nothing changed – Inam Mar 01 '19 at 20:24