kilinkis
kilinkis

Reputation: 602

phpmailer and gmail SMTP ERROR: Failed to connect to server: Network is unreachable (101) SMTP connect() failed

I need help please this is my code:

require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;

$mail->SMTPSecure = "tls";
$mail->Port = 587;

$mail->Username = '[email protected]';
$mail->Password = 'somepass';
$mail->addAddress('[email protected]', 'Josh Adams');
$mail->Subject = 'PHPMailer GMail SMTP test';
$body = 'This is the HTML message body in bold!';
$mail->MsgHTML($body);
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

and I get this error: 2013-12-11 15:15:02 SMTP ERROR: Failed to connect to server: Network is unreachable (101) SMTP connect() failed. Mailer Error: SMTP connect() failed.

any help please?

Upvotes: 6

Views: 59166

Answers (6)

Mar83y
Mar83y

Reputation: 49

I have also received an PHPMailer error: SMTP connect() failed.

  1. Make sure your credentials are correct. You will have to add 2-step verification and create an App password if using smtp.gmail.com. You can find it in your gmail accounts security section. In your project use created application password rather then the main gmail acc password. PS do not replace your gmail acc main password with created app password, they both have to be different.

  2. You can try to use different port 465 for SSL and 587 for TLS.

  3. Check Windows firewall if not blocking any ports. https://www.itechtics.com/check-windows-firewall-blocking-ports/?utm_content=cmp-true

  4. Also you can try to use telnet through your command prompt to see if there is a network issue. https://mailtrap.io/blog/telnet-smtp-test/

     telnet smtp.gmail.com 587
    

    or if using other port then

     telnet smtp.gmail.com 465
    

PS You also can try to use openssl instead of a telnet . I wanted to send emails through command prompt with telnet to see if It was my code that was failing or something else. But unfortunately encountered to a few issues with telnet and was not able to make it work, so instead used openssl and managed to send emails through. https://www.baeldung.com/linux/openssl-send-emails

If would like to try to use openssl I would suggest follow the link https://www.baeldung.com/linux/openssl-send-emails and:

  • start with a command openssl s_client -host smtp.gmail.com -port 587 -starttls smtp -crlf

  • when time comes to add rcpt to: <recipientEmailAddress> command make sure you write rcpt in lowercases and adding an email in between of <>.

5.Also remove SMTPDebug after you find a solution, it will return another error: json parse...

I hope it will help someone!

Upvotes: 0

kantsverma
kantsverma

Reputation: 636

I was facing the same issues on Godaddy hosting so I spend lots of time and fix it by disabling the SMTP restriction on the server end.

SMTP code is for PHPMailer

 $mail = new PHPMailer(true);

try {
    //Server settings
    //$mail->SMTPDebug = SMTP::DEBUG_SERVER;                       //Enable verbose debug output
    $mail->SMTPDebug = 2;                       //Enable verbose debug output           
    //$mail->isSMTP();                                             //Send using SMTP

    $mail->Host         = "tls://smtp.gmail.com";
    $mail->SMTPAuth     = true;
    $mail->Username     = [email protected];
    $mail->Password     = SMTP_PASSWORD;
    $mail->SMTPSecure   = "tls";
    $mail->Port         = 587;
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
    //Recipients
    $mail->setFrom('[email protected]', 'Online Order');
    $mail->addAddress('[email protected]');     //Add a recipient
    $mail->addReplyTo('[email protected]', 'Jewellery');
   

    //Attachments
    //$mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'Order';
    $mail->Body    = 'This is test email content';
    $mail->AltBody = 'This is test email content';

    if($mail->send()){
        return '1';                 
    }else{
        return 'Order email sending failed';
    }

enter image description here

Upvotes: 0

yaya
yaya

Reputation: 8253

This worked for me:

change:

  $mail->SMTPSecure = 'ssl';
  $mail->Host = 'smtp.gmail.com';
  $mail->Port = '465';

to

  $mail->SMTPSecure = 'tls';
  $mail->Host = 'smtp.gmail.com';
  $mail->Port = '587';

Upvotes: 4

Baris Taskiran
Baris Taskiran

Reputation: 148

The code below:

$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Host = 'ssl://smtp.gmail.com:465';

Upvotes: 0

Jesus
Jesus

Reputation: 1

change

$mail->SMTPSecure = "tls";

with

$mail->SMTPSecure = 'ssl';

Upvotes: -3

mti2935
mti2935

Reputation: 12027

You might want to start by isolating this problem to determine whether it's truly a network problem; or whether it's specific to PHP mailer or your code. On your server, from a command prompt, try using telnet to connect to smtp.gmail.com on port 587, like so:

telnet smtp.gmail.com 587

You should see a response from smtp.gmail.com, like so:

Trying 173.194.74.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP f19sm71757226qaq.12 - gsmtp

Do you see this, or does the connection attempt hang and eventually time out? If the connection fails, it could mean that your hosting company is blocking outgoing SMTP connections on port 587.

Upvotes: 8

Related Questions