PHP电子邮件表单每次刷新页面时都会发送电子邮件 [英] PHP email form sends email everytime page is refreshed

查看:144
本文介绍了PHP电子邮件表单每次刷新页面时都会发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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'已发送消息';
}
}


解决方案

使用如果(!$ mail-> Send()){/>

  
header(Location:http://www.example.com);
退出;
}

如果这不适合您,请使用元刷新方法:如果(!$ mail-> Send()){
$ to =http://www.example .COM;
$ url = $ to;
print< meta HTTP-EQUIV = Refresh CONTENT = \0; URL = $ url\ > 中;
退出;
}

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

  if(!$ mail-> Send()){
$ to =http://www.example.com;
$ url = $ to;
print< meta HTTP-EQUIV = Refresh CONTENT = \5; URL = $ url\ > 中;
打印谢谢你的消息。
退出;
}






编辑: (cookie / token method)



您可以使用cookie,这只是一个例子。

 <?php 

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

if(isset($ _ POST ['submit'])){

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

$ error_list。='< li>您不能提交此表单两次。< / li>';

echo $ error_list;

echo'
谢谢你的信息已发送。您不需要重新提交。
';

退出;

}

foreach($ _POST as $ values){$ data。= $ values。 <峰; br> 中; echo $ data; }

}

?>

< form action =method =POST>

名称:< input type =textname =name>
< br>
电子邮件:< input type =textname =email>
< input type =hiddenname =tokenvalue =<?php echo $ token;?> />

< input type =submitvalue =Submitname =submit/>
< / form>


my php email for is sending emails every time the page is refreshed. For example the user is filling out the form and sending it with the send button. That's all fine and good but if they refresh the page it sends the email again with all the same form info.

I believe this is the problem code but don't know what it is.

    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';
    }
   }

解决方案

Use a header instead and make sure you have no output before header.

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

If that does not work for you, use a meta refresh method:

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

or display a message and redirect after 5 seconds:

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


Edit: (cookie/token method)

You can use a cookie and this is just an example.

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

这篇关于PHP电子邮件表单每次刷新页面时都会发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