主题字段添加到PHP电子邮件的形式
问题描述:
我在PHP的电子邮件的形式,我想一个主题添加到邮件正文:主题字段添加到PHP电子邮件的形式
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Camino Contact Form';
$to = '[email protected]';
$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:我的表单中的主题字段是可选的,不需要验证。
答
我不知道如果我失去了一些东西,但在你的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 <[email protected]>';
$to = '[email protected]';
$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变量中。另外,请将'[email protected]'替换为您希望使用的FROM电子邮件地址。也许你可以进一步阐述你的问题,但我认为这是你想要的。请记住,这不是一个代码写作服务,所以你应该在你想获得的确切的技术知识中更加具体,以便将来可以做到这一点。另外,我不知道你是否已经在你的代码中的其他地方处理过这个问题,但是我认为如果$ errName,$ errEmail或$ errMessage都计算为true,你应该在屏幕上显示一条消息。现在,如果其中任何一个为真(假设您在此处粘贴的代码已完成),则最终用户在提交表单时屏幕上不会显示任何内容,他们可能会对发生的事情感到困惑,并尝试重新提交。
答
您的变量有点混淆。 mail()
函数的第4个参数不是'From',而是'电子邮件标题',它可以是正确格式的任意数量的标题值,每个标题值都用一个新行分隔。在“从”只会是那些标题之一..
$to = '[email protected]';
$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 <[email protected]>\r\n";
// and others as required
mail($to, $subject, $body, $headers);
添加您的发贴选项的$body
标签的要求
http://stackoverflow.com/a/6670067/2889187 –