-3

Hi! Why it's doesn't work?When i refresh page, website automatically send again and again same empty email. I know how can i fix it,but it doesn't work now, and idk why.

<?php
        $to = 'email@email.hu';
        $from = $_POST['email'];
        $subject = "XXXXXXX";

            $name= $_POST['name'];
        //
        $htmlContent = '
        <html>
        <body>
        <center>
            <table rules="all" style="border-color: #666;" cellpadding="10" width="40% text-align: center;">
                <tr style="background-color: #43464b; color: white;"><td><strong>name:</strong> </td></td>' . $name. '</td></tr>
            </table>
            </center>
        </body>
        </html>';


        // Set content-type header for sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

        // Additional headers
        $headers .= 'From: Webnév<web@web.hu>' . "\r\n";
        $headers .= 'Cc: welcome@example.com' . "\r\n";
        $headers .= 'Bcc: welcome2@example.com' . "\r\n";

        // Send email
        if(mail($to,$subject,$htmlContent,$headers)): 
        header("Location: " . $_SERVER['REQUEST_URI'] . "?");
        ?>
  • 2
    Your script send again and again the email because you got an redirect in the last line. Remove the last line and the mail would only send once. – Richard May 17 '19 at 17:48
  • What do you expect `header("Location: " . $_SERVER['REQUEST_URI'] . "?")` to do if not load the page again and execute the code again? – David May 17 '19 at 17:52
  • When i press F5 i get automatically new email from website. ( https://imgur.com/a/lBV65hH )( https://imgur.com/a/vOYLUsq ) And i want to fix it. If i press f5 or reload page i dont need / i dont want empty email. – Martin Andrejkovics May 17 '19 at 19:55
  • 1
    Possible duplicate of [Understanding the "post/redirect/get" pattern](https://stackoverflow.com/questions/10827242/understanding-the-post-redirect-get-pattern) – Progman May 17 '19 at 21:24
  • Also possible duplicate of https://stackoverflow.com/questions/6320113/how-to-prevent-form-resubmission-when-page-is-refreshed-f5-ctrlr – Progman May 17 '19 at 21:25
  • `$_POST['email']` is not set when you refresh the page for a 2nd time... – Alessandro May 18 '19 at 13:29
  • `header("Location: " . $_SERVER['REQUEST_URI'] . "?");` reloads the current page again, please change with: `header("Location: thanks.php");` for example... – Alessandro May 18 '19 at 13:32
  • `if (isset($_POST['submit'])) {` then send an email... – Alessandro May 18 '19 at 13:35
  • `if(mail($to,$subject,$htmlContent,$headers)): header("Location: " . $_SERVER['REQUEST_URI'] . "?");` is wrong... use: `mail($to,$subject,$htmlContent,$headers); header("Location: thanks.php");` – Alessandro May 18 '19 at 13:37

1 Answers1

1

You can find the correct code for sending your mail here, I corrected all errors that I found into your script...

1) you are getting several email with empty values because you when you invoke your script for the 2nd time the $_POST values are empty (fixed)

2) you are getting several email because at the end you invoke your script for a 2nd time using header("Location: " . $_SERVER['REQUEST_URI'] . "?"); (fixed)

3) you are getting several email because you haven't provided any check before send an email if (isset($_POST['submit'])) { is missing... (fixed)

4) you have some syntax errors that can be solved in a simple way... (fixed)

  <?php
    $to = 'email@email.hu';
    if (isset($_POST['email'])) {
      $from = $_POST['email'];
    } else {
      exit("Provide a valid email address!");
    }
    $subject = "XXXXXXX";
    if (isset($_POST['name'])) {
      $name = $_POST['name'];
    } else {
      exit("Provide a valid name!");
    }
    $htmlContent = '
    <html>
    <body>
    <center>
        <table rules="all" style="border-color: #666;" cellpadding="10" width="40% text-align: center;">
            <tr style="background-color: #43464b; color: white;"><td><strong>name:</strong> </td></td>' . $name. '</td></tr>
        </table>
        </center>
    </body>
    </html>';
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= 'From: Webnév<web@web.hu>' . "\r\n";
    $headers .= 'Cc: welcome@example.com' . "\r\n";
    $headers .= 'Bcc: welcome2@example.com' . "\r\n";
    if (isset($_POST['submit'])) {
      if (mail($to,$subject,$htmlContent,$headers)) {
        header("Location: thanks.php");
      } else {
        header("Location: sorry.php");
      }
    }
    ?>

Not tested, written on fly... I hope this helps.

Alessandro
  • 900
  • 12
  • 23