每次刷新页面时,Php邮件功能都会发送电子邮件

时间:2017-06-28 08:08:48

标签: php email

我使用邮件功能来做联系表格,一切正常。但是,我发现在发送电子邮件后,每次刷新页面时,即使字段为空,也会发送电子邮件。

我的代码如下所示:

public class MyJob implements org.quartz.Job {

      public MyJob() {
      }

      public void execute(JobExecutionContext context) throws JobExecutionException {
          System.err.println("Hello World!  MyJob is executing.");
      }
}

  JobDetail job = newJob(MyJob.class)
      .withIdentity("job1", "group1")
      .build();

  // Trigger the job to run now, and then repeat every 40 seconds
  Trigger trigger = newTrigger()
      .withIdentity("trigger1", "group1")
      .startNow()
      .withSchedule(simpleSchedule()
              .withIntervalInSeconds(40)
              .repeatForever())
      .build();

  // Tell quartz to schedule the job using our trigger
  scheduler.scheduleJob(job, trigger);

如果发送电子邮件后,如何让网站忘记输入字段中的所有详细信息?

我尝试关注此问题here,但我似乎无法在我的网站上发挥作用

7 个答案:

答案 0 :(得分:1)

最简单的方法是在代码中添加转发,例如:

编辑:@ CD001评论

if (mail($to,$subject,$body,$headers)) {
  $result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
  echo $result;
  // header('Location: ?successfull-submit'); exit; // this one would fail because above is an output.
  echo '<meta http-equiv="refresh" content="0; url=?successfull-submit">'; // its not a good /nice alternative but it "works".

答案 1 :(得分:1)

重定向到?sent = 1而不发送任何输出。并检查发送的&#39;确定是否显示成功消息。请尝试以下(假设您的脚本是contact.php)。还要确保

contact.php

<?php
    $result = '';

    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $mobile = $_POST['mobile'];
        $subject_line = $_POST['subject'];
        $situation = $_POST['situation'];
        $from = 'myemail@email.co.za'; 
        $to = 'myemail@email.co.za'; 
        $subject = 'SchoemanLaw lead ...';

        $body ="From: $name <br/> E-Mail: $email <br/> Mobile:  $mobile Subject: $subject_line <br/> Situation: $situation";

        //$body ="From: $name\r\n E-Mail: $email\r\n Mobile:\r\n $mobile Subject: $subject_line\r\n Situation:\r\n $situation";

        // set content-type when sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

        // More headers optional/headers 
        $headers .= "From:$from";

         // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

         // Check if mobile has been entered
        if (!$_POST['mobile']) {
            $errMobile = 'Please enter your number';
        }

        // If there are no errors, send the email
        if (!$errName && !$errEmail && !$errMobile) {
            if (mail($to,$subject,$body,$headers)) {
                //$result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
                //echo $result;
                header('Location:' . 'contact.php?sent=1');
                exit;
            } else {
                $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
                //echo $result;
            }
        }
    }

    if(isset($_GET['sent'])) {
        $result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
    }   

    echo $result;
?>


<form role="form" method="POST">
    <br style="clear:both">
    <h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
    <div class="form-group">
    <input type="text" class="form-control" id="name" name="name" placeholder="Name" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="email" name="email" placeholder="Email" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
    </div>
    <div class="form-group">
    <input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
    </div>
    <div class="form-group">
        <select name="situation" id="situation">
            <option>Select Current Situation</option>
          <option class="placeholder" value="Unemployed">Unemployed</option>
          <option class="placeholder" value="Employed">Employed</option>
        </select>
    </div>
    <button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>

 </form>

答案 2 :(得分:0)

您可以使用会话变量并在每次请求后重置它。

或者,使用javascript阻止重新加载同一页面。

或者,完成后重定向到其他页面(如果可能)

答案 3 :(得分:0)

尝试这样,在你的成功消息部分..

