0

I am trying to send SMTP email using my gmail account details through PHP but every time I try got an error. I am writing my code so you can see what is the problem.

PHP:

<?php

        require 'phpmailer/PHPMailerAutoload.php';

        $mail = new PHPMailer;
        //Enable SMTP debugging. 
        $mail->SMTPDebug = 3;                               
        //Set PHPMailer to use SMTP.
        $mail->isSMTP();            
        //Set SMTP host name                          
        $mail->Host = "smtp.gmail.com";
        //Set this to true if SMTP host requires authentication to send email
        $mail->SMTPAuth = true;                          
        //Provide username and password     
        $mail->Username = "username";                 
        $mail->Password = "password";                           
        //If SMTP requires TLS encryption then set it
        $mail->SMTPSecure = "tls";                           
        //Set TCP port to connect to 
        $mail->Port = 587;                                   

        $mail->From = "sender email";
        $mail->FromName = "sender name";

        $mail->addAddress("receiver email");

        $mail->isHTML(true);

        $mail->Subject = "Subject Text";
        $mail->Body = "<i>Mail body in HTML</i>";
        $mail->AltBody = "This is the plain text version of the email content";

        if(!$mail->send()) 
        {
            echo "Mailer Error: " . $mail->ErrorInfo;
        } 
        else 
        {
            echo "Message has been sent successfully";
        }
    ?>

and the error I am facing:

2016-08-12 00:22:02 Connection: opening to smtp.gmail.com:587, 
timeout=300, options=array ( ) 2016-08-12 00:22:16 SMTP ERROR: Failed to 
connect to server: Network is unreachable (101) 2016-08-12 00:22:16 SMTP 
connect() failed. Mailer Error: SMTP connect() failed. 

any help would be appreciated. Thanks

qarar
  • 5
  • 6
  • can you ping smtp.gmail.com? – DWright Aug 12 '16 at 00:36
  • @DWright ping is responding – qarar Aug 12 '16 at 00:38
  • Are you running on a vps/shared host? – Darren Aug 12 '16 at 00:38
  • You could also try port 465 on ssl. See http://stackoverflow.com/a/16048485/49251 – DWright Aug 12 '16 at 00:40
  • I am using online server/hosting server – qarar Aug 12 '16 at 00:40
  • @DWright I already try that but same problem – qarar Aug 12 '16 at 00:42
  • Check with your host, they may block outgoing SMTP. I've had this issue before with a VPS host & I needed to request an unblock on it. (*they put this on to prevent spammers black-listing their IP's.*) – Darren Aug 12 '16 at 00:43
  • ok I will check with them otherwise do you note any error in my code? @Darren – qarar Aug 12 '16 at 00:45
  • @qarar not that I can see. The error is pretty self-explanatory, it's something with your network. Might also be worth checking googles site for more info on sending like this – Darren Aug 12 '16 at 00:47
  • 1
    Heck I don't know. Make sure the path is correct for the require and the library loaded correctly. That's all I can think of besides what's already been said. Error reporting will tell you if it's ok or not. http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Aug 12 '16 at 00:48
  • You could also look at https://github.com/PHPMailer/PHPMailer/issues/270 and more specifically the first two comments in there. could be a folder/file permissions issue also. See also http://stackoverflow.com/a/32498958/ - if all those fail to solve it, then contact your host. – Funk Forty Niner Aug 12 '16 at 00:53

0 Answers0