将主题字段添加到PHP电子邮件表单

时间:2015-07-19 17:31:43

标签: php html email contact-form

我在PHP中有一个电子邮件表单,我想在电子邮件正文中添加一个主题:

<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $from = 'Camino Contact Form'; 
        $to = 'emailo@example.com'; 
        $subject = 'Message from Camino.bo ';
        $body ="From: $name\n E-Mail: $email\n Message:\n $message";


// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="danger">Sorry there was an error sending your message. Please try again.</div>';
    }
}
    }
?>

此表单提交了一封主题为“来自Camino.bo的消息”的电子邮件,这没关系,但我想在电子邮件正文中提交用户从下拉菜单中选择的值:

HTML:

<select name="subject" id="email_subject">
    <option value="default subject">Select a subject</option>
    <option>Product A</option>
    <option>Product B</option>
    <option>Product C</option>
</select>

我该怎么做? PS:我的表单中的主题字段是可选的,无需验证。

2 个答案:

答案 0 :(得分:1)

你的变量有点混乱。 mail()函数的第4个参数不是'From',它是'Email Headers',它可以是正确格式的任意数量的标题值,每个标题值用新行分隔。 'From'将只是其中一个标题..

$to = 'you@youremailaddress.com';

$subject = 'My Email Subject';

$body = 'The body text of your email....';

$headers  = "MIME-Version: 1.0\r\n".
            "Content-type: text/html; charset=iso-8859-1\r\n".
            "From: YOU <your@email.com>\r\n";
            // and others as required

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

根据需要将您的POST选项添加到$body标记

答案 1 :(得分:0)

我不确定我是否遗漏了某些内容,但在您的HTML中,您希望为其余选项添加值:

<select name="subject" id="email_subject">
    <option value="default subject">Select a subject</option>
    <option value="Product A">Product A</option>
    <option value="Product B">Product B</option>
    <option value="Product C">Product C</option>
</select>

然后你的php可以如下:

<?php
if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'Camino Contact Form <youremailaddress@domain.com>'; 
    $to = 'emailo@example.com'; 
    $subject = 'Message from Camino.bo ';
    $select_subject = $_POST['subject'];

    $body ="From: $name\n E-Mail: $email\n Message: $message\n Subject: $select_subject";

    $headers = "From: " . $from;


    // If there are no errors, send the email
    if (!$errName && !$errEmail && !$errMessage) {
        if (mail ($to, $subject, $body, $headers)) {
            $result='<div class="success">Thank You! I will be in touch</div>';
        } else {
            $result='<div class="danger">Sorry there was an error sending your message. Please try again.</div>';
        }
    }
}
?>

请注意,我已添加$ select_subject并将其添加到您的电子邮件$ body变量中。另请更换&#39; youremailaddress@domain.com'使用您希望使用的FROM电子邮件地址。也许你可以进一步详细说明你的问题,但我认为这就是你想要的。请记住,这不是代码编写服务,因此您应该更加具体地了解您希望获得的确切技术知识,以便将来可以无需指导。

此外,我不知道您是否已在代码中的其他位置处理此问题,但我认为如果$ errName,$ errEmail或$ errMessage评估为true,则应显示一条消息。现在,如果其中任何一个是真的(假设您在此处粘贴的代码已完成),您的最终用户在提交表单时不会在其屏幕上显示任何内容,他们可能会对发生的事情感到困惑并尝试重新提交。