I have a code that send an email but how I can prevent it from always sending everytime my page refreshes.

function two_dim_array_to_html_table($arr, $colcomments){
    $ret = "<table border='1' width='auto' cellpadding='1px' cellspacing='0px' align='center'>\n";
$ret .= "\t<tr>\n";
    foreach($arr[0] as $key => $val){
        $ret .= "\t\t<th>".$colcomments[$key]."</th>\n";
    $ret .= "\t</tr>\n";
    foreach($arr as $row){
        $ret .= "\t<tr>\n";
        foreach($row as $column){
            $ret .= "\t\t<td>".$column."</td>\n";
        $ret .= "\t</tr>\n";
    $ret .= "<table>\n";
    return $ret;

    if($result) {
        $Body = "<html>\n"
            . "<head>\n"
. "</head>\n"
            . "<body>\n"
. two_dim_array_to_html_table($result, $colcomments)
            . "</body>\n"
. "</html>\n";
//Setting up Mail
        $mail = new PHPMailer();
        if (EMAIL_USE_SMTP) {
            // Set mailer to use SMTP
            //useful for debugging, shows full SMTP errors
            //$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
            // Enable SMTP authentication
            $mail->SMTPAuth = EMAIL_SMTP_AUTH;
            // Enable encryption, usually SSL/TLS
            if (defined(EMAIL_SMTP_ENCRYPTION)) {
                $mail->SMTPSecure = EMAIL_SMTP_ENCRYPTION;
            // Specify host server
            $mail->Host = EMAIL_SMTP_HOST;
            $mail->Username = EMAIL_SMTP_USERNAME;
            $mail->Password = EMAIL_SMTP_PASSWORD;
            $mail->Port = EMAIL_SMTP_PORT;
        } else {
        $mail->From = EMAIL_FROM_ADDRESS;
        $mail->FromName = EMAIL_FROM_NAME;
        $mail->Subject = 'Daily MW ITSM Tasks - "'.date('d-m-Y h:m:s').'"';
        $mail->WordWrap = 100;
        $mail->Body = $Body;

I tried using this but it does not work,

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

Recommended Answers

All 4 Replies

It all depends on how the mailing is triggered. If, for example, a button is clicked which creates POST data to this page with a value like sendmail=yes and the code checks for that and sends the mail if and only if that happens, you can append a random number created by PHP instead of yes. A positive value for sendmail means send the mail. A negative or 0 or unset value does not.

So you create a button using PHP and create a random value of, say 42689 for sendmail. The user clicks the button and sendmail=42689 is sent as POST data. You check that, see that 42689 is positive, and on line 55, send that email, as you do.

Now how do you make sure it doesn't RE-send on refresh?

Create a SESSION variable called lastsendmailcode. You set that value to 42689 AFTER line 55 executes.

Now at the top of the page, you now do TWO checks before mailing. You check that sendmail has been POSTed and set to a positive value and you also check the SESSION variable lastsendmailcode. If it has NOT been set or if it is set to some value OTHER than 42689, you send the email. If it's equal to 42689 (or whatever sendmail is set to), you DO NOT send the email.

When creating the button or however you set that sendmail code to legitimately send, you'll set it randomly or in some other way set it so that it does not equal lastsendmailcode.

Google "PHP Session variables" for how to use SESSION variables.

You can also use an ajax REST call.

Member Avatar

An added gotcha is when a submit form "sends to itself", that is the form sends to the file/page that the form is on. This means subsequent refreshes send the data again and again.

As a rule it is safer to post to a form handler (a separate file). This means that the data gets sent to the handler and then rerouted to a different page (e.g. confirmation of sending) or even back to the previous page. The difference here is that refreshing will not result in multiple submits. Just an idea.

You can use browser cookie for same.

1) When page is loaded first set cookie say 'sendmail'to 0 in bottom of page in javascript.
2) on click of button to send mail set cookiet in javascript 'sendmail'to 1, call php mail handler.
3) When php code is executed check cookie value 'sendmail' if its 1 then only send email, otherwise dont send mail

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.