phpmailer 5.1 fsockopen connection refused

We struggled with this for days, no exaggeration! And I googled like crazy, but never got an answer, and only accidentally stumbled onto the solution in the end.

The problem was that when I set up my phpmailer (5.1) to send mails using my gmail account, via google smtp (, the mailer program worked. But if I tried other SMTP servers, I got the following error:

fsockopen() [function.fsockopen]: unable to connect to (Connection refused)

Now all the other SMTP servers were working with those details in other programs such as Outlook, but when using phpmailer the error kept saying that we can’t connect with the server. So we contacted the server IT guys and asked them why the server won’t allow us to make a connection, but when they monitored the server, they found that no connection attempts were even being made… which means we were getting the wrong error from phpmailer!

So what was the solution?
In phpmailer’s setup you can set what type of emailing method you wish to use — SMTP, sendmail or mail. You can set this using the functions IsSMTP(), IsMail() and IsSendmail(). Now we used SMTP so I called IsSMTP() to set the method to SMTP but nothing worked. So I changed the public variable myself like this :

$phpmailerinstance->Mailer = "SMTP"

And all of a sudden it worked!

So what was the catch? It was the CAPITAL LETTERS ! The IsSMTP() function sets the $Mailer variable to ‘smtp’ in lowercase which created problems, but when I manually set it to ‘SMTP’ in uppercase, it worked!

Some SMTP servers (like can accept SMTP requests if you set the method to ‘smtp’ in lower case, but all SMTP servers accept the request in upper case “SMTP”

I really hope this saves someone from the trouble I went through to figure it out!

8 Responses to “phpmailer 5.1 fsockopen connection refused”

  1. Chris says:

    that can’t be right

    that variable is for internal use only and it has to be lowercase ‘smtp’ otherwise it will try and use the mail() method, which is probably why it ‘appears’ to work

    • Nico says:

      Hi Chris
      After reading your comment I went through the code a few times, and I believe you might be right, I can not see that the variable Mailer is ever used externally, only internally. What I forgot to mention in my post however is that I did change some of the code in the phpmailer class. I changed the switch-case code in the send function to this:

            // Choose the mailer and send through it
            switch($this->Mailer) {
              case 'sendmail':
                return $this->SendmailSend($header, $body);
              case 'SMTP':
                return $this->SmtpSend($header, $body);
              case 'smtp':
                return $this->SmtpSend($header, $body);		  
                return $this->MailSend($header, $body);

      adding the SMTP in capital letters to be sure it goes through.
      I also checked the system by echoing data from the SmtpSend() function to make sure that the system is indeed using SMTP and not mail() or sendmail().

      So if then indeed the Mailer variable is never used other than internally than I am still left with no explanation as to why the smtp is now working, and why it was not working prior to the change. If anyone can give me more input it will really be appreciated.

      In the meantime I will further investigate it myself.

  2. Abraham says:

    Quick tip :
    When multiple cases in a switch do the same thing, you can group them — just don’t break from the case, and so let control pass to the next line, i.e. :

        case 'SMTP':
        case 'smtp':
            return $this->SmtpSend($header, $body);
  3. Deon says:

    I had a issue recently while working on CakePHP which had a similar solution. I was trying to send a mail via a smtp server with cake. Everything seemed to work fine with our details and with some other test details, but when I input the client’s details I got a 550 Authentication Required errors.

    I input the details into Outlook and into gmail and I could send emails just fine.

    My code looked as follows:

    $this->Email->template = 'activate';
    $this->Email->sendAs = 'both';
    $this->Email->smtpOptions = array(
       'host' => $host,
    $this->Email->delivery = 'smtp';

    All standard if you look at the cookbook.
    After desperately looking for a solution for about just as long as we looked for the problem with phpmailer I decided, why not. I changed the line

    $this->Email->delivery = 'smtp';


    $this->Email->delivery = 'SMTP';

    and then BAM. It worked. So strange.

  4. Sunil says:

    I was looking for this fix…………..

  5. Blancoleon says:

    Thanks, thanks and thanks

    I have solved the problem. Locally it worked perfectly, but when I uploaded it to my dedicated server it gave me errors, and it was driving me crazy, but with this change, everything’s perfect.

    Thanks again
    Gracias, gracias y gracias

    Me ha solucionado el problema. En local funcionaba perfectamente, pero cuando lo subía a mi servidor dedicado me daba fallos, y me estaba volviendo loco, pero con este cambio, todo perfecto.

    Gracias de nuevo

  6. carlos says:

    Incredible i didnt believe it could work… but i have already tried everything….. so i thought why not give a try… and it worked!!! My host provider told fsockopen is blocked… but doing what u said it worked… very strange… but worked!!

  7. Wolfgang says:

    Hi Nico, thanks for posting your solution. I had the same error after uploading on webserver and tried some things. Your solution made it, sounds unbelievable, but BANG !

    Thanks a lot !

Leave a Reply to Abraham