Hi all, I have a terribly frustrating issue I'm trying to solve and wondered if anyone would be able to help?

I have a simple HTML / CSS website hosted on my webserver. This website has a basic web contact form that allows a user to fill in their details and click submit. It then sends an email to a single contact so they can then get in touch with the user.

This contact form is handled by a version of Formmail.php - Sadly I know very little about PHP, but from what I can gather I don't need to know much for this to work.

The issue I have is fairly individual. The email address that the form is sent to when submitted is located on a mailserver belonging to an ISP where they seem to have fairly stringent DNS checks. Submitting the form to this specific email address fails and logs the following error on local SMTP servers debug log (I am using MailEnable Standard Edition as my mailserver FYI):

09/21/12 11:44:52 ME-I0107: [828] Relay Granted: Sender IP (127.0.0.1) is within an authorized IP range.
09/21/12 11:44:52 ME-I0149: [828] 2125B92602164065B7BD0DF70B7231C3.MAI was received successfully and delivery thread was initiated
09/21/12 11:44:52 ME-I0074: [828] (Debug) End of conversation
09/21/12 11:44:53 ME-I0018: [8747E05908714D8CAA4E0B1A1DE82AA5.MAI] Outbound message from ([SMTP:admin@localhost]) requeued as [12297CABDAB8473FA7A92312E667285F.MAI] to the target domain [osmor.co.uk]
09/21/12 11:44:53 ME-I0123: Domain [osmor.co.uk] has MX list [forwarder.mail.uk.easynet.net,noc.mx.easynet.net,tele.mx.easynet.net]
09/21/12 11:44:53 ME-I0026: [12297CABDAB8473FA7A92312E667285F.MAI] Sending message
09/21/12 11:44:53 ME-IXXXX: [SYSTEM] DNS resolved to the following record: IP Address=212.135.6.27, Family=2, Type=1, Protocol= 0
09/21/12 11:44:53 ME-IXXXX: [12297CABDAB8473FA7A92312E667285F.MAI] Remote server returned a response indicating a temporary failure. Server Response: (451 Sender domain localhost not verified in DNS)
09/21/12 11:44:53 ME-E0034: [12297CABDAB8473FA7A92312E667285F.MAI] RCPT command to all recipients produced an error.
09/21/12 11:44:53 ME-I0050: [12297CABDAB8473FA7A92312E667285F.MAI] Send attempt completed successfully, but some recipients did not resolve
09/21/12 11:44:53 ME-I0053: [12297CABDAB8473FA7A92312E667285F.MAI] Message generated a recipient failure message to sender.
09/21/12 11:44:53 ME-I0054: [12297CABDAB8473FA7A92312E667285F.MAI] Message is being requeued to recipients where server returned an error.

If I change the address the form is sent to in the HTML and Formmail.php (for example to my own email address) it works absolutly fine - no problem. It seems to only give DNS errors when sending to my clients email address.

Formmail.php appears to use the "From" email address field submitted with the form as the email address that the form submission email is sent from. Checking the SMTP logs on the email server it appears that this isn't the case, the mail server we are sending to thinks that we are sending from "[SMTP:admin@localhost]". Because of this it is rejecting the email./

Looking at the SMTP logs we can see that it fails (above) sending to "easynet.net" and is successful (below) sending to "gmail.com".

09/21/12 11:28:24 ME-I0107: [772] Relay Granted: Sender IP (127.0.0.1) is within an authorized IP range.
09/21/12 11:28:24 ME-I0149: [772] 70537DD21FDF4E9AAD8221B3E305A14A.MAI was received successfully and delivery thread was initiated
09/21/12 11:28:24 ME-I0074: [772] (Debug) End of conversation
09/21/12 11:28:25 ME-I0018: [7F0A9843E4B542649AA5105431E55DE1.MAI] Outbound message from ([SMTP:admin@localhost]) requeued as [CF7E44BA064D4A609A166334D23CAE92.MAI] to the target domain [gmail.com]
09/21/12 11:28:25 ME-I0123: Domain [gmail.com] has MX list [gmail-smtp-in.l.google.com,alt1.gmail-smtp-in.l.google.com,alt2.gmail-smtp-in.l.google.com,alt3.gmail-smtp-in.l.google.com,alt4.gmail-smtp-in.l.google.com]
09/21/12 11:28:25 ME-I0026: [CF7E44BA064D4A609A166334D23CAE92.MAI] Sending message
09/21/12 11:28:25 ME-IXXXX: [SYSTEM] DNS resolved to the following record: IP Address=173.194.67.27, Family=2, Type=1, Protocol= 0
09/21/12 11:28:27 ME-I0049: [CF7E44BA064D4A609A166334D23CAE92.MAI] Send Completed Successfully

