PHP mail()脚本向我发送了不需要的电子邮件

时间:2016-01-29 02:00:47

标签: php email

我有一个简单的PHP邮件程序脚本,它每天都向我发送一封电子邮件,好像有人点击了提交按钮而没有任何输入(空白电子邮件)。有没有办法抑制这种行为?我的剧本应该改变什么?由于
    

$message = $_POST[message];
$name = $_POST[name];
$email = $_POST[email];
$email_message = $name ."  has sent you a message: \n" . $message . "\n Please contact " . $name . " with \n Email: " . $email . "\n Phone: " . $phone;

echo "Hi " .$name ."<br> Your Email is: " .$email ."<br><br>And we     received the following message: <br>" . $message."<br><a href='../index.html'>Back Home</a>";

mail($to, $subject, $email_message);

?>

7 个答案:

答案 0 :(得分:1)

试试这段代码:

$message = $_POST[message];   
$name = $_POST[name];    
$email = $_POST[email];

$email_message = $name ."  has sent you a message: \n" . $message . "\n Please contact " . $name . " with \n Email: " . $email . "\n Phone: " . $phone;

echo "Hi " .$name ." < break tag > Your Email is: " .$email ." < break tag > And we     received the following message: < break tag > " . $message." < break tag > <a href='../index.html'>Back Home</a>";

if(isset($_POST[message]) && isset($_POST[name]) && isset($_POST[email]))
{
   mail($to, $subject, $email_message);

} else{
   echo "Error";

}

答案 1 :(得分:1)

在发送邮件之前检查值是否为空:

// Check if values exist before continuing
if ( 
    isset($_POST['message']) && strlen($_POST['message']) > 0
    &&
    isset($_POST['name']) && strlen($_POST['name']) > 0
    &&
    isset($_POST['email']) && strlen($_POST['email']) > 0
)
{
    $message = $_POST['message']; // Remember to use single or double quotes!!!!
    $name = $_POST['name'];
    $email = $_POST['email'];

    $email_message = $name ."  has sent you a message: \n" . $message . "\n Please contact " . $name . " with \n Email: " . $email . "\n Phone: " . $phone;

    echo "Hi " .$name ."<br> Your Email is: " .$email ."<br><br>And we     received the following message: <br>" . $message."<br><a href='../index.html'>Back Home</a>";

    mail($to, $subject, $email_message);
}

答案 2 :(得分:1)

您必须检查是否设置了要发送的值,然后单击“提交”按钮。

示例表格(form.php)

<form method="POST">
  <input type="text" name="name" />
  <input type="text" name="email" />
  <textarea name="message"></textarea>
  <input type="submit" name="submit" />
</form>

回调页面(submit.php)

<?php
if(isset($_POST['submit'])) {
   // Your code comes here
   // you can use conditions to validate the form
   // ex:  if(!empty($_POST['message'])) or if(trim($_POST['message']) != "") to avoid empty message submissions
   // Regular expressions to validate email addresses / name
}
?>

答案 3 :(得分:1)

好吧,所以 - 问题是你没有检查字段是否填满,所以我们正在这样做,如果他们没有填写所有字段,请重定向回到联系页面,告诉他为什么会出错。此外,我们暂时存储这些字段以避免问题,用户必须再次填写所有字段,以防他们确实填写了一些字段。

<?php
session_start(); // For remembering input (please remember to put this before any output)

$message = $_POST[message];
$name = $_POST[name];
$email = $_POST[email];

if( empty($message) || empty($name) || empty($email) )
{
    // Redirect back to contact page, but use sessions to remember already filled out fields
    $_SESSION['contact_form']['message'] = $message;
    $_SESSION['contact_form']['name'] = $name;
    $_SESSION['contact_form']['email'] = $email;

     header("Location: http://example.com/contact_page");
    exit; // Make sure the rest of the script isn't completed
}

if(isset($_SESSION['contact_form'])) // Check if cleaning up is needed
{
    unset($_SESSION['contact_form']); //No need to store this after we send the mail
}

$email_message = $name ."  has sent you a message: \n\n"; 
$email_message .= "Please contact " . $name . " with \n Email: " . $email . "\n";
$email_message .= "Phone: " . $phone;

mail($to, $subject, $email_message, "From: youremail@example.com\r\n"); // added a sender header, just for best practices

?>

Hi <?php echo $name; ?><br>
Your Email is: <?php echo $email; ?><br><br>
And we received the following message: <br>
<?php echo $message; ?><br>
<a href='../index.html'>Back Home</a>

现在,完成后,我们需要再次考虑联系页面,因为现在我们需要告诉他们发生了错误并再次填写联系人字段,因为这些字段确实填满了。

<?php
session_start(); // For fetching remembered output input (please remember to put this before any output)

$error = ;
$message = $name = $email = "";
if(isset($_SESSION['contact_form']))
{
    $error = true;
    $message = $_SESSION['contact_form']['message'];
    $name = $_SESSION['contact_form']['name'];
    $email = $_SESSION['contact_form']['email'];

    unset($_SESSION['contact_form']); // In case they just leave
}

?>


<?php if($error): ?>
<p class="error">You missed some fields, could you please fill them out?</p>
<?php endif; ?>
<form method="post" action="">
    <input type="text" name="name" value="<?php echo $name; ?>">
    <input type="text" name="email" value="<?php echo $email; ?>">
    <textarea name="message"><?php echo $message; ?></textarea>
    <input type="submit" name="submit">
</form>

我希望这可以让您了解一些工作流程并结合一点用户体验。

答案 4 :(得分:0)

$message = $_POST['message'];
$name = $_POST['name'];
$email = $_POST['email'];
$email_message = $name ."  has sent you a message: \n" . $message . "\n Please contact " . $name . " with \n Email: " . $email . "\n Phone: " . $phone;

echo "Hi " .$name ."<br> Your Email is: " .$email ."<br><br>And we     received the following message: <br>" . $message."<br><a href='../index.html'>Back Home</a>";

if(!empty($message) && !empty($name) && !empty($email)) {
    mail($to, $subject, $email_message);
}

我没有包含$ email_message,因为从上面开始组装它就不会是空的。

答案 5 :(得分:0)

$ subject ='电子邮件的主题行';

$ headers ='来自:Email Tital'。 “\ r \ n”。'X-Mailer:PHP /'。 phpversion();

$ message =“您的留言”\ n \ r“;

$ sentMail = @mail($ to_Email,$ subject,$ message,$ headers);

if(!$ sentMail){

echo "Server error, could not send email. Sorry for the inconvenience." ;

}其他{

回复“感谢您的垂询。我们会尽快与您联系!”;

}

答案 6 :(得分:0)

您需要检查第一个消息字段是否为空,其客户端站点检查您是否按下提交而没有消息它从不提交您的表单,

function empty() {
    var x;
    x = document.getElementById("message").value;
    if (x == "") {
        alert("Enter a message first");
        return false;
    };
}

<input type="submit" value="submit" onClick="return empty()" />