Viewed   114 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?

 Answers

5

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 :

<?php

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

/* CONFIGURATION */
$crendentials = array(
    'email'     => 'XXXXXXXX@gmail.com',    //Your GMail adress
    'password'  => 'XXXXXXXX'               //Your GMail password
    );

/* SPECIFIC TO GMAIL SMTP */
$smtp = array(

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

);

/* TO, SUBJECT, CONTENT */
$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->isSMTP();
$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
5

Most local boxes with Windows don't come with a SMTP server. You could use an external SMTP server and configure it in your php.ini file, but SMTP authentication is not possible.

I recommend you to use PHPMailer, is a simple and flexible PHP Class, that can use a SMTP server with authentication. It is also more secure than using bundled PHP mail() function.

http://phpmailer.worxware.com/

Thursday, November 17, 2022
 
5

Man, you read single line replies where there are multiple lines.

MULTILINE REPLY:

250-LINE #1 // lines that have - after ### continue on the next
250-LINE #2 // lines that have - after ### continue on the next
250 LINE #3 // last line has space after ###

SINGLELINE REPLIES:

220 LINE #1 // one reply with code 220
250 LINE #2 // another reply with code 250
// no ### code is followed by - as that means: not the last line!

Your commands are out of sync. So fix like this:

if($smtpLog['hello'] = fgets( $smtpConn )){
    // handle multiline reply
    while($smtpLog['hello']{4} !== ' '){
        $smtpLog['hello'] .= PHP_EOL.fgets( $smtpConn );
    }
}

This applies to every response you get but in your case, commands break sync at EHLO.

Thursday, December 22, 2022
2

These helped me out when I was having trouble.

http://www.danieltmurphy.com/setting-up-mercury-smtp/

http://www.youtube.com/watch?v=VU4PT7xMSO0

Mercury can be activated from the Xampp control panel.

Monday, October 3, 2022
 
1

You probably have a firewall that blocks outgoing TCP packets going to imap.gmail.com on port 993.

Ask your sysadmin to check for outgoing TCP on dport 993 (imaps). Also check if your DNS is resolving imap.gmail.com:

The command:

telnet imap.gmail.com 993

should give you a valid connection. If it doesn't succeed you found the problem.

You may want to install a IMAP server on your development machine so to continue the development offline... you can install "courier imap" package but it's not a very simple task...

If the connection succeded and the command:

openssl s_client -connect imap.gmail.com:993

give you a valid connection, the problem could be that your libc-client doesn't have SSL support compiled in. In this case you cannot use imaps with PHP, and you could use the "stunnel" command to forward clear traffic originating on your local machine going encrypted to gmail IMAP servers.

The command:

stunnel -c -d 127.0.0.1:443 -r imap.gmail.com:993

should do the trick. This way you can connect your PHP script to 127.0.0.1:443:

<?
  $connect = "{localhost:443}INBOX";
?>
Friday, December 9, 2022
 
red888
 
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 :