Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SMTP connect() failed. #270

Closed
nileshcool opened this issue Aug 31, 2014 · 43 comments
Closed

SMTP connect() failed. #270

nileshcool opened this issue Aug 31, 2014 · 43 comments

Comments

@nileshcool
Copy link

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:https://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

@Synchro
Copy link
Member

Synchro commented Aug 31, 2014

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.

@Synchro
Copy link
Member

Synchro commented Aug 31, 2014

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

@nileshcool
Copy link
Author

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!
}
}

@nileshcool
Copy link
Author

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.

@Synchro
Copy link
Member

Synchro commented Aug 31, 2014

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.

@Synchro Synchro closed this as completed Sep 25, 2014
@adonis1989
Copy link

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:https://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 .= '<p style="direction:rtl;font-family:tahoma;"'."message".'

';
$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.

@Synchro
Copy link
Member

Synchro commented Oct 25, 2014

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

@adonis1989
Copy link

thank you ! you could solve my problem .

@andres18281
Copy link

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

@Synchro
Copy link
Member

Synchro commented Dec 28, 2014

@Ali3nKill3r
Copy link

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

@Synchro
Copy link
Member

Synchro commented Feb 24, 2015

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

@cony69
Copy link

cony69 commented Feb 6, 2016

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

@Synchro
Copy link
Member

Synchro commented Feb 6, 2016

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

@DonaldKellett
Copy link

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().

@Synchro
Copy link
Member

Synchro commented Feb 9, 2016

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.

@21accel
Copy link

21accel commented Mar 16, 2016

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

2016-03-16 03:06:40 Connection: opening to ssl:https://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?

@Synchro
Copy link
Member

Synchro commented Mar 16, 2016

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

@21accel
Copy link

21accel commented Mar 17, 2016

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"?

@Synchro
Copy link
Member

Synchro commented Mar 17, 2016

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.

@dickrex
Copy link

dickrex commented Mar 29, 2016

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.

@Synchro
Copy link
Member

Synchro commented Mar 29, 2016

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.

@dickrex
Copy link

dickrex commented Mar 29, 2016

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

@Synchro
Copy link
Member

Synchro commented Mar 29, 2016

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.

@dickrex
Copy link

dickrex commented Mar 29, 2016

Thank You!!!

@dickrex
Copy link

dickrex commented Mar 29, 2016

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

@dickrex
Copy link

dickrex commented Mar 29, 2016

OK, forget the last post...

@dickrex
Copy link

dickrex commented Mar 29, 2016

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?

@Synchro
Copy link
Member

Synchro commented Mar 29, 2016

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

@Synchro
Copy link
Member

Synchro commented Mar 29, 2016

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.

@dickrex
Copy link

dickrex commented Mar 29, 2016

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.

@Synchro
Copy link
Member

Synchro commented Mar 29, 2016

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.

@dickrex
Copy link

dickrex commented Mar 29, 2016

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.

@dickrex
Copy link

dickrex commented Mar 30, 2016

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

@ghost
Copy link

ghost commented Jun 19, 2016

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!

@Msz900
Copy link

Msz900 commented Jun 19, 2016

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:https://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....

@ghost
Copy link

ghost commented Jun 20, 2016

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!

@Synchro
Copy link
Member

Synchro commented Jun 20, 2016

@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.

@ghost
Copy link

ghost commented Jun 20, 2016

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

@ghost
Copy link

ghost commented Jun 29, 2016

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.

@darkworks
Copy link

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

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

@Synchro
Copy link
Member

Synchro commented Jul 3, 2016

@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.

samuelaguilera added a commit to samuelaguilera/sar-friendly-smtp that referenced this issue Sep 11, 2016
* Added handling of fatal errors in Send Email Test page.
* Added setting to allow the use of SMTP servers with invalid SSL setup.
[More information](PHPMailer/PHPMailer#270)
* Added SAR_FSMTP_DEBUG_MODE and SAR_FSMTP_ALLOW_INVALID_SSL constants,
check FAQ for more details.
@Synchro
Copy link
Member

Synchro commented Apr 3, 2017

@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.

@PHPMailer PHPMailer locked and limited conversation to collaborators Apr 3, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests