IIS Hosted webpage sending email to Exchange 5.5/2000 server via SMTP

Posted on 2003-03-13
Medium Priority
Last Modified: 2010-04-06

My resident programmer is having problems writing a webpage in PHP that submits an email containing web page information to email account on the companies Exchange Server via SMTP.  
The code he wrote works fine when using a (non production) Linux server that is running send mail, however the projects call for using the Live (production) Exchange Server.  

There is a firewall between the 2 servers, but is open for port 25.

The programmer says the code is correct, however he is only change the SMTP server variable.

Here is a copy of the code that is currently work with a Linux box

Can anyone help ?



    function read_response($socket) {
        // This will contain the last status message from the SMTP server
        $response    = fgets($socket, 1024);
        $status     = substr($response, 0, 1); // Grab the first number of the status msg
        $message     = substr($response, 0, strlen(trim($response)));
        $resp_msg    = $message . "<br>";
        return array($status=>$resp_msg);

    function send_email($settings, $return_errors = true) {
        // Set some constants
        $STATUS_OK             = "2";
        $STATUS_DATA_OK     = "3";
        $CRLF                 = "\n";

        // Pull in any globals
        global $error, $HTTP_HOST;

        // Set the success or fail flag to fail
        $send_success = false;

        // Set some defaults
        if (empty($settings[crlf]))     $settings[crlf]     = "<br>";
        if (empty($settings[host]))     $settings[host]     = "smtp.mydomain.com";      
        if (empty($settings[port]))     $settings[port]     = "25";
        if (empty($settings[domain]))     $settings[domain]     = $_SERVER["HTTP_HOST"];
        if (empty($settings[prior]))     $settings[prior]     = 3; // Normal Priority

        // Let's do some error checking
        if (empty($settings[to]))         $error .= "TO address not specified" . $settings[crlf];
        if (empty($settings[from]))     $error .= "FROM address not specified" . $settings[crlf];
        if (empty($settings[msg]))         $error .= "MSG not speficied" . $settings[crlf];
        if (empty($settings[domain]))     $error .= "DOMAIN not specified" . $settings[crlf];

        if (empty($settings[reply]))    $settings[reply] = $settings[from];
        if (empty($settings[errors]))    $settings[errors] = $settings[reply];

        // Pull out the simple email if they are putting in 'name <email@domain.com>'
        if (ereg("(([[:alnum:]]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))", $settings[to], $reg_array)) {
            $simple_to = $reg_array[0];
        } else {
            $simple_to = $settings[to];
        if (ereg("(([[:alnum:]]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))", $settings[from], $reg_array)) {
            $simple_from = $reg_array[0];
        } else {
            $simple_from = $settings[from];

        // If we already have errors, don't even attempt to open the socket
        if (!empty($error)) {
            if ($return_errors) {
                return $error;
            } else {
                return false;

        if ($socket = fsockopen($settings[host], $settings[port])) {
            list($status, $resp_msg) = each(read_response($socket));
            if ($status == $STATUS_OK) {
                fputs($socket, "HELO " . $settings[domain] . $CRLF);
                list($status, $resp_msg) = each(read_response($socket));
                if ($status == $STATUS_OK) {
                    fputs($socket, "MAIL FROM:" . $simple_from . $CRLF);
                    list($status, $resp_msg) = each(read_response($socket));
                    if ($status == $STATUS_OK) {
                        fputs($socket, "RCPT TO: " . $simple_to . $CRLF);
                        list($status, $resp_msg) = each(read_response($socket));
                        if ($status == $STATUS_OK) {
                            fputs($socket, "DATA" . $CRLF);
                            list($status, $resp_msg) = each(read_response($socket));
                            if ($status == $STATUS_DATA_OK) {
                                $buffer = "From: " . $settings[from] . $CRLF;
                                $buffer .= "To: " . $settings[to] . $CRLF;
                                if (!empty($settings[cc])) {
                                    $buffer .= "CC: " . $settings[cc] . $CRLF;
                                if (!empty($settings[bcc])) {
                                    $buffer .= "BCC: " . $settings[bcc] . $CRLF;
                                $buffer .= "Reply-To: " . $settings[reply] . $CRLF;
                                $buffer .= "Errors-To: " . $settings[errors] . $CRLF;
                                $buffer .= "Bounce-To: " . $settings[errors] . $CRLF;
                                $buffer .= "X-Priority: " . $settings[prior] . $CRLF;
                                $buffer .= "X-Mailer: QUICK-N-DIRTY Mailer w/PHP " . phpversion() . $CRLF;
                                $buffer .= "Subject: " . $settings[subject] . $CRLF;
                                $buffer .= $CRLF;
                                $buffer .= $settings[msg];

                                // echo nl2br($buffer);
                                if (fputs($socket, $buffer . "$CRLF.$CRLF")) {
                                    list($status, $resp_msg) = each(read_response($socket));
                                    if ($status == $STATUS_OK) {
                                        $send_success = true;
                                        fputs($socket, "QUIT" . $CRLF);
                                } // Close fputs of buffer
                            } // Close status of DATA command
                        } // Close status of RCPT TO command
                    } // Close status of MAIL FROM command
                } // Close status of HELO command
            } // Close status of SMPT welcome
        } else {
            $error .= "Unable to establish initial connection with " . $settings[host] . ":" . $settings[port] . $settings[crlf];

        if (!empty($resp_msg)) {
            $error .= $resp_msg . $settings[crlf];

        if ($send_success) {
            return true;
        } else {
            if ($return_errors && isset($error)) {
                return $error;
            } else {
                return false;

Question by:markcrees
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

iozturk earned 672 total points
ID: 8134696
Activate your SMTP service and in internet Information Services aplet, right click SMTP ->properties->delivery->advanced delivery write smart host as []
and change change smtp server variable to your server.

Assisted Solution

arthuryeung earned 664 total points
ID: 8135106
You probably have not enable relay mail in your SMTP server.

You can go to the SMTP Server, in your IIS Management Console and right click on your SMTP Server -> Properties -> Access -> Relay -> Add

Try to add the ip of the Web Server to the granted list.

hope this can help

Expert Comment

ID: 8135155
You probably have not enable relay mail in your SMTP server.

You can go to the SMTP Server, in your IIS Management Console and right click on your SMTP Server -> Properties -> Access -> Relay -> Add

Try to add the ip of the Web Server to the granted list.

hope this can help
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.


Assisted Solution

bolesj earned 664 total points
ID: 8137699

Have you tried using the builtin php Mail() command to test?


[just a thought]
LVL 53

Expert Comment

ID: 9115241
This question has been classified abandoned. I will make a recommendation to the
moderators on its resolution in a week or two. I appreciate any comments
that would help me to make a recommendation.

Unless it is clear to me that the question has been answered I will recommend delete.  It is possible that a Grade less than A will be given if no expert makes a case for an A grade. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

If the user does not know how to close the question, the options are here:



Expert Comment

ID: 9648722
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split between iozturk, arthuryeung, bolesj

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question