Phpmailer: SMTP connect() failed.

Created on 31 Aug 2014  路  43Comments  路  Source: PHPMailer/PHPMailer

Hi,
I am using the gmail smtp server (smtp.gmail.com), with my gmail (free) username and password but each time I am getting the following error

2014-08-31 17:13:00 Connection: opening to ssl://smtp.gmail.com:465, t=10, opt=array ( )
2014-08-31 17:13:00 SMTP ERROR: Failed to connect to server: Permission denied (13)
2014-08-31 17:13:00 SMTP connect() failed. array(1) { [0]=> string(46) "SMTP connect() failed.

I have tried with both tls (port=587), and ssl (port=465), but got the same error

Most helpful comment

This is like saying that a broken light bulb works perfectly when it's switched off.

All 43 comments

This is a problem with permissions somewhere outside PHPMailer. At a guess I'd say your php.ini does not allow the use of fopen, or perhaps your PHP fcgi socket is not set up correctly.

You could try setting $mail->SMTPDebug = 4; to get more feedback on connection errors. Generally you should be using tls on 587.

Synchro,
Tried the reply submitted by you, first let me commit that I am just an enthusiast user, and so am not so I might miss a point or two due to lack of knowledge, so please pardon if you feels so,
Next, "fopen" is allowed from my php.ini file, because I have used this function more then once, but for fcgi socket, I really don't know that.

I have tried the above suggestion by keeping $mail->SMTPDebug = 4; and using tls on 587, and here is the error, I am getting

2014-08-31 18:28:49 Connection: opening to smtp.gmail.com:587, t=10, opt=array ( )
2014-08-31 18:28:59 SMTP ERROR: Failed to connect to server: Permission denied (13)
2014-08-31 18:28:59 SMTP connect() failed. array(1) { [0]=> string(46) "SMTP connect() failed.

Is their a chance that and I am (my gmail account) not allowed to use smtp.gmail.com.

Also, for you reference I am putting my code below (removing password, and only the part which is responsible for sending mail, not the whole)

if (empty($errors)) {
    $mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
try {
    $mail->SMTPDebug = 4;                               // Enable verbose debug output
    $mail->isSMTP();                                    // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                             // Enable SMTP authentication
    $mail->Username = '[email protected]';           // SMTP username
    $mail->Password = 'password';                       // SMTP password
    $mail->SMTPSecure = 'tls';                          // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                  // TCP port to connect, tls=587, ssl=465
    $mail->From = '[email protected]';
    $mail->FromName = 'Teknaps Contact Form';
    $mail->addAddress($fields['email'], $fields['name']);     // Add a recipient
    $mail->addReplyTo($fields['email'], $fields['name']);
    $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
    $mail->isHTML(false);                                  // Set email format to HTML
    $mail->Subject = $fields['subject'];
    $mail->Body    = $fields['message'];
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }
    $errors[] = "Send mail sucsessfully";
} catch (phpmailerException $e) {
    $errors[] = $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
    $errors[] = $e->getMessage(); //Boring error messages from anything else!
}
}

Also, when using localhost for sending mail, I am getting suscess message

Connection: closed Message has been sentarray(1)

So, again I doubt that my user is not allowed to use the smtp.gmail.com server, though I have POP/IMAP enabled.
So, is their any thing else, I have to enable from my email.

Typically there are no restrictions on doing anything on localhost. I suspect you may be running into a system-level policy, such as SELinux or AppArmor, see this question and this posting, both of which suggest it's a system-level problem.

hi there
I have problem with my php mailer which has this error : SMTP connect() failed.
my openssl is on
my fopen is on
and i have tryed every solution i found in web
my code is like this:

include 'class/class.phpmailer.php' ;
include 'class/class.smtp.php';

$mail= new PHPMailer();

$mail->IsSMTP();
$mail->Host = "ssl://smtp.gmail.com";
$mail->SMTPAuth=true;
$mail->SMTPSecure = 'tls';
$mail->Username = "[email protected]"; // SMTP username
$mail->Password = "secret"; // SMTP password

$mail->SetFrom("secret"," secret");
$mail->AddReplyTo("secret", "secret");

$address = "secret" ;
$mail->AddAddress($address);
$mail->Subject = "subject";
$mail->IsHTML(true);
$body = '';
$body .= '

';
$body .= "";

$mail->MsgHTML($body);
$mail->AltBody= "message";
//$mail->SMTPDebug = 4;
if(!$mail->Send()) {
echo "no " . $mail->ErrorInfo;
} else {
echo "message sent";
}

i can't recognize why it dose not work
please help me.

You're not looking very hard since you are not using the code provided on this site, nor have you read the documentation either.

thank you ! you could solve my problem .

Hi to all, i have this problem often i see SMTP Error: Could not connect to SMTP host but from a server Hostinger, how would recommend me ?
sorry per the bad english, i speak spanish

Try to remove the $mail->IsSMTP();
It's already declared in $mail->SMTPAuth=true;

No, don't do that. Setting SMTPAuth = true will do nothing unless you also call isSMTP().

It's an incredibly, but I remove $mail->IsSMTP(); and all work fine

This is like saying that a broken light bulb works perfectly when it's switched off.

Just a simple question relating to SMTP and PHPMailer.

Is it true that if you comment out the $mail->isSMTP() function, PHPMailer ignores all the $mail->Username, $mail->Password authentication and just defaults to using the PHP mail() function?

I currently use GoDaddy as my website hosting platform (which I believe is what someone else was referring to when he/she said "s** hosting company") and when I tried to use Gmail SMTP authentication via PHPMailer, it did not work (apparently the SMTP request never left the GoDaddy servers). Once I commented out the $mail->isSMTP(), sending emails was like 10x faster and the problem disappeared. *However, I also noticed that the $mail->SMTPDebug no longer worked and that I could finally alter the "From" email address which I could not alter when using $mail->isSMTP().

Yes, and it's all entirely logical.

SMTP-related settings are only applicable when using SMTP, including Username, Password, Host, Port, SMTPAuth, SMTPSecure, SMTPDebug and others.

GoDaddy blocks outbound mail connections (to gmail and everywhere else), so you need to use GoDaddy's own mail servers, which may include localhost. Obviously your gmail login attempts will not work on GoDaddy's servers, though it's a good way of telling them your gmail password if your'e not paying attention. This is also why you should not disable SSL verification.

It's faster because you're not trying to connect to somewhere that's blocked, not because it's a faster sending mechanism. As I said, the most effective way to send is via SMTP to localhost (or a nearby smarthost), which avoids all the issues you've mentioned.

The from address limitation is a gmail restriction, nothing to do with PHPMailer nor GoDaddy.

Hi, I have an error for connecting to SMTP...
The error said

2016-03-16 03:06:40 Connection: opening to ssl://smtp.gmail.com:465, timeout=600, options=array ( )
2016-03-16 03:06:40 SMTP ERROR: Failed to connect to server: (0)
2016-03-16 03:06:40 SMTP connect() failed.

I've tried with this setting using Codeigniter :
$config['useragent'] = 'PHPMailer';
$config['protocol'] = 'smtp';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['smtp_host'] = 'smtp.gmail.com';
$config['smtp_user'] = '[email protected]';
$config['smtp_pass'] = 'my_password';
$config['smtp_port'] = 465;
$config['smtp_timeout'] = 600;
$config['smtp_crypto'] = 'ssl';
$config['smtp_debug'] = 3;
$config['wordwrap'] = true;
$config['wrapchars'] = 76;
$config['mailtype'] = 'html';
$config['charset'] = null;
$config['validate'] = true;
$config['priority'] = 4;
$config['crlf'] = "\n";
$config['newline'] = "\n";
$config['bcc_batch_mode'] = false;
$config['bcc_batch_size'] = 200;
$config['encoding'] = '8bit';

And I'm using local server using XAMPP...
How can i fix the error?

@21accel You need to use the latest PHPMailer, and read the troubleshooting guide. Please don't hijack unrelated tickets.

Yes I had use the latest version that I just downloaded some days ago...
And thanks after I read the troubleshooting guide, now I have a conclusion for my problem, that I don't have any access right into gmail SMTP.
By the way, what do you mean with "Please don't hijack unrelated tickets"?

You can't have been using a recent version - all releases in the last year or so include a link to the troubleshooting guide in error output, and yours did not show that.

This was hijacking because you posted your query on a closed ticket that had a completely different symptom than you were seeing.

From the http-side, how can I get som informaion from SMTPDebug? I have PHPMailer on a standrad web-hotel without consol access, just ftp.

If you set SMTPDebug to anything > 0 (2 is probably what you need), it will output an SMTP transcript to standard output, so it will just appear as normal page output. It's worth disabling any redirects after sending or you may not get to see the output. Just set it in your code before uploading.

OK, but if I would like to have the output to a variable like I can do with $error = $mail->ErrorInfo; is it possible?

Sure, just inject your own callable for the Debugoutput property, like this:

$mail->Debugoutput = function($str, $level) {$GLOBALS['error'] .= $str;};

That way all the debug output will accumulate in the $errors global variable for you to do what you like with.

Thank You!!!

But.... To get the Deugoutput, shall I do like this:
$error = $mail->Debugoutput = function($str, $level) {$GLOBALS['error'] .= $str;};

OK, forget the last post...

At most I get this:

Connection: opening to mailout.one.com:25, timeout=300, options=array (
)SMTP ERROR: Failed to connect to server: Connection timed out (110)SMTP connect()

And this must be before the password and everyting else that can go wrong I suppose?

Just looks like your mail server is dead:

telnet mailout.one.com 25
Trying 10.246.18.1...
telnet: connect to address 10.246.18.1: Connection refused
telnet: Unable to connect to remote host

Actually that IP address is not internet-accessible because it's in an RFC1918 range, so it's impossible to connect to it unless you're on the same network already.

Well, I have my web space at their (one.com) server, so I am at the same network in some way even if I have my own domain.

Does your server have a 10.x.x.x ip address? Either way, it's refusing connections or timing out, so it's not looking good for your mail server. I suggest you contact their support, read their docs.

one.com is a big actor, and I don't thing they kan have a mail-server down for several hours. But I've sent a mail to their support.

But it was like you said :-) The server was down and now it's working just fine.

