Search code examples
phpwordpresspluginsmailer

wp_mail is sending messages with same data


i've a problem with wp_mail() function. It sends me messages correctly but after calling it is sending me multiple messages with the same data, how should I call it properly or kill process to send it only once ?

Here is how i call wp_mail()

        // confirms reservation - doesn't matter for wp_mail()
        $CLASS->ConfirmReservation($data);

        // it gets all updated data about current reservation
        $info = $CLASS->GetReservationInfo($_POST['operation_number'])[0];

        // it prepares me array with parameters: to, subject, msg
        $msgHTML = $CLASS->PrepareOrderHTMLmsg($info);

        // adding headers to array
        $msgHTML['headers'] = array('Content-Type: text/html; charset=UTF-8','From: '.get_bloginfo('name').' <'.get_bloginfo('admin_email').'>');

        // send e-mail
        wp_mail($msgHTML['send_to'], $msgHTML['subject'], $msgHTML['msg'] , $msgHTML['headers']);

Solution

  • From some other page I just send data colected by FORM through simple POST to payments API , and as a respond i get also some POST data for defined url which calls functions from below

    function DC_Shortcode(){
            ob_start();
            isset($_POST) ? DotpayCallback() : '';
            return ob_get_clean();
        }
        add_shortcode('DCallback', 'DC_Shortcode');
    
    // DOTPAY
        function DotpayCallback() {
            $CLASS= new CLASS();
            $dotpaySettings = $BOANERGES->getDotpaySettings();
    
    
            $PIN = $dotpaySettings[0]['pin_setting'];
            $verify = 1;        //verify data and save, requires valid PIN (above)
    
            if($verify)
            {
                if($_SERVER['REQUEST_METHOD'] != 'POST') //URLC always uses POST
                    die($_SERVER['REQUEST_METHOD']." is incorrect request method");
    
                if($_SERVER['REMOTE_ADDR'] != '195.150.9.37') //Dotpay IP for URLC is always 195.150.9.37
                    die("Unexpected IP: ".$_SERVER['REMOTE_ADDR']);
    
                if(strlen($_POST['signature']) != '64') //signature always has 64 characters
                    die("Invalid POST content");
    
                $sign=
                    $PIN.
                    $_POST['id'].
                    $_POST['operation_number'].
                    $_POST['operation_type'].
                    $_POST['operation_status'].
                    $_POST['operation_amount'].
                    $_POST['operation_currency'].
                    $_POST['operation_withdrawal_amount'].
                    $_POST['operation_commission_amount'].
                    $_POST['operation_original_amount'].
                    $_POST['operation_original_currency'].
                    $_POST['operation_datetime'].
                    $_POST['operation_related_number'].
                    $_POST['control'].
                    $_POST['description'].
                    $_POST['email'].
                    $_POST['p_info'].
                    $_POST['p_email'].
                    $_POST['credit_card_issuer_identification_number'].
                    $_POST['credit_card_masked_number'].
                    $_POST['credit_card_brand_codename'].
                    $_POST['credit_card_brand_code'].
                    $_POST['credit_card_id'].
                    $_POST['channel'].
                    $_POST['channel_country'].
                    $_POST['geoip_country'];
                $signature = hash('sha256', $sign);
    
                if($signature != $_POST['signature']) //compare POST signature with calculated one
                    die("Signature mismatch! Expected: ".$signature." Received: ".$_POST['signature']);
            }
    
            if($_POST['operation_status'] == 'completed') {
                $data = array(
                    'id' => $_POST['operation_number'],
                    'control' => $_POST['control'],
                    'cs' => 1
                );
                // confirms reservation
                $CLASS->ConfirmReservation($data);
    
                // it gets all updated data about current reservation
                $info = $CLASS->GetReservationInfo($_POST['operation_number'])[0];
    
                // it prepares me array with parameters to, subject, msg
                $msgHTML = $CLASS->PrepareOrderHTMLmsg($info);
    
                // adding headers to array
                $headers = array('Content-Type: text/html; charset=UTF-8','From: '.get_bloginfo('name').' <'.get_bloginfo('admin_email').'>');
    
                // send e-mail
                wp_mail($msgHTML['send_to'], $msgHTML['subject'], $msgHTML['msg'] , $headers);
                wp_die();
                die();
            }
    
            if($_POST['operation_status'] == 'rejected' || $_POST['operation_status'] == 'fail'){
                $data = array(
                    'id' => $_POST['operation_number'],
                    'control' => $_POST['control'],
                    'cs' => 2
                );
                $CLASS->ConfirmReservation($data);
            }
        }