Asked  2 Years ago    Answers:  5   Viewed   109 times

I am new to php and in my project I have used php mail function, but while sending mail from database it shows an error like:

Failed to connect to mailserver at "localhost" port 25, verify your "SMTP"

By searching on and google, I come to know that XAMPP does not provide SMTP server and I will have to install a SMTP server.

I am really confused.So, Which SMTP server I should install?



For this example, I will use PHPMailer.

So first, you have to download the source code of PHPMailer. Only 3 files are necessary :

  • PHPMailerAutoload.php
  • class.phpmailer.php
  • class.smtp.php

Put these 3 files in the same folder. Then create the main script (I called it 'index.php').

Content of index.php :


//Load PHPMailer dependencies
require_once 'PHPMailerAutoload.php';
require_once 'class.phpmailer.php';
require_once 'class.smtp.php';

$crendentials = array(
    'email'     => '[email protected]',    //Your GMail adress
    'password'  => 'XXXXXXXX'               //Your GMail password

$smtp = array(

'host' => '',
'port' => 587,
'username' => $crendentials['email'],
'password' => $crendentials['password'],
'secure' => 'tls' //SSL or TLS


$to         = ''; //The 'To' field
$subject    = 'This is a test email sent with PHPMailer';
$content    = 'This is the HTML message body <b>in bold!</b>';

$mailer = new PHPMailer();

//SMTP Configuration
$mailer->SMTPAuth   = true; //We need to authenticate
$mailer->Host       = $smtp['host'];
$mailer->Port       = $smtp['port'];
$mailer->Username   = $smtp['username'];
$mailer->Password   = $smtp['password'];
$mailer->SMTPSecure = $smtp['secure']; 

//Now, send mail :
//From - To :
$mailer->From       = $crendentials['email'];
$mailer->FromName   = 'Your Name'; //Optional
$mailer->addAddress($to);  // Add a recipient

//Subject - Body :
$mailer->Subject        = $subject;
$mailer->Body           = $content;
$mailer->isHTML(true); //Mail body contains HTML tags

//Check if mail is sent :
if(!$mailer->send()) {
    echo 'Error sending mail : ' . $mailer->ErrorInfo;
} else {
    echo 'Message sent !';

You can also add 'CC', 'BCC' fields etc...

Examples and documentation can be found on Github.

If you need to use another SMTP server, you can modify the values in $smtp.

Note : you may have a problem sending the mail, like 'Warning: stream_socket_enable_crypto(): this stream does not support SSL/crypto'.

In such case, you must enable the OpenSSL extension. Check your phpinfo(), look for the value of 'Loaded Configuration File' (in my case : E:Program Fileswampbinapacheapache2.4.2binphp.ini) and in this file, uncomment the line extension=php_openssl.dll. Then, restart your XAMPP server.

Wednesday, September 21, 2022

"but it doesn't seem to work on my free 000webhost account"

A: They do not offer SMTP for free hosting. Use PHP's mail() function instead.

Notice the red x next to SMTP Server on their site => ?

Either use mail() or pay for it in order to use SMTP.

You can do it on their website, or upgrade from within the control panel.

Edit (Sept. 16, 2016): This answer was posted before they revamped their website. The URL where you can see the available options for free/paid services, can be see here

This also goes for remote connections to a database.

This answer was edited in regards to a question I saw today which was related to my edit.

  • https://.com/q/39541929/1415724
Sunday, August 28, 2022

The difference between the prefixing the hostname with ssl:// and without it is whether or not the underlying stream will be wrapped through OpenSSL, or speak in plain text.

When you connect to Gmail on port 465, it expects the client will use TLS encryption. Most likely, the error message you were seeing was a general result of connecting to a service expecting an encrypted connection when it was just trying to write and read data in plain text.

PHP does magic for you when you use the ssl:// wrapper to connect to a service that supports TLS or SSL. It allows you to read and write on the stream (using say fread/fwrite) in the same manner you would on an unencrypted connection and all the handshaking, encryption, and decryption is done in the background having to do no more than prefix the host with the ssl wrapper.

As to the second question, it is most likely security related. In order to relay (send mail to another domain) you need to authenticate over SMTP which should NEVER be done in cleartext while you can connect on port 25 using an unencrypted connection and send mail to a Gmail user without authenticating (this is what most outside mail servers do when one of their users which to send mail to Gmail). But technologically, there's nothing preventing them from allowing you to send mail using an unencrypted connection, or even authenticating with Gmail credentials (this is called an open relay and is usually badly abused by spammers).

You can learn more about your first question by just reading about the SMTP protocol, the STARTTLS command, and TLS encryption in general. STARTTLS allows a client to connect to the mail server over an unencrypted connection and then negotiate (upgrade) the connection to use encryption, where on the other hand, connections to port 465 expect a TLS handshake to occur as soon as the connection is established and before any protocol (SMTP) communication occurs.

Tuesday, August 2, 2022

You would have to configure the mercury server bundled with xampp to actually deliver/relay the mails.

But I suggest you use something like SwiftMailer instead of php's mail() function.

edit: there is a third option. The mini-smtp-client built into php/win32 can't do authentication. Therefore you can't simply put; smtp_port=25 in your php.ini. But you can set sendmail_path and point to an application that can relay the message to another smtp server (including authentication), e.g. fake sendmail.
(But I still suggest swiftmailer)

Saturday, August 13, 2022

It is not possible to send emails using SMTP servers with JS or Jquery.

There is no way to hit SMTP server directly from our browsers.

EmailJS or SMTPJs gives us API but eventually we are using their servers to hit the SMTP. actually we are sending data to their server which further sends data to SMTP servers.

So we must need a server to send to SMTP. and we can use NodeJs,C#,Python or Java to do this.

In my case I even tried EmailJs , but my SMTP server blocks EmailJs server IP address.

If you are using enterpise SMTP server , in no way you can email through browser JS & Jquery. you must use a server in backend and write some code in NodeJS etc...

Thanks & correct me If I am wrong

Tuesday, December 6, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages