PHP邮件程序不包含正文

时间:2011-09-17 23:33:19

标签: php email mailer

我有这个表格

<form method="POST" action="mailer.php">
    <div id="email"><input type="email" name="email" class="email" placeholder="example@example.com"></div>
</form>

这个PHP

<?php

$email = $_POST['email'];
$to = "test@test.com";
$subject = "ADD THIS EMAIL ADDRESS TO THE MAILING LIST";
$body = "\n\n";
$url = 'http://10.0.1.1/~ewiuf';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo '<script> alert("PLEASE ENTER A VALID EMAIL ADDRESS") </script>';
    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL=' . $url . '">';
} else {
    if (mail($to, $subject, $body)) {
        echo '<script> alert("THANK YOU FOR SUBSCRIBING TO THE NEWSLETTER") </script>';
        echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL=' . $url . '">';
    } else {
        echo '<script> alert("THERE WAS AN UNEXPECTED ERROR. PLEASE TRY AGAIN LATER") </script>';
        echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL=' . $url . '">';
    }
}
?>

为什么表单的内容(应该是电子邮件地址)不包含在脚本发送的电子邮件正文中?脚本发送,验证,但用户输入表单的电子邮件地址不会发送给我。请帮助,谢谢。

3 个答案:

答案 0 :(得分:2)

您需要告诉它向您发送用户的电子邮件。 试试这个:

<?php
$email = $_POST['email'];
$to = "test@test.com";
$subject = "ADD THIS EMAIL ADDRESS TO THE MAILING LIST";
$body = "User Email: ".$email;
$url = 'http://10.0.1.1/~ewiuf';
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo '<script> alert("PLEASE ENTER A VALID EMAIL ADDRESS") </script>';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
}
else
{
if (mail($to, $subject, $body)) {
echo '<script> alert("THANK YOU FOR SUBSCRIBING TO THE NEWSLETTER") </script>';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
 } else {
echo '<script> alert("THERE WAS AN UNEXPECTED ERROR. PLEASE TRY AGAIN LATER") </script>';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
 }
}
?>

答案 1 :(得分:2)

$body = "\n\n";是您设置或更改$body变量的唯一位置,该变量是您作为正文(第三个参数)发送给mail函数的变量。

在您的情况下,我假设您只想将电子邮件地址放在正文中,因此您应该使用$body = $email;,或者只使用$email代替$body作为mail的第三个参数。

答案 2 :(得分:0)

您正在$ body中发送“\ n \ n”。 如果我的问题是正确的,你可能想要添加$ email,比如 $ body =“\ n \ n”+ $ email

希望有所帮助