Search code examples
phpgmailphpmailer

PHPMailer: SMTP ERROR:10051 Failed to connect to server: A socket operation was attempted to an unreachable network


I have written a script to send mail using my Gmail credentials. It's working fine on my local machine. It means I am able to send mail from my local machine. But as I hosted it while executing this script on live server, I got this error:

SMTP -> ERROR: Failed to connect to server: A socket operation was attempted to an unreachable network. (10051)

My code is

<?php

    require_once('core/class.phpmailer.php');

    $mail = new PHPMailer(); // Create a new object
    $mail->IsSMTP(); // Enable SMTP
    $mail->SMTPDebug = 2; // Debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true; // Authentication enabled
    $mail->SMTPSecure = 'ssl'; // Secure transfer enabled REQUIRED for GMail
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465; // or 587
    $mail->IsHTML(true);
    $mail->Username = "[email protected]";
    $mail->Password = "***";
    $mail->SetFrom("[email protected]");
    $mail->Subject = "Test";
    $mail->Body = "hello";
    $mail->AddAddress("[email protected]");
    echo "<pre>";
    //var_dump($mail);
    if (!$mail->Send())
    {
       echo "Mailer Error: " . $mail->ErrorInfo;
    }
    else
    {
       echo "Message has been sent";
    }
    //var_dump($mail->Send())
?>

It is working fine on for local machine.

I think there is something wrong in configuration of SMTP email sever

Is there any solution?


Solution

  • I think there is something missing while configuring mail server.

    I'd like to think that Google know how how to configure an email server (certainly in my experience they provide a very reliable service).

    The problem is that the machine where your code is running can resolve the hostname but is not configured with a route to that server (it's not uncommon to disable outgoing internet access on servers accessible from networks).

    You need to speak to whoever manages the network and server - but unless this is a dedicated server with a very expensive support contract they're not likely to configure this kind of access - but they may be able to advise which MTAs you do have access to (but you won't be able to send emails from '@gmail.com')