简体   繁体   English

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

[英]PHP mail() script is sending me unwanted emails

I have a simple PHP mailer script and it is sending me an email everyday as if someone has clicked the submit button without any inputs (blank email). 我有一个简单的PHP邮件程序脚本,它每天都向我发送一封电子邮件,好像有人点击了提交按钮而没有任何输入(空白电子邮件)。 Is there a way to suppress this behavior? 有没有办法抑制这种行为? What should I change in my script? 我的剧本应该改变什么? Thanks 谢谢

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

?>

Try this code: 试试这段代码:

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

}

Check if the values are empty or not before sending the mail: 在发送邮件之前检查值是否为空:

// 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);
}

You have to check whether the values that you are about to send are set or not and submit button is clicked. 您必须检查是否设置了要发送的值,然后单击“提交”按钮。

Sample Form (form.php) 样本表格 (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>

Callback Page (submit.php) 回调页面 (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
}
?>

Alright, so - the problem is that you're not checking wether or not the fields has been filled, so we're doing that and if they haven't filled out all fields, redirect them back to the contact page with something telling why it made an error. 好吧,所以 - 问题是你没有检查字段是否已被填充,所以我们这样做,如果他们没有填写所有字段,将它们重定向回联系页面,告诉他们为什么它犯了一个错误。 Additionally, we're storing the fields temporarily to avoid issues with users have to fill out all fields again, in case they did fill some out. 此外,我们暂时存储这些字段以避免用户必须再次填写所有字段的问题,以防他们确实填写了一些字段。

<?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>

Now, that being done, we need to consider the contact page again, because now we need to tell them that an error occurred and fill out the contact fields again, for those fields thet did fill. 现在,完成后,我们需要再次考虑联系页面,因为现在我们需要告诉他们发生了错误并再次填写联系人字段,因为那些字段确实已填写。

<?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>

I hope this can give you an idea of some workflow and combining with a little user experience. 我希望这可以让您了解一些工作流程并结合一点用户体验。

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

I did not include $email_message because it won't be empty from the start since you're assembling it above. 我没有包含$ email_message,因为它从一开始就不会是空的,因为你在上面组装它。

$subject = 'Subject line for emails'; $ subject ='电子邮件的主题行';

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

$message = "Your Message"\\n\\r"; $ message =“你的消息”\\ n \\ r“;

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

if(!$sentMail) { if(!$ sentMail){

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

} else { } else {

echo "Thank you for enquiry. We will contact you shortly !"; echo“感谢您的垂询。我们会尽快与您联系!”;

} }

You need to check first message field is empty or not,its client site check if you press submit without message it never submit your form, 您需要检查第一个消息字段是否为空,其客户端站点检查您是否按下提交而没有消息它从不提交您的表单,

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

and

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM