1

我在下面有一个表格,其中包含必填字段,并在用户填写时通过电子邮件发送给公司。问题是发件人电子邮件地址显示为我们的服务器名称,有没有办法规定发件人地址是什么?

这些是控制数据的字段:

$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'];

这是完整的表格:

<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>
4

1 回答 1

2

您可以通过传入一个额外的标头在 PHP 邮件命令中设置它:

$header = 'From: email@domain.com';
mail($to, $subject, $body, $header);
于 2012-05-28T08:45:18.657 回答