Link to home
Start Free TrialLog in
Avatar of Jacobbaby
Jacobbaby

asked on

SMTP Error: Could not connect to SMTP host

Hello,

           I have an issue with SMTP mail. I am getting an error "SMTP -> ERROR: Failed to connect to server:  (0) SMTP Error: Could not connect to SMTP host.", while trying to send an email. All the details (host, port etc.) are correct. But If I comment the line
 $mail->SMTPSecure="ssl";
mails are sending. What is the issue? Is there any problem, if I comment this line? Do I need to configure anything on the serverside? Please reply.
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Usually the ports for normal and ssl authentication are different (25 vs 465).
See here an example of using phpmailer with tls: http://phpmailer.worxware.com/index.php?pg=exampleagmail

HTH,
Dan
You might need to configure your SMTP server to use SSL i guess. If it works without SSL
Avatar of Jacobbaby
Jacobbaby

ASKER

Hi
    I checked it with tls and port 587. But it is not working. Now I am using port 25 and removed the line $mail->SMTPSecure="ssl"; This is working. So for port 25, no need of that line ? or that is only for 465? . How can I configure the SMTP server to use ssl?
Depends.

Is that your server? Or you're trying to send mail from your web host?

If it's from your host, send them a message asking if SSL is enabled and on what port.

If it's your server, I'm going to need more information.

Normally, if everything is configured, you'd need this:
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;

as opposed to:
$mail->SMTPAuth = true;
$mail->Port = 25;
Basically, SMPT requires SSL (authentication). I've found this out with similar errors.
Please don't link to PHPMailer on worxware; it's no longer maintained there but on github.

SMTP over SSL on port 465 has been deprecated since 1998, and many hosts no longer support it. It was only ever really used by Microsoft products anyway. The right way to use SMTP auth is TLS on port 587, knows as the SMTP submisison protocol, described in RFC4409 and RFC6409.

One of the main differences is that with SSL you first have to talk SSL, then once the connection is established, start talking SMTP over it. With the the SMTP submission protocol, you make an initial SMTP connection, then enable TLS encryption with the STARTTLS command. This means that you can do both encrypted and unencrypted on one port with TLS, though encryption is actually a requirement for the submission protocol.

With PHPMailer, you'd do it like this:

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username@gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";

Open in new window

I believe that GitHub is old also, I've been getting a more recent version from Google at https://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list .

Also about ports:  While I believe you are correct about what is "supposed" to happen, what you really have to do is use what the SMTP server requires.  Last time I checked, Gmail will use SSL on 465 and TLS on 587.  Yahoo was requiring SSL on 465.  I have several hosts that require plain POP3 on 587.
No, the google code repo was deprecated earlier this year, as it says on its front page. That's why the version on Google code was last updated in Feb and is several versions behind, and the version on github was updated today. The maintainer from Google code is now also a commiter on github, along with me.

Both Gmail and Yahoo can use both SSL on 465 and TLS on 587.

POP3 on 587? Nothing preventing you doing that, but it's an extremely strange thing to do, a good way of generating support calls. POP3 should be on 110, and 995 with SSL. You can use TLS with POP3 (RFC2595), It's rare, but it would also normally run on port 110. POP3 also has no bearing on this question as it is inbound-only.
Sorry, plain text SMTP on 587.  I have hosts that have stopped supporting email clients on port 25 and ISPs that block port 25.
ASKER CERTIFIED SOLUTION
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial