我正在做一个学校项目的联系表格。我发现它只发送邮件,但没有主题和评论。你能帮我吗?
<?php
if (isset($_POST['jmeno'])) {
if (!isset($_POST['email']) || !isset($_POST['koment']) || $_POST['jmeno'] = "" || $_POST['email'] = "" || $_POST['koment'] = "") {
die("Nastala chyba, musíte vyplnit všechny pole! Požadavek nebyl odeslán.");
} else {
mail('something@mail.com', $_POST['email'], $_POST['koment']);
echo '<span style="text-align:center;color:green;font-size:30px;"> Připomínka odeslána</span>';
}
}
?>
答案 0 :(得分:-1)
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] );
我创建了一个函数
mails($EmailTo, $Subject, $Message, $EmailFrom, $ReplyTo);
通过传递可以HTML格式发送内容的参数来发送电子邮件。您可以在$Message
中添加HTML表格,以便以良好的格式查看电子邮件。
<?php
// email alerts
function mails($EmailTo, $Subject, $Message, $EmailFrom, $ReplyTo='')
{
$To = $EmailTo;
$Body = $Message;
$Headers = "MIME-Version: 1.0 \r\n";
$Headers .= "From: $EmailFrom \r\n";
$Headers .= "Reply-To: $ReplyTo \r\n";
$Headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$MailSent = mail($To, $Subject, $Body, $Headers);
return $MailSent;
}
<强>编辑:强>
if (isset($_POST['jmeno']))
{
if (!isset($_POST['email']) || !isset($_POST['koment']) || $_POST['jmeno'] == "" || $_POST['email'] == "" || $_POST['koment'] == "")
{
die("Nastala chyba, musíte vyplnit všechny pole! Požadavek nebyl odeslán.");
}
else
{
$EmailFrom = isset($_POST['email']) ? $_POST['email'] : '';
$ReplyTo = $EmailFrom;
$EmailTo = 'something@mail.com';
$Subject = 'Subject Here';
$Comment = isset($_POST['koment']) ? nl2br($_POST['koment']) : '';
$Message = '
Hi, <br />
You have recieved a new message.<br /><br />
<table border="0" cellpadding="5">
<tr>
<td align="left" valign="top" width="100px">Email From :</td><td align="left" valign="top" width="200px">'.$EmailFrom.'</td>
<td align="left" valign="top">Koment :</td><td align="left" valign="top">'.$Comment.'</td>
</tr>
</table>';
$MailSent = mails($EmailTo, $Subject, $Message, $EmailFrom, $ReplyTo);
if ($MailSent)
echo '<span style="text-align:center;color:green;font-size:30px;"> Připomínka odeslána</span>';
}
}
?>
答案 1 :(得分:-2)
以下是使用php mail()函数发送带有额外标题的电子邮件的工作示例。
<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
查看PHP documentation,您可以在其中找到有关向电子邮件添加主题和其他标题的更多示例。
您的代码,例如编辑:
<?php
if (isset($_POST['jmeno']))
{
if (!isset($_POST['email']) ||
!isset($_POST['koment']) ||
$_POST['jmeno'] = "" ||
$_POST['email'] = "" ||
$_POST['koment'] = "")
{
die("Nastala chyba, musíte vyplnit všechny pole! Požadavek nebyl odeslán.");
}
else
{
$headers = 'From: something@mail.com' . "\r\n" .
'Reply-To: something@mail.com';
if( mail($_POST['email'], 'SUBJECT HERE', $_POST['koment'], $headers) )
{
echo '<span style="text-align:center;color:green;font-size:30px;"> Připomínka odeslána</span>';
} else {
echo 'Email error message';
}
}
}
?>