Introduction
Unfortunately I am one of you or one of rear, that, I got situation to use PHP mail function with IIS and SMTP servers. So, I searched many web sites for help, I think none of them given me the information. I found many web sites explain using of PHP mail function with apache and other SMTP servers.
So, I thought of writing this tutorial which contains the following things:
- Installing SMTP server on windows 2003 and XP
- Simple PHP mail examples which uses IIS – SMTP
- Under windows there is a bug in php/mail
- Using local SMTP server for sending mail
- Using remote SMTP server for sending mail
- Using sockets for the sending mail
How to install the SMTP service as an IIS component on the windows 2003
In order to use SMTP, you first have to install the SMTP service if you are running Windows Server 2003 Standard Edition or Windows Server 2003 Enterprise Edition.
To install the SMTP service,
- Place the Windows Server 2003 CD-ROM in the CD-ROM drive.
- Click Start, Control Panel, and click Add/Remove Programs.
- Click Add/Remove Windows Components in the Add Or Remove Programs dialog box.
- Click Application Server in the Windows Components dialog box, and then click the Details button.
- The Application Server dialog box appears next.
- Click IIS and then select the Details button.
- Click the SMTP Service checkbox.
- Click OK.
- Open IIS Manager.
- Verify that the SMTP Virtual Server node appears in the console tree.
When you install the SMTP service on IIS, the SMTP directory structure is created, as well as the Default SMTP Virtual Server. You can use the IIS Manager to perform the following SMTP management tasks listed below:
- Create SMTP virtual servers.
- Configure SMTP virtual servers, such as configuring the following settings:
- Connection settings
- Message settings
- Delivery settings
- Security and authentication settings
- Start, stop and pause a SMTP virtual server.
- Create and configure SMTP alias domains and remote domains.
- View current SMTP sessions.
- Terminate a particular session(s), or terminate all sessions
How to install the SMTP server on windows xp
To install SMTP server on win XP we have to follow the same steps which we followed on windows 2003 to install SMTP. Except, the four and fifth steps:
4 & 5 steps: Instead of selecting and looking at the Application sever DETAILS, select Internet Information Services (IIS) and click on DETAILS. Make sure that SMTP Service is selected and install the package
Simple PHP mail examples which uses IIS – SMTP
Here I will try to explain a simple example, but, along with a bug in it. Did not be confused, that bug I will solve on next step in this tutorial (please see below).
-
-
<?php
-
-
$Name = "Mac Donlad Nelson"; //senders name
-
$email = "email@adress.com"; //senders e-mail adress
-
$recipient = "PersonWhoGetsIt@emailadress.com"; //recipient
-
$mail_body = "The text for the mail…"; //mail body
-
$subject = "Subject for reviever"; //subject
-
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
-
-
?>
-
The example is very simple; comments are added after every line. But, additional we have to consider some others things here:
See the $header, we ended the line with “\r\n”. This is because; headers are separated with “\r\n”. Suppose, if you want to add another header like “CC”, then the $header will be as follows:
$headers = “From: sender@example.com\r\n”. “CC: ccsender@example.com”;
Looks very simple, but, trust me this solves many of our problems while programming. (Because, we usually forget this always)
Under windows there is a bug in php/mail
If you run the above example code on the IIS-SMTP servers, most of time it did not work and you will not be able to send mail. This is because, there is a bug under windows: See here: http://bugs.php.net/bug.php?id=28038
So, there is a need of little bit modification to the above example as follows:
-
-
<?php
-
-
$Name = "Mac Donlad Nelson"; //senders name
-
$email = "email@adress.com"; //senders e-mail adress
-
$recipient = "PersonWhoGetsIt@emailadress.com"; //recipient
-
$mail_body = "The text for the mail…"; //mail body
-
$subject = "Subject for reviever"; //subject
-
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
-
-
-
-
?>
-
See the ini_set function which sets the sendmail_from setting from the program.
Using local SMTP server for sending mail
Many people questioned me that, they want to install IIS and SMTP servers on the local machine and send mails from their. It is bit tricky, but with modifications in the php.ini file and SMTP configurations, we can send mails from the local SMTP server.
Configure IIS server on the localhost:
- GOTO Control Panel » Administrative Tools » Internet Services Manager
- Right click Server’s name » Properties » Server Extensions
- Now goto Options » Specify how mail should be sent » Settings
- Web server’s mail address : @localhost
- Contact address: @localhost
- SMTP mail server: localhost
- Mail encoding and character set: Use Default encoding
- Hit OK twice
Configuring SMTP Server on the localhost
- Right Click Default SMTP Virtual Server » Properties » Access » Relay
- Select “only the list below” option and add 127.0.0.1 to the computers.
Edit PHP.ini file
[mail function]
; For Win32 only.
SMTP = localhost
; For Win32 only.
sendmail_from = mymailid@mydomain.com
Using remote SMTP server for sending mail
Recently I came a across situation like, i wanted to make sure the software/code was fully functional before making the necessary changes and going live. So, I tested my code on the local machine. But, it failed to send mails while testing.
After investing a good of time, I across a simple solution for this kind of problems:
We to configure the php.ini with remote smtp server address or ISP mail server address and as well as sendmail_from as follows:
[mail function]
; For Win32 only.
smtp = smtp.myisp.com
; For Win32 only.
sendmail_from = myemailid@myisp.com
Using sockets for the sending mail
The following is the example which used sockets to connect to the mail server and send mails using the socket calls. This script I found at the
http://www.greywyvern.com/php.php and after little modifications to the script the following example is the output:
-
-
<?php
-
/* ***************************************************************
-
* Socket_mail Function v1.4
-
* A simple and efficient mailing list function
-
* Copyright (C) 2004 GreyWyvern
-
*
-
* This program may be distributed under the terms of the GPL
-
* - http://www.gnu.org/licenses/gpl.txt
-
*
-
* Tested using a Linux SMTP server. I am unsure how a Windows
-
* SMTP server will react to this function; it may need some
-
* tweaking.
-
*
-
* This function accepts an array of email addresses in the form:
-
*
-
* array("Recipient’s Name 1 <email@address1.com>",
-
* "Recipient’s Name 2 <email@address2.com>",
-
* "Recipient’s Name 3 <email@address3.com>",
-
* …);
-
*
-
* See the inline comments and http://www.greywyvern.com/php.php
-
* for more info
-
*************************************************************** */
-
-
-
// Setup
-
$fromName = "Brian Salter";
-
$fromEmail = "ns-survey@kcl.ac.uk";
-
$fromMailer = "Socketmail v2.0";
-
$smtp = "smtp.kcl.ac.uk";
-
$smtp_port = 25;
-
$charset = "ISO-8859-1";
-
-
// The file is in the format EMAIL, SCJ_CODE, SCE_SRTN
-
$subject = "National Student Survey";
-
-
$baseMessage="This is a test mail………baby";
-
-
// Strip "\r" from the message (if it came from a form input)
-
-
$baseMessage = str_replace("\r\n.", "\r\n..", str_replace("\n", "\r\n", stripslashes($baseMessage))." \r\n");
-
-
-
if (!$connect) return false;
-
-
-
-
-
-
fputs($connect, "Message-Id: <".md5(uniqid(rand())).".".preg_replace("/[^a-z0-9]/i", "", $fromName)."@$smtp>\r\n");
-
-
-
-
?>
-
The example is to understand but, anyway, I will try to explain thru simple steps as follows:
- The above Script first initializes the required things like from name, from email, email message etc.
- As we are using mail on the windows environment, we are trying to set the sendmail_from ini setting.
- We are connecting to the mail server using sockets (fsockopen)
- Adding the message headers, body and every thing using fputs
- Ending the mail connection with QUIT word
- Closing the socket connection
- Restoring the sendmail_from ini setting
Note: I know using php mail function with IIS - SMTP server is bit tricky, If anyone thinks i have done something wrong or unnecessary. Then please post here to help me and others
Hi, this is Shiva Kumar. You can call me Shiva. I'm a developer like you. Earlier I used to write articles for Computers Today. Now I started this web site to help each other ( or ) atleast contribute some thing from my end. I'm always a student, if you need any articles or help which is not there in this web site, please
Jun 15, 2009 at 20:02:00
Hi. I like the way you write. Will you post some more articles?
Jun 16, 2009 at 04:57:43
Sure GarykPatton, i am in the way of writing articles. Thank you, this complaint has given me a booast honestly