So, in short, Formmail.php cannot appear to send an email via a form submission to my clients email address because it uses "admin@localhost" as a SMTP email address because the mail server rejects "localhost" in DNS. Other domains I've used in test appear to work no problem, so it isn't a specific problem with Formmail.php, I just need to know how to change Formmail.php so I can force the "From" ([SMTP:admin@localhost]) address to be something specific, and therefore stop getting rejected by "easynet.net"'s mailsevers.

Would anyone please be able to help me with this?

My current version of Formmail.php is below:

<?php
# Copyright (c) 2002 Eli Sand

# PHP FormMail v2.0 20030305

# Initialize variables
$auth = NULL;
$deny = NULL;
$must = NULL;
$post = NULL;
$http = NULL;
$form = NULL;
$list = NULL;

# Fix for magic quotes when enabled
if (get_magic_quotes_gpc()) {
    foreach ($_POST as $key => $value) {
        $_POST["$key"] = stripslashes($value);
    }
}

# Detect any Windows operating system
if (strstr(php_uname(), 'Windows')) {
    $IS_WINDOWS = TRUE;
}

# USER CONFIGURABLE SETTINGS

# Authorized email address masks that can be used as the recipient
$auth = "*@osmor.co.uk, *@".str_replace('www.','',$_SERVER['localhost']);

# Email address masks that will be rejected if in the email field
$deny = "nobody@*, anonymous@*, postmaster@*";

# DO NOT EDIT ANYTHING PAST THIS POINT
# Functions

# Trim leading and trailing white space from array values
function array_trim(&$value, $key) {
    $value = trim($value);
}

# Show an error message to the user
function error_msg($error, $required = FALSE) {
    global $post;

    if (!empty($post['missing_fields_redirect']) && $required) {
        header('Location: ' . $post['missing_fields_redirect']);
    }
    elseif (!empty($post['error_redirect'])) {
        header('Location: ' . $post['error_redirect']);
    }
    else {
        echo "<html>\r\n";
        echo "\t<head>\r\n";
        echo "\t\t<title>Form Error</title>\r\n";
        echo "\t\t<style type=\"text/css\">* {font-family: \"Verdana\", \"Arial\", \"Helvetica\", monospace;}</style>\r\n";
        echo "\t</head>\r\n";
        echo "\t<body>\r\n";
        echo "\t\t<p>${error}</p>\r\n\t\t<p><small>« <a href=\"javascript: history.back();\">go back</a></small></p>\r\n";
        echo "\t</body>\r\n";
        echo "</html>\r\n";
    }

    exit();
}

# Basic pattern matching on an entire array
function pattern_grep($input, $array) {
    foreach ($array as $value) {
        $value = addcslashes($value, '^.[]$()|{}\\');
        $value = str_replace('*', '.*', $value);
        $value = str_replace('?', '.?', $value);
        $value = str_replace('+', '.+', $value);

        if (eregi('^' . $value . '$', $input)) {
            return TRUE;
        }
    }

    return FALSE;
}