Hi @dickrex and anybody else :),
I've been searching and trying now for several hours to find the correct settings for PHPMailer on one.com but nothing seems to work. Both, mail and website run on one.com so I think its just a matter of misconfiguration.

Also I dont get any error if it comes to $mail->send (not even with SMTPDebug = 4, no timeout just loading and waiting).

I would really appreciate it if you could post your settings or help otherwise. Thank you!

Hi @Hagenzone Please use the below config, as it works for me, and i am also using it.

require 'PHPMailer-master/class.phpmailer.php';
require 'PHPMailer-master/PHPMailerAutoload.php';

$mail = new PHPMailer();
$mail->isSMTP(true); // telling the class to use SMTP
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->SMTPSecure = 'tls';
$mail->Host = 'tls://smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = "your Email address"; // SMTP username
$mail->Password = "your Email password here"; // SMTP password
// TCP port to connect to
// $mail->SMTPDebug = 1;
$mail->setFrom('your Email address');
$mail->AddAddress("user/client email address");
$mail->Subject = "some text, what ever you want";
$mail->Body = "Email info/message here. ";
$mail->WordWrap = 70;
$mail->Send();
Hope it will help you....

Hi @Msz900,

thank you for posting your code. Unfortunately this code does not work. I suppose its because your configuration is for gmail, not for one.com which seems to need a slightly more different config.

But thank you anyway!

@Msz900 Do not post incorrect, obsolete, unsafe code. It doens't help anyone, especially when it doesn't address the problems of the original poster.

@Synchro exactly, GitHub isn't StackOverflow, lol.

The one.com-support helped me out. Here is the configuration that worked for me:

smtp: true
smtpauth: true
host: send.one.com
username, password,
smtpsecure: ssl,
port: 465

Hope this one can help somebody.

for gmail you must turn on access to 3rd party apps

https://www.google.com/settings/security/lesssecureapps // turn it on

@darkworks don't post incorrect and irrelevant comments please. It is not a requirement to enable less secure apps in gmail, and the OP isn't using gmail anyway.

@mqasim1990 - that "fix" is mentioned and described in detail above, and is covered in the PHPMailer docs, which are linked to from the error message you got. Don't hijack year-old tickets to add duplicate and misleading information.

Was this page helpful?
0 / 5 - 0 ratings