Setting up smtp on windows-7 iis-7.5

14,785

Solution 1

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

Solution 2

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.

Solution 3

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.

Share:
14,785
php-b-grader
Author by

php-b-grader

Updated on June 13, 2022

Comments

  • php-b-grader
    php-b-grader almost 2 years

    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
    
  • Dima
    Dima over 12 years
    It does work, but apparently not in windows 7. This operation system comes without build-in SMTP server.
  • php-b-grader
    php-b-grader over 12 years
    point me towards a document which states that iis 7.5 smtp feature will not operate on windows 7 without an smtp server?
  • Lex Li
    Lex Li over 12 years
    You should not configure your application and let it try to find an SMTP server locally (like you mentioned above). If you want that to work, you need to have an SMTP server running locally. It is common sense, and I don't think it needs to be documented somewhere.
  • php-b-grader
    php-b-grader over 12 years
    What? common sense to know that the SMTP feature in IIS does not provide SMTP functionality? How could it possibly be common sense to know that?????
  • Lex Li
    Lex Li over 12 years
    Read the Microsoft article again, please, technet.microsoft.com/en-us/library/cc772058%28WS.10%29.aspx‌​. Be careful and you will see "Type the unique name of your SMTP server in the SMTP Server text box or select the Use localhost box to set the name to LocalHost". The common sense is that if there is no local SMTP service locally (like the Windows 7 case), you should do what is before the "or", such as using GMail SMTP server or another. If you intend to do what is after the "or", like @Dima pointed out, you need to install a third part SMTP server locally.