【问题标题】:Add a subject field to PHP email form向 PHP 电子邮件表单添加主题字段
【发布时间】:2015-07-19 17:31:43
【问题描述】:

我有一个 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:我的表单中的主题字段是可选的,不需要验证。

【问题讨论】:

标签: php html email contact-form


【解决方案1】:

您的变量有点混乱。 mail()函数的第4个参数不是'From',它是'Email Headers',可以是任意数量的正确格式的header值,每个用新行分隔。 “发件人”只是这些标题之一..

$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);

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

【讨论】:

    【解决方案2】:

    我不确定我是否遗漏了某些内容,但在您的 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 变量中。另外,请将“yourremailaddress@domain.com”替换为您希望使用的 FROM 电子邮件地址。也许您可以进一步详细说明您的问题,但我认为这就是您想要的。请记住,这不是一个代码编写服务,因此您应该更具体地了解您希望获得的确切技术知识,以便您将来可以在没有指导的情况下进行此操作。

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

    【讨论】:

      猜你喜欢
      • 2014-06-26
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      相关资源
      最近更新 更多