Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up smtp on windows-7 iis-7.5

I have configured a php/mysql app on my local laptop using iis7 for testing. I use php mail() to send emails using localhost smtp service on the server and want to replicate locally for testing. (it has been working fine for a long time on the server so I just want to replicate locally for testing purposes.)

Using the technet article: http://technet.microsoft.com/en-us/library/cc772058(WS.10).aspx I was able to configure my SMTP settings however, I still can't send email.

I have recycled the server a number of times with no effect.

I've ran a netstat -an and there is nothing listening on port25 - is there something else I need to do to get the smtp service listening on port25?

The error I'm receiving:

PHP Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

php.ini:

SMTP = localhost
smtp_port = 25
like image 854
php-b-grader Avatar asked Aug 27 '11 08:08

php-b-grader


People also ask

How do I enable SMTP on Windows 7?

Open Server Manager by right-clicking on My Computer, and selecting Manage. (Alternately, open Control Panel, click on Programs and Features, and then select Turn Windows features on or off.) Under Features, select Add Features. Select the SMTP Server check box.

Does IIS support SMTP?

The SMTP service in IIS is completely directory based. The SMTP installation creates the following directory structure in the \inetpub\mailroot directory: BADMAIL—This directory stores messages that the SMTP service can't deliver.

How do I find my SMTP server in IIS?

Open Start > Programs > Administrative Tools > Internet Information Service (IIS) Manager. Right click “Default SMTP Virtual Server” and choose “Properties”. Check “Enable logging”. Step 2: Click “Properties …” to check all options.


3 Answers

You can use something like smtp4dev (http://smtp4dev.codeplex.com/) instead of iis for test purposes. Works like a charm for me.

like image 168
Dima Avatar answered Sep 24 '22 14:09

Dima


Windows 7 does not ship SMTP service. So you have to use a third party product. This has been a well known issue, but not sure why you did not find it by searching on the Internet.

like image 37
Lex Li Avatar answered Sep 25 '22 14:09

Lex Li


Well I agree with the OP. It's not immediately obvious that W7 (even Ultimate) ships without an SMTP server (I'm pretty sure that we had it on Vista 64 Ultimate and possibly even XP), so you will have to identify a server to use, whether local, or remote.

If the server is not using authorisation, then this should work without having to mess around with IIS7 or IIS7 Express:

$smtpserver = 'host.domain.tld';
$port = 25;
$from = '[email protected]';
$replyto = $from;
$headers = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $replyto . "\r\n" . 
    'X-Mailer: PHP/' . phpversion();
$to = '[email protected]';
$subject = 'Test Message';
ini_set('SMTP', $smtpserver);
ini_set('smtp_port', $port);
$message = wordwrap("Hello World!", 70);
$success = mail($to, $subject, $message, $headers);

If the server is using clear-text authorisation (not TLS/SSL), then adding the credentials may work, depending on your version of PHP:

ini_set('username', 'yourusername');
ini_set('password', 'yourpwd');

If the server enforces the use of TLS/SSL to connect with credentials, like GMail does, then the Sourceforge xpm4 package is a straightforward solution. There are two ways you might use it with GMail (these are straight out of the examples provided with the package):

// manage errors
error_reporting(E_ALL); // php errors
define('DISPLAY_XPM4_ERRORS', true); // display XPM4 errors
// path to 'MAIL.php' file from XPM4 package
require_once '../MAIL.php';
// initialize MAIL class
$m = new MAIL;
// set from address
$m->From('[email protected]');
// add to address
$m->AddTo('[email protected]');
// set subject
$m->Subject('Hello World!');
// set HTML message
$m->Html('<b>HTML</b> <u>message</u>.');
// connect to MTA server 'smtp.gmail.com' port '465' via SSL ('tls' encryption)
// with authentication: '[email protected]'/'password'
// set the connection timeout to 10 seconds, the name of your host 'localhost'
// and the authentication method to 'plain'
// make sure you have OpenSSL module (extension) enable on your php configuration
$c = $m->Connect('smtp.gmail.com', 465, '[email protected]', 'password', 'tls', 10,
            'localhost', null, 'plain')
        or die(print_r($m->Result));
// send mail relay using the '$c' resource connection
echo $m->Send($c) ? 'Mail sent !' : 'Error !';
// disconnect from server
$m->Disconnect();

The IIS7 Express (which is what I was using) FastCGI PHP module installs with OpenSSL Extension support enabled. The above allows you to use HTML tags in your message content. The second way of using the xpm4 package is shown below, for text-only messages (again, example is from the package source):

// manage errors
error_reporting(E_ALL); // php errors
define('DISPLAY_XPM4_ERRORS', true); // display XPM4 errors
// path to 'SMTP.php' file from XPM4 package
require_once '../SMTP.php';
$f = '[email protected]'; // from (Gmail mail address)
$t = '[email protected]'; // to mail address
$p = 'password'; // Gmail password
// standard mail message RFC2822
$m = 'From: '.$f."\r\n".
     'To: '.$t."\r\n".
     'Subject: test'."\r\n".
     'Content-Type: text/plain'."\r\n\r\n".
     'Text message.';
// connect to MTA server (relay) 'smtp.gmail.com' via SSL (TLS encryption) with 
// authentication using port '465' and timeout '10' secounds
// make sure you have OpenSSL module (extension) enable on your php configuration
$c = SMTP::connect('smtp.gmail.com', 465, $f, $p, 'tls', 10) or die(print_r($_RESULT));
// send mail relay
$s = SMTP::send($c, array($t), $m, $f);
// print result
if ($s) echo 'Sent !';
else print_r($_RESULT);
// disconnect
SMTP::disconnect($c);

Both the above work with GMail, as of the date of this post, using IIS7 and without having to do any extra configuration.

like image 39
jdw Avatar answered Sep 24 '22 14:09

jdw