PHP电子邮件表单会在每次刷新页面时发送电子邮件

时间:2014-11-05 04:07:16

标签: php forms email refresh

我的php电子邮件是每次刷新页面时发送电子邮件。例如,用户填写表单并使用发送按钮发送。这一切都很好,但如果他们刷新页面,它会再次发送电子邮件,其中包含所有相同的表单信息。

我相信这是问题代码,但不知道它是什么。

    require_once('class.phpmailer.php');
    if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $subject = 'WebForm';
    $email = $_POST['email'];
    $body = $_POST['message'];
    $mail = new PHPMailer;
    // $mail->SMTPDebug = 2;
    // print_r($_POST);
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "tls";
    $mail->Host = "smtp.office365.com";
    $mail->Port = 587;
    $mail->Username = "person@emailaddy.com";
    $mail->Password = "password";

    $mailto = "person@emailaddy.com";
    $mailfrom = "person@emailaddy.com";
    $mail->SetFrom($mailto, '');
    // $mail->AddReplyTo($mailfrom, 'email');
    $address = 'person@emailaddy.com';
    $mail->AddAddress($address, "My Addy");

    $mail->Subject  = $subject;
    $mail->AltBody  = $body;
    $mail->MsgHTML($body);

    if(!$mail->Send()) {
    echo 'Message has been sent';
    }
   }

3 个答案:

答案 0 :(得分:3)

改为使用标题,确保标题前没有输出。

if(!$mail->Send()) {
    header("Location: http://www.example.com");
    exit;
    }

如果这对您不起作用,请使用元刷新方法:

if(!$mail->Send()) {
$to = "http://www.example.com";
    $url = $to;
    print "<meta HTTP-EQUIV=Refresh CONTENT=\"0; URL=$url\">";
    exit;
}

或在5秒后显示消息并重定向:

if(!$mail->Send()) {
$to = "http://www.example.com";
    $url = $to;
    print "<meta HTTP-EQUIV=Refresh CONTENT=\"5; URL=$url\">";
    print "Thank you for your message.";
    exit;
}

编辑:(Cookie /令牌方法)

您可以使用Cookie,这只是一个示例。

<?php

$token = time();
setcookie('formToken', $token, time() + 3600);

if(isset($_POST['submit'])){

if($_POST['token'] != $_COOKIE['formToken']){
// die("Sorry");

$error_list .= '<li>You can not submit this form twice.</li>';

echo $error_list;

echo '
Thank you, your message has been sent. You do not need resubmit it again.
';

exit;

}

    foreach( $_POST as $values ) { $data .= $values . "<br>"; echo $data; }

}

?>

<form action="" method="POST">  

Name: <input type="text" name="name">
<br>
Email: <input type="text" name="email">
<input type="hidden" name="token" value="<?php echo $token; ?>" />

<input type="submit" value="Submit" name="submit" />
</form>

答案 1 :(得分:0)

试试这个 -

require_once('class.phpmailer.php');
if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $subject = 'WebForm';
    $email = $_POST['email'];
    $body = $_POST['message'];
    $mail = new PHPMailer;
    // $mail->SMTPDebug = 2;
    // print_r($_POST);
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "tls";
    $mail->Host = "smtp.office365.com";
    $mail->Port = 587;
    $mail->Username = "person@emailaddy.com";
    $mail->Password = "password";

    $mailto = "person@emailaddy.com";
    $mailfrom = "person@emailaddy.com";
    $mail->SetFrom($mailto, '');
    // $mail->AddReplyTo($mailfrom, 'email');
    $address = 'person@emailaddy.com';
    $mail->AddAddress($address, "My Addy");

    $mail->Subject  = $subject;
    $mail->AltBody  = $body;
    $mail->MsgHTML($body);
    if(!$mail->Send()) {
        echo 'Message has been sent';
    }
}

mail发送功能超出了$_POST检查的if条件。所以它每次刷新时都会发送邮件。

答案 2 :(得分:0)

您应该在不同的URL处使用不同的文件来处理操作:

  • 发送电子邮件
  • 显示确认页面

例如:

以您的形式

<form ... method="post" action="/sendmail.php">

在sendmail.php

// same code as above except:
if(!$mail->Send()) {
    header("Location: success.php");
} else {
    header("Location: error.php");
}

See the doc for details

成功.php

<p>Oh yeah ;)</p>

在error.php中

<p>Ooops :(</p>