SMTP Error: Could not connect to SMTP host


           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
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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
Usually the ports for normal and ssl authentication are different (25 vs 465).
See here an example of using phpmailer with tls:

You might need to configure your SMTP server to use SSL i guess. If it works without SSL
JacobbabyAuthor Commented:
    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?
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Dan CraciunIT ConsultantCommented:

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.
Marcus BointonCommented:
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 = '';
//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 = "";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";

Open in new window

Dave BaldwinFixer of ProblemsCommented:
I believe that GitHub is old also, I've been getting a more recent version from Google at .

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.
Marcus BointonCommented:
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.
Dave BaldwinFixer of ProblemsCommented:
Sorry, plain text SMTP on 587.  I have hosts that have stopped supporting email clients on port 25 and ISPs that block port 25.
Marcus BointonCommented:
Sure, that's why 587 was invented - outbound port 25 is a definite no-no. The thing that's nice about TLS is that you can suport both encrypted and unencrypted connections on one port - it's not uncommon to support TLS on port 25 for inbound connections. Strictly speaking the submission protocol mandates the use of TLS, but skipping it is not a big problem - it doesn't present any interoperability issues unless it actively rejects encrypted requests, which would be an odd thing to do.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.