banner 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 send me a mail. I will try to update them in this web site if possible.
Jun
3rd

Things to consider while using php mail function with IIS-SMTP

Author: admin | Files under php, tutorials


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).

  1.  
  2. <?php
  3.  
  4. $Name = "Mac Donlad Nelson"; //senders name
  5. $email = "email@adress.com"; //senders e-mail adress
  6. $recipient = "PersonWhoGetsIt@emailadress.com"; //recipient
  7. $mail_body = "The text for the mail…"; //mail body
  8. $subject = "Subject for reviever"; //subject
  9. $header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
  10.  
  11. mail($recipient, $subject, $mail_body, $header); //mail command :)
  12. ?>
  13.  

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:

  1.  
  2. <?php
  3.  
  4. $Name = "Mac Donlad Nelson"; //senders name
  5. $email = "email@adress.com"; //senders e-mail adress
  6. $recipient = "PersonWhoGetsIt@emailadress.com"; //recipient
  7. $mail_body = "The text for the mail…"; //mail body
  8. $subject = "Subject for reviever"; //subject
  9. $header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
  10.  
  11. ini_set(’sendmail_from’, ‘me@mydomain.com’); // specify the ini setting  
  12.  
  13. mail($recipient, $subject, $mail_body, $header); //mail command :)
  14.  
  15. ?>
  16.  

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:

  1.  
  2. <?php
  3. /* ***************************************************************
  4. * Socket_mail Function v1.4
  5. *  A simple and efficient mailing list function
  6. * Copyright (C) 2004 GreyWyvern
  7. *
  8. * This program may be distributed under the terms of the GPL
  9. *   - http://www.gnu.org/licenses/gpl.txt
  10. *
  11. * Tested using a Linux SMTP server.  I am unsure how a Windows
  12. * SMTP server will react to this function; it may need some
  13. * tweaking.
  14. *
  15. * This function accepts an array of email addresses in the form:
  16. *
  17. * array("Recipient’s Name 1 <email@address1.com>",
  18. *       "Recipient’s Name 2 <email@address2.com>",
  19. *       "Recipient’s Name 3 <email@address3.com>",
  20. *       …);
  21. *
  22. * See the inline comments and http://www.greywyvern.com/php.php
  23. * for more info
  24. *************************************************************** */
  25.  
  26.  
  27.  // Setup
  28.  $fromName = "Brian Salter";
  29.  $fromEmail = "ns-survey@kcl.ac.uk";
  30.  $fromMailer = "Socketmail v2.0";
  31.  $smtp = "smtp.kcl.ac.uk";
  32.  $smtp_port = 25;
  33.  $charset = "ISO-8859-1";
  34.  
  35.  $toArray = file(‘Test data.csv’);  // Read the whole file into an array
  36.   // The file is in the format EMAIL, SCJ_CODE, SCE_SRTN
  37.  $subject = "National Student Survey";
  38.  
  39.  $baseMessage="This is a test mail………baby";
  40.  
  41.  // Strip "\r" from the message (if it came from a form input)
  42.  $baseMessage = str_replace(chr(13), "", $baseMessage);
  43.  
  44.  $baseMessage = str_replace("\r\n.", "\r\n..", str_replace("\n", "\r\n", stripslashes($baseMessage))." \r\n");
  45.  
  46.  ini_set(’sendmail_from’, $fromEmail);
  47.  
  48.  $connect = @fsockopen ($smtp, $smtp_port, $errno, $errstr, 5);
  49.    if (!$connect) return false;
  50.    $rcv = fgets($connect, 1024);
  51.  
  52.  
  53.    fputs($connect, "HELO 127.0.0.1\r\n");
  54.    $rcv = fgets($connect, 1024);
  55.  
  56.     fputs($connect, "RSET\r\n");
  57.      $rcv = fgets($connect, 1024);
  58.  
  59.    fputs($connect, "MAIL FROM:$fromEmail\r\n");
  60.      $rcv = fgets($connect, 1024);
  61.    fputs($connect, "RCPT TO:$toRcpt\r\n");
  62.      $rcv = fgets($connect, 1024);
  63.    fputs($connect, "DATA\r\n");
  64.      $rcv = fgets($connect, 1024);
  65.  
  66.    fputs($connect, "Subject: $subject\r\n");
  67.    fputs($connect, "From: $fromName <$fromEmail>\r\n");
  68.    fputs($connect, "To: $toRcpt\r\n");
  69.    fputs($connect, "X-Sender: <$fromEmail>\r\n");
  70.    fputs($connect, "Return-Path: <$fromEmail>\r\n");
  71.    fputs($connect, "Errors-To: <$fromEmail>\r\n");
  72.    fputs($connect, "Message-Id: <".md5(uniqid(rand())).".".preg_replace("/[^a-z0-9]/i", "", $fromName)."@$smtp>\r\n");
  73.    fputs($connect, "X-Mailer: PHP - $fromMailer\r\n");
  74.    fputs($connect, "X-Priority: 3\r\n");
  75.    fputs($connect, "Date: ".date("r")."\r\n");
  76.    fputs($connect, "Content-Type: text/plain; charset=$charset\r\n");
  77.    fputs($connect, "\r\n");
  78.    fputs($connect, $message);
  79.  
  80.    fputs($connect, "\r\n.\r\n");
  81.      $rcv = fgets($connect, 1024);
  82.  
  83.  
  84.  fputs ($connect, "QUIT\r\n");
  85.    $rcv = fgets ($connect, 1024);
  86.  fclose($connect);
  87.  ini_restore(’sendmail_from’);
  88. ?>
  89.  

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

Similar Posts - 90% of Users have seen these posts also

2 responses. Wanna say something?

  1. GarykPatton
    Jun 15, 2009 at 20:02:00
    #1

    Hi. I like the way you write. Will you post some more articles?

  2. admin
    Jun 16, 2009 at 04:57:43
    #2

    Sure GarykPatton, i am in the way of writing articles. Thank you, this complaint has given me a booast honestly

Post a Comment