<?php
        if (isset($_POST["submit"])) {
            $name = $_POST['name'];
            $email = $_POST['email'];
            $mobile = $_POST['mobile'];
            $subject_line = $_POST['subject'];
            $situation = $_POST['situation'];
            $from = 'myemail@email.co.za'; 
            $to = 'myemail@email.co.za'; 
            $subject = 'SchoemanLaw lead ...';

            $body ="From: $name <br/> E-Mail: $email <br/> Mobile:  $mobile Subject: $subject_line <br/> Situation: $situation";

            //$body ="From: $name\r\n E-Mail: $email\r\n Mobile:\r\n $mobile Subject: $subject_line\r\n Situation:\r\n $situation";

            // set content-type when sending HTML email
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

            // More headers optional/headers 
            $headers .= "From:$from";

             // Check if name has been entered
            if (!$_POST['name']) {
                $errName = 'Please enter your name';
            }

            // Check if email has been entered and is valid
            if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
                $errEmail = 'Please enter a valid email address';
            }

             // Check if mobile has been entered
            if (!$_POST['mobile']) {
                $errMobile = 'Please enter your number';
            }

            // If there are no errors, send the email
            if (!$errName && !$errEmail && !$errMobile) {
                if (mail($to,$subject,$body,$headers)) {
                     echo "<script>alert('Mail sent Successfully');</script>";
                     echo "<script>window.location = 'contact.php';</script>";
                } else {
                    echo "<script>alert('Mail not sent');</script>";
    echo "<script>window.location = 'contact.php';</script>";
                }
            }
        }   
    ?>

在重定向到另一个页面时,您可以限制该重复问题....

答案 4 :(得分:0)

只需尝试重定向到另一个页面或另一个丢弃旧的$ _POST数据的函数。

答案 5 :(得分:0)

您可以尝试在页面中添加CSRF token以防止重复提交。 请参阅此链接:How to prevent multiple form submission on multiple clicks in PHP

答案 6 :(得分:0)

当用户刷新页面时,可能会再次发布相同的参数。因此,if (isset($_POST["submit"]))此条件成立,每次用户重新加载时都会发送邮件。

一种解决方案是在成功完成时重定向到同一页面或不同的页面。

即,

if (mail($to,$subject,$body,$headers)) {
                $result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
                echo $result;
            } 

而不是上述方法,将用户重定向到同一页面或不同页面并在那里显示消息。如果要显示同一页面,可以使用查询字符串中的标志重定向为“show_success_msg = true”。

然后这样做。

if(isset($_GET['show_success_msg']) && $_GET['show_success_msg'] == 'true') {
  $result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
  echo $result;
}

完整的解决方案:

<?php
    // Handle PHP code always on Top of the page (ie, Not after your HTML), You cant send headers if you have any content above it.

    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $mobile = $_POST['mobile'];
        $subject_line = $_POST['subject'];
        $situation = $_POST['situation'];
        $from = 'myemail@email.co.za'; 
        $to = 'myemail@email.co.za'; 
        $subject = 'SchoemanLaw lead ...';

        $body ="From: $name <br/> E-Mail: $email <br/> Mobile:  $mobile Subject: $subject_line <br/> Situation: $situation";

        //$body ="From: $name\r\n E-Mail: $email\r\n Mobile:\r\n $mobile Subject: $subject_line\r\n Situation:\r\n $situation";

        // set content-type when sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

        // More headers optional/headers 
        $headers .= "From:$from";

         // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

         // Check if mobile has been entered
        if (!$_POST['mobile']) {
            $errMobile = 'Please enter your number';
        }

        // If there are no errors, send the email
        if (!$errName && !$errEmail && !$errMobile) {
            if (mail($to,$subject,$body,$headers)) {
                $result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
                echo $result;
            } else {
                header("Location: your_page.php?show_success_msg=true")
            }
        }
    }   
?>
<form role="form" method="POST">
<?php if(isset($_GET['show_success_msg']) && $_GET['show_success_msg'] == 
'true') {
$result='<div class="alert alert-success">Thank You ! We will be in touch 
soon</div>';
echo $result;
} ?>

<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
    <select name="situation" id="situation">
        <option>Select Current Situation</option>
      <option class="placeholder" value="Unemployed">Unemployed</option>
      <option class="placeholder" value="Employed">Employed</option>
    </select>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>