# Main
# Check to make sure the info was posted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $post = array(
        'recipient'         => $_POST['recipient'],
        'email'             => $_POST['email'],
        'subject'           => $_POST['subject'],
        'realname'          => $_POST['realname'],
        'required'          => $_POST['required'],
        'env_report'            => $_POST['env_report'],
        'sort'              => $_POST['sort'],
        'redirect'          => $_POST['redirect'],
        'error_redirect'        => $_POST['error_redirect'],
        'missing_fields_redirect'   => $_POST['missing_fields_redirect']
    );

    $http = array(
        'REMOTE_USER'           => $_SERVER['REMOTE_USER'],
        'REMOTE_ADDR'           => $_SERVER['REMOTE_ADDR'],
        'HTTP_USER_AGENT'       => $_SERVER['HTTP_USER_AGENT']
    );

    if (isset($must['required'])) {
        $post['required']           = $must['required'] . ',' . $_POST['required'];
    }
    if (isset($must['env_report'])) {
        $post['env_report']         = $must['env_report'] . ',' . $_POST['env_report'];
    }
    if (isset($must['redirect'])) {
        $post['redirect']           = $must['redirect'];
    }
    if (isset($must['error_redirect'])) {
        $post['error_redirect']         = $must['error_redirect'];
    }
    if (isset($must['missing_fields_redirect'])) {
        $post['missing_fields_redirect']    = $must['missing_fields_redirect'];
    }

    if (($auth = explode(',', $auth))) {
        array_walk($auth, 'array_trim');
    }
    if (($deny = explode(',', $deny))) {
        array_walk($deny, 'array_trim');
    }

    if ($_POST['security_code'] != $_SESSION['security_code']) {
        error_msg("Invalid security code.");
    }
    else {
        unset($_POST['security_code']);
        unset($_SESSION['security_code']);
    }

    # Check for missing required fields
    if ((!empty($post['required'])) && ($list = explode(',', $post['required']))) {
        $list[] = 'recipient';
        $list[] = 'email';

        array_walk($list, 'array_trim');

        foreach ($list as $value) {
            if (!empty($value) && empty($_POST["$value"])) {
                error_msg("You have left a required field blank.", TRUE);
            }
        }
    }

    # Check the email addresses submitted
    if (pattern_grep($post['email'], $deny)) {
        error_msg("You have specified a banned email address.");
    }
    if (!eregi('^([a-zA-Z0-9\.\_\-]+)\@((([a-zA-Z0-9\-]+)\.)+([a-zA-Z]+))$', $post['email'])) {
        error_msg("You have specified an invalid email address.");
    }

    # Check if the recipients email address is authorized
    if ((!empty($post['recipient'])) && ($list = explode(',', $post['recipient']))) {
        array_walk($list, 'array_trim');

        foreach ($list as $value) {
            if (!eregi('^([a-zA-Z0-9\.\_\-]+)\@((([a-zA-Z0-9\-]+)\.)+([a-zA-Z]+))$', $value)) {
                error_msg("The recipients email address is invalid.");
            }
            if (!pattern_grep($value, $auth)) {
                error_msg("The recipients email address is unauthorized.");
            }
        }
    }
    else {
        error_msg("There was an unknown error while checking the recipients email address.");
    }

    # Sort the fields
    if ((!empty($post['sort'])) && ($list = explode(',', $post['sort']))) {
        array_walk($list, 'array_trim');

        foreach ($list as $value) {
            $form["$value"] = $_POST["$value"];
        }
    }
    else {
        $form = $_POST;
    }

    # Create the message
    $subject = empty($post['subject']) ? "Web Reply" : $post['subject'];

    foreach ($form as $key => $value) {
        if (!array_key_exists($key, $post)) {
            $message .= "${key}: ${value}\r\n\r\n";
        }
    }

    $message .= "Submitted by: <" . $post['email'] . "> on " . date('l, F jS, Y @ g:i:s a (O)') . "\r\n";

    if (!empty($post['env_report'])) {
        if (($list = explode(',', $post['env_report']))) {
            $message .= "Sender Details:\r\n";

            array_walk($list, 'array_trim');

            foreach ($list as $value) {
                if (array_key_exists($value, $http)) {
                    $message .= "\t${value}: " . $http["$value"] . "\r\n";
                }
            }
        }
    }

    # Send out the email
    if (mail($post['recipient'], $subject, $message, "From: " . $post['email'] . "\r\nReply-To: " . $post['email'] . "\r\nX-Mailer: PHP FormMail")) {

        if (!empty($post['redirect'])) {
            header('Location: ' . $post['redirect']);
        }
        else {
            echo "<html>\r\n";
            echo "\t<head>\r\n";
            echo "\t\t<title>Thank you</title>\r\n";
            echo "\t\t<style type=\"text/css\">* {font-family: \"Verdana\", \"Arial\", \"Helvetica\", monospace;}</style>\r\n";
            echo "\t</head>\r\n";
            echo "\t<body>\r\n";
            echo "\t\t<p>Thank you for filling out the form.</p>\r\n\t\t<p><small>« <a href=\"javascript: history.back();\">go back</a></small></p>\r\n";
            echo "\t</body>\r\n";
            echo "</html>\r\n";
        }
    }
    else error_msg("There was an unknown error while sending email.");
}
else error_msg("Invalid request method used.");

Recommended Answers

All 2 Replies

I am not familiar with FormMail but maybe this link helps:

http://www.scriptarchive.com/readme/formmail.html#email

In line 215 it uses PHP's internal mail function to send mail. There it sets in additional headers the From: addres using $post['email'] which some server reuire (I guess). But I think PHP's mail function sends mail using sendmail and not SMTP so maybe somewhere there could also be the cause for a trouble (well, eventualy sendmail goes to SMTP anyway).

have you set an outgoing server address in your php.ini file?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.