Rob
Rob

Reputation: 6380

Dictate "from" email address when sending a form

I have a form below that has required fields and is emailed to the company when a user fills it it. The problem is that the from email address shows as our server name, is there a way to dictate what the from address is?

These are the fields that control the data:

$to = the_field('email_address');
$subject = "UKHG contact enquiry";
$companyname_field = $_POST['companyname'];
$fullname_field = $_POST['fullname'];
$email_field = $_POST['email'];
$tel_field = $_POST['tel'];
$enquiry_field = $_POST['enquiry'];

Here's is the complete form:

<div id="contactform">
<script language="JavaScript">
<!--

/***********************************************
* Required field(s) validation v1.10- By NavSurf
* Visit Nav Surf at http://navsurf.com
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function formCheck(formobj){
    // Enter name of mandatory fields
    var fieldRequired = Array("fullname", "email", "tel", "enquiry");
    // Enter field description to appear in the dialog box
    var fieldDescription = Array("Name", "Email", "Telephone", "Enquiry");
    // dialog message
    var alertMsg = "Please complete the following fields:\n";

    var l_Msg = alertMsg.length;

    for (var i = 0; i < fieldRequired.length; i++){
        var obj = formobj.elements[fieldRequired[i]];
        if (obj){
            switch(obj.type){
            case "select-one":
                if (obj.selectedIndex == 0 || obj.options[obj.selectedIndex].text == ""){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
                break;
            case "select-multiple":
                if (obj.selectedIndex == -1){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
                break;
            case "text":
            case "textarea":
                if (obj.value == "" || obj.value == null){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
                break;
            default:
            }
            if (obj.type == undefined){
                var blnchecked = false;
                for (var j = 0; j < obj.length; j++){
                    if (obj[j].checked){
                        blnchecked = true;
                    }
                }
                if (!blnchecked){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
            }
        }
    }

    if (alertMsg.length == l_Msg){
        return true;
    }else{
        alert(alertMsg);
        return false;
    }
}
// -->
</script>
<form class="form" onsubmit="return formCheck(this);" method="POST" action="<?php the_permalink(); ?>">
    <table border="0" style="float:left;">
        <tbody>
            <tr>
                <td>
                <p>Company Name:</p>
                </td>
                <td>&nbsp;</td>
                <td><input type="text" name="companyname" id="companyname" /></td>
            </tr>
            <tr>
                <td>
                <p>Your Name<span style="color:red;">*</span>:</p>
                </td>
                <td>&nbsp;</td>
                <td><input type="text" name="fullname" id="fullname" /></td>
            </tr>
            <tr>
                <td>
                <p>E-mail<span style="color:red;">*</span>:</p>
                </td>
                <td>&nbsp;</td>
                <td><input type="text" name="email" id="email" /></td>
                <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
                <td>
                <p>Telephone<span style="color:red;">*</span>:</p>
                </td>
                <td>&nbsp;</td>
                <td><input type="text" name="tel" id="tel" /></td>
                <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">
                <p>Enquiry<span style="color:red;">*</span>:</p>
                </td>
                <td>&nbsp;</td>
                <td><textarea name="enquiry"></textarea></td>
                <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
                <td colspan="4">
                    <button class="blue medium awesome awesomeforward" type="submit" name="submit">Send message</button>               
                </td>
            </tr>            
        </tbody>
    </table>

<? if(isset($_POST['submit'])) { 

$to = the_field('email_address');
$subject = "UKHG contact enquiry";
$companyname_field = $_POST['companyname'];
$fullname_field = $_POST['fullname'];
$email_field = $_POST['email'];
$tel_field = $_POST['tel'];
$enquiry_field = $_POST['enquiry'];


$body = "Hello,\n\n You have an enquiry from the website, please see the details below:\n\n Name: $fullname_field\n Company Name: $companyname_field\n E-Mail: $email_field\n Tel: $tel_field\n Message:\n $enquiry_field\n\n Please reply to the enquiry asap.\n\n Kind Regards \n";

mail($to, $subject, $body);

echo "</br>Thank you for getting in touch, we will contact you shortly.";

} ?>
</form>
</div>

Upvotes: 1

Views: 77

Answers (1)

Jahnold
Jahnold

Reputation: 7683

You can set it in the PHP mail command by passing in an extra header:

$header = 'From: [email protected]';
mail($to, $subject, $body, $header);

Upvotes: 2

Related Questions