PHP表单评论 - 抄送和密送不起作用

时间:2010-09-18 18:41:51

标签: php webforms

我有一个html表单,指向PHP电子邮件的链接。表格效果很好,但我遇到的问题是抄送和密送不能通过。

这是整个代码。请查看并帮助我理解我在标题中的抄送和密件更正错误。

感谢:

<?php
    $emailFromName = $_POST['name'];
    $emailFrom = $_POST['email'];
    $emailFromPhone = $_POST['phone'];
    $email9_11 = $_POST['9-10'];
    $email10_11 = $_POST['10-11'];
    $email11_12 = $_POST['11-12'];
    $email12_1 = $_POST['12-1'];

    if (empty($emailFromName)) {
        echo 'Please enter your name.';
    } elseif (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
        echo 'The email address entered is invalid.';   
    } else {

        $emailTo = "main@gmail.com" ;
        $subject = "Family History Conference Registration";


       if (!empty($emailFrom)) {
    $headers = 'From: "' . $emailFromName . '" <' . $emailFrom . '>';
        } else {
            $headers = 'From: Family History Conference <noreply@domain.org>' . "\r\n";
            $headers .= 'Cc: $emailFrom' . "\r\n";
            $headers .= 'Bcc: myemail@domain.com' . "\r\n";
        }

        $body = "From: ".$emailFromName."\n";
        $body .= "Email: ".$emailFrom."\n";
        $body .= "Phone: ".$emailFromPhone."\n\n";
        $body .= "I would like to attend the following classes.\n";
        $body .= "9:10 to 10:00: ".$email9_11."\n";
        $body .= "10:10 to 11:00: ".$email10_11."\n";
        $body .= "11:10 to 12:00: ".$email11_12."\n";
        $body .= "12:10 to 1:00: ".$email12_1."\n";

        /* Send Email */
        if (mail($emailTo, $subject, $body, $headers)) {
            echo "<h2>Thank you for Registering</h2>
            <h3>You have registered for the following classes</h3>
            <p>9:10 to 10:00am: \"$email9_11\" <br />
             10:10 to 11:00am: \"$email10_11\"<br />
             11:10 to 12:00:  \"$email11_12\"<br />
             12:10 to 1:00: \"$email12_1\"</p>
        <p>We look forward to seeing you October 31, 2010</p>";


        } else {
            echo 'There was an internal error while sending your email.<br>';
            echo 'Please try again later.';    
        }
    }
?>

1 个答案:

答案 0 :(得分:1)

您正在使用单引号

$headers .= 'Cc: $emailFrom' . "\r\n";

PHP不会解释单引号内的变量,必须使用双引号

$headers .= "Cc: $emailFrom\r\n";