每次刷新页面时如何解决发送邮件的问题[重复]

This question already has an answer here:

These form work after submission .but after refreshing the page the mail send again and again.

<?php
$to = "abc.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:mithlesh@rightturn.co.in 
";
$header .= "MIME-Version: 1.0
";
$header .= "Content-type: text/html
";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
    echo "Message sent successfully...";
}else {
    echo "Message could not be sent...";
}
?>
</div>

There are multiple ways to handle it.

1) Please write below code after mail sent successfully.

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

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

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

3) You can use a cookie also. To use cookie,you have to create one token which have value of current timestamp and whenever you create cookie with also current timestamp and then compare both variable when page refresh. but cookies has previous timestamp and and token has current timestamp then it will not match.

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

    }

Source

Mostly mail sending happens after particular event or activity just like form-submit or button-click.. you need to design form with relevant fields.. then you can use isset() or !empty() for $_POST array

if(!empty($_POST)){
    $to = "abc.com";
     $subject = "This is subject";
      $message = "<b>This is HTML message.</b>";
     $message .= "<h1>This is headline.</h1>";
     $header = "From:mithlesh@rightturn.co.in 
";
     $header .= "MIME-Version: 1.0
";
     $header .= "Content-type: text/html
";
     $retval = mail ($to,$subject,$message,$header);
     if( $retval == true ) {
        echo "Message sent successfully...";
     }else {
        echo "Message could not be sent...";
     }
}

put this code in starting of your php code outside isset submit button

if(empty($_SESSION['key']) && !isset($_SESSION['key'])){
  $randomkey = rand(0,99999);
  $mykey = $_SESSION['key'] = $randomkey
}

It set Session a unique random key. now in form field take one hidden field

<input type="hidden" name="key" value="<?=$mykey?>">

Now On Submit

First Check Current $mykey Matches With Hidden Variable After Performing Certain Action Unset $mykey So it prevent user adding data multiple times by Refreshing the page.

I hope it Helps... :-)

You need to check submit

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

So your code is :

if (isset($_POST['submit'])) {
    $to = "abc.com";
    $subject = "This is subject";
    $message = "<b>This is HTML message.</b>";
    $message .= "<h1>This is headline.</h1>";
    $header = "From:mithlesh@rightturn.co.in 
";
    $header .= "MIME-Version: 1.0
";
    $header .= "Content-type: text/html
";
    $retval = mail ($to,$subject,$message,$header);
    if( $retval == true ) {
       echo "Message sent successfully...";
    }else {
       echo "Message could not be sent...";
    }
}