• Resolved facem Web

    (@shojikama)


    Hi there,

    I’m actually coding a page with a mail sending.
    It’s a destination page for users after a payement with bank portal.
    I need to send an email to the admin as a confirmation that the payment is achieved or not. But for now, the mail is sent two times. Here’s the code of the page :

    <?php
    session_start();
    $nom=$_SESSION['nom'];
    
    require_once('Mercanet.php');
    	
    // Initialisation de la classe Mercanet avec passage en parametre de la cle secrete
    $paymentResponse = new Mercanet('002001000000001_KEY1');
    $paymentResponse->setResponse($_POST);
    ?>
    
    <?php $mts_options = get_option(MTS_THEME_NAME); ?>
    <?php get_header(); ?>
    <div id="page" class="single">
      <div class="container">
      	<article class="<?php mts_article_class(); ?>">
          	<div id="content_box" class="panel">
          		<?php
    				if($paymentResponse->isValid() && $paymentResponse->isSuccessful()){
    			        // Traitement pour les paiements valides
    					echo "paiement reussi ";
    					$emailTo = '[email protected]';
    				    $subject = 'Paiement de la facture';
    				    /*$sendCopy = trim($_POST['sendCopy']);*/
    				    $body = "paiement accepté $nom";
    				    $headers .= "Content-Type: text/plain; charset=UTF-8\n";
    				    $headers .= "Content-Transfer-Encoding: 8bit\n";
    			    }
    			    else{
    			        // Traitement pour les paiements en echec
    					echo "paiement en echec ";
    					$emailTo = '[email protected]';
    				    $subject = 'Paiement de la facture';
    				    /*$sendCopy = trim($_POST['sendCopy']);*/
    				    $body = "paiement echec $nom";
    				    $headers .= "Content-Type: text/plain; charset=UTF-8\n";
    				    $headers .= "Content-Transfer-Encoding: 8bit\n";
          			}
          			if (is_page()) {
          				wp_mail($emailTo, $subject, $body, $headers);
          			}
          		?>
          	</div>
      	</article>
    </div>
    </div>
    
    <?php get_footer(); 
    session_destroy();
    ?>

    The body of the mail is “paiement echec $nom”. One mail is getting the $nom variable stocked in the session but not the other.

    If you have some clues it’ll be great.
    Thanks !

Viewing 8 replies - 1 through 8 (of 8 total)
  • Are you able to provide me a link where I can have a look?

    Thread Starter facem Web

    (@shojikama)

    Hi, thanks for the answer.

    For seeing that page, you have to follow the payment process, but you can cancel the payment once you’re in the bank portal, it will redirect you to the page where the mails are sent.

    https://www.piscineetjardin.com/payer-sa-facture/

    You mean https://www.piscineetjardin.com/retour-paiement/ is the page which sends email? I’ve been redirected to that page once I canceled the payment on bank portal.

    Thread Starter facem Web

    (@shojikama)

    yes it’s that page, I’ve recieved the twice mails some minutes ago.

    Thread Starter facem Web

    (@shojikama)

    Maybe the plage is loaded twice. One time without the session variable and one with it. But I don’t know how to sned the mail when the page is fully loaded.

    Could you try the following code?

    <?php
    session_start();
    $nom = isset($_SESSION['nom']) ? $_SESSION['nom'] : '';
    
    require_once('Mercanet.php');
    	
    // Initialisation de la classe Mercanet avec passage en parametre de la cle secrete
    $paymentResponse = new Mercanet('002001000000001_KEY1');
    $paymentResponse->setResponse($_POST);
    ?>
    
    <?php $mts_options = get_option(MTS_THEME_NAME); ?>
    <?php get_header(); ?>
    <div id="page" class="single">
      <div class="container">
      	<article class="<?php mts_article_class(); ?>">
          	<div id="content_box" class="panel">
          		<?php
    				if($paymentResponse->isValid() && $paymentResponse->isSuccessful()){
    			        // Traitement pour les paiements valides
    					echo "paiement reussi ";
    					$emailTo = '[email protected]';
    				    $subject = 'Paiement de la facture';
    				    /*$sendCopy = trim($_POST['sendCopy']);*/
    				    $body = "paiement accepté $nom";
    				    $headers .= "Content-Type: text/plain; charset=UTF-8\n";
    				    $headers .= "Content-Transfer-Encoding: 8bit\n";
    			    }
    			    else{
    			        // Traitement pour les paiements en echec
    					echo "paiement en echec ";
    					$emailTo = '[email protected]';
    				    $subject = 'Paiement de la facture';
    				    /*$sendCopy = trim($_POST['sendCopy']);*/
    				    $body = "paiement echec $nom";
    				    $headers .= "Content-Type: text/plain; charset=UTF-8\n";
    				    $headers .= "Content-Transfer-Encoding: 8bit\n";
          			}
          			if (is_page() && $nom != '') {
          				wp_mail($emailTo, $subject, $body, $headers);
          			}
          		?>
          	</div>
      	</article>
    </div>
    </div>
    
    <?php get_footer(); 
    session_destroy();
    ?>

    Hopefully, this should send a single email only.

    Thread Starter facem Web

    (@shojikama)

    Dude it worked !

    The $nom variable verification is simple and smart, I searched too complicated solution.
    Anyway, I had to unset the is_page() in the if, the $nom != ” is enought to block the twice sending.

    Thanks a lot !

    Glad I could help ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘wp_mail() sending twice’ is closed to new replies.