【问题标题】:Setting the email correctly in a contact form在联系表格中正确设置电子邮件
【发布时间】:2012-07-17 18:23:39
【问题描述】:

考虑到我在 PHP 方面的经验有限,我使用了一个联系表单生成器,它几乎可以按照我的意愿工作。该表单及其所有变量都可以正常运行,但是在收到电子邮件后,它会将我自己的域添加为发件人的电子邮件。我想让填写联系表单电子邮件的人作为发件人,以便我可以轻松地回复邮件(而不是从邮件中复制和粘贴电子邮件地址)。

下面是部分代码:

<?php

$where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

session_start();
if( ($_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) ) { 
// Checkbox handling
$field_4_opts = $_POST['field_4'][0].",". $_POST['field_4'][1].",". $_POST['field_4'][2];

// Checkbox handling
$field_5_opts = $_POST['field_5'][0].",". $_POST['field_5'][1].",". $_POST['field_5'][2].",". $_POST['field_5'][3];

mail("mail@mail.com","phpFormGenerator - Form submission","Form data:

Your Name:: " . $_POST['field_1'] . " 
Country:: " . $_POST['field_2'] . " 
Your Email:: " . $_POST['field_3'] . " 
Your areas of interest:: $field_4_opts
What type of articles are you interested in writing?:: $field_5_opts
Any other types of articles that interest you?:: " . $_POST['field_6'] . " 
Approximately how often will you contribute?: " . $_POST['field_7'] . " 
Your blog/website:: " . $_POST['field_8'] . " 
What would make you a good contributor?: " . $_POST['field_9'] . " 


 powered by phpFormGenerator.
");

include("confirm.html");
}
else {
echo "Invalid Captcha String.";
}

?>

安装.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>testing2 - created by phpFormGenerator</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"><link href="style.css" rel="stylesheet" type="text/css">
    <!-- calendar stuff -->
          <link rel="stylesheet" type="text/css" href="calendar/calendar-blue2.css" />
          <script type="text/javascript" src="calendar/calendar.js"></script>
          <script type="text/javascript" src="calendar/calendar-en.js"></script>
          <script type="text/javascript" src="calendar/calendar-setup.js"></script>
    <!-- END calendar stuff -->

    <!-- expand/collapse function -->
    <SCRIPT type=text/javascript>
    <!--
    function collapseElem(obj)
    {
        var el = document.getElementById(obj);
        el.style.display = 'none';
    }


    function expandElem(obj)
    {
        var el = document.getElementById(obj);
        el.style.display = '';
    }


    //-->
    </SCRIPT>
    <!-- expand/collapse function -->


    <!-- expand/collapse function -->
        <SCRIPT type=text/javascript>
        <!--

        // collapse all elements, except the first one
        function collapseAll()
        {
            var numFormPages = 1;

            for(i=2; i <= numFormPages; i++)
            {
                currPageId = ('mainForm_' + i);
                collapseElem(currPageId);
            }
        }


        //-->
        </SCRIPT>
    <!-- expand/collapse function -->


     <!-- validate -->
    <SCRIPT type=text/javascript>
    <!--
        function validateField(fieldId, fieldBoxId, fieldType, required)
        {
            fieldBox = document.getElementById(fieldBoxId);
            fieldObj = document.getElementById(fieldId);

            if(fieldType == 'text'  ||  fieldType == 'textarea'  ||  fieldType == 'password'  ||  fieldType == 'file'  ||  fieldType == 'phone'  || fieldType == 'website')
            {   
                if(required == 1 && fieldObj.value == '')
                {
                    fieldObj.setAttribute("class","mainFormError");
                    fieldObj.setAttribute("className","mainFormError");
                    fieldObj.focus();
                    return false;                   
                }

            }


            else if(fieldType == 'menu'  || fieldType == 'country'  || fieldType == 'state')
            {   
                if(required == 1 && fieldObj.selectedIndex == 0)
                {               
                    fieldObj.setAttribute("class","mainFormError");
                    fieldObj.setAttribute("className","mainFormError");
                    fieldObj.focus();
                    return false;                   
                }

            }


            else if(fieldType == 'email')
            {   
                if((required == 1 && fieldObj.value=='')  ||  (fieldObj.value!=''  && !validate_email(fieldObj.value)))
                {               
                    fieldObj.setAttribute("class","mainFormError");
                    fieldObj.setAttribute("className","mainFormError");
                    fieldObj.focus();
                    return false;                   
                }

            }



        }

        function validate_email(emailStr)
        {       
            apos=emailStr.indexOf("@");
            dotpos=emailStr.lastIndexOf(".");

            if (apos<1||dotpos-apos<2) 
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        function validateDate(fieldId, fieldBoxId, fieldType, required,  minDateStr, maxDateStr)
        {
            retValue = true;

            fieldBox = document.getElementById(fieldBoxId);
            fieldObj = document.getElementById(fieldId);    
            dateStr = fieldObj.value;


            if(required == 0  && dateStr == '')
            {
                return true;
            }


            if(dateStr.charAt(2) != '/'  || dateStr.charAt(5) != '/' || dateStr.length != 10)
            {
                retValue = false;
            }   

            else    // format's okay; check max, min
            {
                currDays = parseInt(dateStr.substr(0,2),10) + parseInt(dateStr.substr(3,2),10)*30  + parseInt(dateStr.substr(6,4),10)*365;
                //alert(currDays);

                if(maxDateStr != '')
                {
                    maxDays = parseInt(maxDateStr.substr(0,2),10) + parseInt(maxDateStr.substr(3,2),10)*30  + parseInt(maxDateStr.substr(6,4),10)*365;
                    //alert(maxDays);
                    if(currDays > maxDays)
                        retValue = false;
                }

                if(minDateStr != '')
                {
                    minDays = parseInt(minDateStr.substr(0,2),10) + parseInt(minDateStr.substr(3,2),10)*30  + parseInt(minDateStr.substr(6,4),10)*365;
                    //alert(minDays);
                    if(currDays < minDays)
                        retValue = false;
                }
            }

            if(retValue == false)
            {
                fieldObj.setAttribute("class","mainFormError");
                fieldObj.setAttribute("className","mainFormError");
                fieldObj.focus();
                return false;
            }
        }
    //-->
    </SCRIPT>
    <!-- end validate -->




</head>

<body onLoad="collapseAll()">

<div id="mainForm">




    <div id="formHeader">
            <h2 class="formInfo">testing2</h2>
            <p class="formInfo"></p>
    </div>


    <BR/><div id="footer"><p class="footer"><a class=footer href=http://phpformgen.sourceforge.net>Generated by phpFormGenerator</a></p></div>

</body>
</html>

您能找到我需要更改的内容吗?

【问题讨论】:

    标签: php email field contact-form sender


    【解决方案1】:

    在 PHP 中调用mail 函数之前添加相关的头文件。

        $headers = 'From: user@example.com' . "\r\n" .
        'Reply-To: user@example.com' . "\r\n";
    
    mail($to, $subject, $message, $headers);
    

    在你的情况下:

    <?php
    
    $where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));
    
    session_start();
    if( ($_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) ) { 
    // Checkbox handling
    $field_4_opts = $_POST['field_4'][0].",". $_POST['field_4'][1].",". $_POST['field_4'][2];
    
    // Checkbox handling
    $field_5_opts = $_POST['field_5'][0].",". $_POST['field_5'][1].",". $_POST['field_5'][2].",". $_POST['field_5'][3];
    
    $message = "Form data:
            Your Name:: " . $_POST['field_1'] . " 
            Country:: " . $_POST['field_2'] . " 
            Your Email:: " . $_POST['field_3'] . " 
            Your areas of interest:: $field_4_opts
            What type of articles are you interested in writing?:: $field_5_opts
            Any other types of articles that interest you?:: " . $_POST['field_6'] . " 
            Approximately how often will you contribute?: " . $_POST['field_7'] . " 
            Your blog/website:: " . $_POST['field_8'] . " 
            What would make you a good contributor?: " . $_POST['field_9'] . " 
    
    
             powered by phpFormGenerator.
            ";
    
         $headers = 'From: '.$_POST['field_3']. "\r\n" .
                'Reply-To: '.$_POST['field_3'] . "\r\n" .
                'X-Mailer: PHP/' . phpversion();
    
          mail("mail@mail.com","phpFormGenerator - Form submission", $message, $headers);
    
    include("confirm.html");
    }
    else {
    echo "Invalid Captcha String.";
    }
    
    ?>
    

    【讨论】:

    • 我复制并粘贴了您的示例,但没有成功。但是,我所做的只是用现有的 processor.php 代码替换了这段代码。您能否使用该文件的完整代码更新您的答案,我可以尝试一下吗?
    • 更新了我的答案。您应该真正学习 PHP 并按照@Rogue Coder 的建议更改代码。
    • 我正在学习,但我很感激我只练习了一周......所以我必须从某个地方开始。无论如何,我添加了您更新的修复程序,但返回的电子邮件以“user@example.com”作为发件人,而不是表单中添加的电子邮件,我错过了什么?
    • 再次检查答案。我已经改变了它:D
    • 谢谢,但是还是不行。它像这样返回:$field_1@myowndomain.com
    【解决方案2】:

    你应该去学习 PHP,自动生成的表单代码太糟糕了。

    <?php
    
    $where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));
    
    session_start();
    if( ($_SESSION['security_code']==$_POST['security_code']) && (!empty($_POST['security_code'])) ) { 
    // Checkbox handling
    $field_4_opts = $_POST['field_4'][0].",". $_POST['field_4'][1].",". $_POST['field_4'][2];
    
    // Checkbox handling
    $field_5_opts = $_POST['field_5'][0].",". $_POST['field_5'][1].",". $_POST['field_5'][2].",". $_POST['field_5'][3];
    
    mail("mail@mail.com","phpFormGenerator - Form submission","Form data:
    
    Your Name:: " . $_POST['field_1'] . " 
    Country:: " . $_POST['field_2'] . " 
    Your Email:: " . $_POST['field_3'] . " 
    Your areas of interest:: $field_4_opts
    What type of articles are you interested in writing?:: $field_5_opts
    Any other types of articles that interest you?:: " . $_POST['field_6'] . " 
    Approximately how often will you contribute?: " . $_POST['field_7'] . " 
    Your blog/website:: " . $_POST['field_8'] . " 
    What would make you a good contributor?: " . $_POST['field_9'] . " 
    
    
     powered by phpFormGenerator.
    ", 'From: ' . $_POST['field_3']);
    
    include("confirm.html");
    }
    else {
    echo "Invalid Captcha String.";
    }
    
    ?>
    

    我在 mail() 函数中添加了一个新参数,用于设置 from 标头。 , 'From: ' . $_POST['field_3'] 是我添加的。看来 field_3 应该包含用户的电子邮件。试试看它是否有效。

    【讨论】:

    • 这不起作用,它返回了 namefield@myowndomain.com
    【解决方案3】:

    只需让您的代码看起来漂亮,并为其添加一些标题。它会完成您的工作 使用标头,您可以做任何您可以用您的电子邮件客户端(如 GMAIL)做的事情。(附加文件或其他任何内容)

    <pre> 
    
    $headers =  'From: abc@example.com' . "\r\n" .
                'Reply-To: def@example.com' . "\r\n" .
                'X-Mailer: PHP/' . phpversion();
    
    $message = "Form data:
        Your Name:: " . $_POST['field_1'] . " 
        Country:: " . $_POST['field_2'] . " 
        Your Email:: " . $_POST['field_3'] . " 
        Your areas of interest:: $field_4_opts
        What type of articles are you interested in writing?:: $field_5_opts
        Any other types of articles that interest you?:: " . $_POST['field_6'] . " 
        Approximately how often will you contribute?: " . $_POST['field_7'] . " 
        Your blog/website:: " . $_POST['field_8'] . " 
        What would make you a good contributor?: " . $_POST['field_9'] . " 
    
    
         powered by phpFormGenerator.
        ";
     $subject = "phpFormGenerator - Form submission"
     $to = "mail@mail.com"
    
      mail($to ,$subject,$message, $headers);
    

    【讨论】:

    • 我复制并粘贴了您的示例,没有您在顶部的
       并且它不起作用。但是,我所做的只是用现有的 processor.php 代码替换了这段代码。您能否使用该文件的完整代码更新您的答案,我可以尝试一下吗?
    猜你喜欢
    • 2022-10-23
    • 1970-01-01
    • 2017-10-19
    • 2014-07-09
    • 1970-01-01
    • 2022-07-06
    • 2019-08-15
    • 2013-11-02
    • 2020-03-02
    相关资源
    最近更新 更多