Link to home
Start Free TrialLog in
Avatar of markcrees
markcrees

asked on

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

Hi,

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 ?

cheers
Mark

<?php

    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;
        unset($error);

        // 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
            fclose($socket);
        } 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;
            }
        }
    }

?>
ASKER CERTIFIED SOLUTION
Avatar of iozturk
iozturk
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of arthuryeung
arthuryeung

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

<note>
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.
</note>

If the user does not know how to close the question, the options are here:
https://www.experts-exchange.com/help/closing.jsp


Cd&

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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Programming_Gal
EE Cleanup Volunteer