4

I've looked into the following links:

phpmailer send gmail smtp timeout

send email using Gmail SMTP server through PHP Mailer

http://uly.me/phpmailer-and-gmail-smtp/

...and tried to implement for myself a combination of those however...most of the time it sends this message...

Message could not be sent.

Mailer Error: SMTP connect() failed.

However there was one time where it sent this when I experimented between "tls" and "ssl"...

SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed. Message could not be sent.

Mailer Error: SMTP connect() failed.

My code is attached...did I somehow miss something? I asked the web hosting service if they're blocking and gave them a template of my code - they said the server allows connections to Gmail's SMTP.

    require_once("class.phpmailer.php");
    $mail = new PHPMailer();
    $mail -> IsSMTP();
    $mail -> SMTPDebug = 2;
    $mail -> SMTPAuth = 'true';
    $mail -> SMTPSecure = 'tls';
    $mail -> SMTPKeepAlive = true;
    $mail -> Host = 'smtp.gmail.com';
    $mail -> Port = 587;
    $mail -> IsHTML(true); 

    $mail -> Username = "myemail@gmail.com";
    $mail -> Password = "mypassword";
    $mail -> SingleTo = true; 

    $to = xxx;                           
    $from = xxx;
    $fromname = xxx;
    $subject = xxx;
    $message = xxx
    $headers = "From: $from\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";

    $mail -> From = $from;
    $mail -> FromName = $fromname;
    $mail -> AddAddress($to);

    $mail -> Subject = $subject;
    $mail -> Body    = $message;

    if(!$mail -> Send()){
        echo "Message could not be sent. <p>";
        echo "Mailer Error: " . $mail-> ErrorInfo;
        exit;
    }
Community
  • 1
  • 1
redber2009
  • 47
  • 1
  • 1
  • 6
  • By the way, it works perfectly great on my own localhost if I send I use the mailer from there - when I upload it to the host however, it errors. – redber2009 Oct 07 '13 at 02:44
  • I have also looked into [link](http://stackoverflow.com/questions/2867017/phpmailer-with-gmail-smtp-error) it doesn't help since I experimented with both ssl and tls and it gives out the same output – redber2009 Oct 07 '13 at 02:46
  • i edited my answer, have a look incase you missed it – Krimson Oct 07 '13 at 03:18

8 Answers8

8

I dug into it. Use fsocketopen, which is native to php, to test the connection and eliminate most of the potential problems. Write a simple php page with this:

    $host = "smtp.gmail.com";
    $port = "587";
    $checkconn = fsockopen($host, $port, $errno, $errstr, 5);
    if(!$checkconn){
        echo "($errno) $errstr";
    } else {
        echo 'ok';
    }

You should get back "ok". If not you know you have a connection problem that has nothing to do with Phpmailer. If that's the case it's probably the hosting company. If not then it's probably something simple about the difference between your local host and the hosting company like different versions of php.

I suspect though that this script won't make the connection

Casey
  • 1,941
  • 3
  • 17
  • 30
  • Pretty cool Casey!!! It says "ok"...which means it's problem with the hosting company. In any case, I'm trying to do it with ssl now...the error is now SMTP -> ERROR: Failed to connect to server: Connection timed out (110)SMTP Connect() failed – redber2009 Oct 11 '13 at 04:23
  • It means the hosting company isn't blocking the port. Must be some configuration issue with phpMailer – Casey Oct 11 '13 at 19:42
  • I am having a similar problem of blocked / not-blocked. Linking here for visibility https://stackoverflow.com/questions/32827193/outbound-port-open-on-firewall-how-to-unblock-for-php – William Entriken Sep 28 '15 at 20:52
3

I had this same problem and solved it:

First, turn on smtp error logging in phpmailer:

    $mail->SMTPDebug  = 2;       // enables SMTP debug information (for testing)

Then retry your phpmailer email send. You will see the entire SMTP conversation on standard error output. If you're using a web server, look in the web server log file.

I could then see the error response from gmail. Gmail was not accepting the login.

The error within the smtp conversation refers to an article. It gives some tips:

  1. Allow less secure apps to use your account.
  2. Login to gmail from a web browser.
  3. Confirm the gmail login captcha. (It may not actually display a captcha to you, but this was the additional step that suddenly allowed my email to go through.)
Larry K
  • 47,808
  • 15
  • 87
  • 140
2

Use ssl

$mail -> SMTPSecure = 'ssl';

Port should be 465

$mail -> Port = 465;

Change your host to:

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

Hopefully it works

Krimson
  • 7,386
  • 11
  • 60
  • 97
1

Check to make sure you can reach gmail from your webhost. I'm assuming it's linux. SSH in and on the command line type

telnet smtp.gmail.com 587

You should get back

Connected to smtp.something 

It has to be a configuration difference between localhost and your provider

Casey
  • 1,941
  • 3
  • 17
  • 30
  • This is intersting Casey - not sure how to do this though...For localhost connections to gmail the code works...as for connections from my remote web host - they told me that their server allows connections to gmail. Do I clarify for configuration? – redber2009 Oct 07 '13 at 03:48
  • Can you ssh into your webserver? And the only thing you need to know from them is if port 587 is blocked. If they tell you it's not that doesn't necessarily mean it isn't. What webhost are you using, and is it shared hosting? – Casey Oct 07 '13 at 03:53
  • it's shared hosting - one that you get for under 5$ a month. Webhostingpad – redber2009 Oct 08 '13 at 04:35
  • I dug into it [here](https://support.webhostingpad.com/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=51) so you can't get SSH without paying. – Casey Oct 08 '13 at 23:13
1

This question has many duplicates, so here's a canned answer:

  1. Base your code on the gmail example provided with PHPMailer
  2. Check out the troubleshooting docs
  3. Be aware of this issue, related to what Larry K said.
Synchro
  • 35,538
  • 15
  • 81
  • 104
0

That might probably be Gmail blocking your access.

Go to your security configurations and see if it's blocking any access..

Or try to change your password and try again.

Rafael Fix
  • 19
  • 4
0

Add

date_default_timezone_set('Etc/UTC');

before including the autoloader. SMTP Needs to have the timezone set which was my issue.

Morgan Green
  • 1,012
  • 2
  • 10
  • 22
-2

Download sendmail for Windows from http://www.glob.com.au/sendmail/sendmail.zip Copy sendmail.exe and sendmail.ini into C:/usr/lib/

Edit sendmail.ini and enter your mail account credentials.

You might want to configure these 2 fields as well (or sending may not work) force_sender=you-sender@yourdomain.com force_recipient=you@yourdomain.com By the way I uncommented debug_logfile so I can see what data is being sent to my SMTP server.

edit c:\php\php.ini

sendmail_from = you@yourdomain.com

; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). sendmail_path = C:/usr/lib/sendmail.exe -t -i

Restart apache Start sendmail.exe either from [Start] > Run > C:/usr/lib/sendmail.exe or Go to C:/usr/lib in Windows Explorer and then DoubleClick on the exe file.

And this solution appears in Sendmail Wamp Php

I tried it on Windows 10 now and it runs with gmail account

Added:

Open CMD and make a sendmail as daemon/service using

sc create SendMail binpath= "C:\usr\lib\sendmail.exe"

Now, be sure you have OpenSSL installed on your system, you can download it from here: https://slproweb.com/products/Win32OpenSSL.html

Installs the version what do you need and dont remember to install "files to windows folder". Do a restart and try again, you will have it solved.

Community
  • 1
  • 1
Newtron
  • 160
  • 1
  • 8