PHP email form sends email everytime page is refreshed

一世执手 提交于 2019-12-01 14:44:57

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>

try this-

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

the mail sending function was outside of the if condition with $_POST check. So it was sending mail everytime it is refreshed.

You should use different files, at different URLs to handle the actions:

  • send email
  • display confirmation page

For example:

In your form

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

In sendmail.php

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

See the doc for details.

In success.php

<p>Oh yeah ;)</p>

In error.php

<p>Ooops :(</p>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!