Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Wordpress) Contact Form 7 abort in before_send_mail filter

I am using Contact Form 7 as a mailing system on my WordPress website. I use the wpcf7_before_send_mail filter to send all the data to an external webservice (SOAP). When I receive a "SUCCESS" message of that webservice, all should continue as normal, but when I receive a "FAILED" message, contact form 7 shouldn't send out an email and a different output message should appear on the website. Is it possible to alter this within the function?

<?

 add_filter( 'wpcf7_load_js', '__return_false' );
 add_filter( 'wpcf7_load_css', '__return_false' );

 add_action('wpcf7_before_send_mail', 'wpcf7_soap_service');


//Pushen via SOAP service naar servers
function wpcf7_soap_service($contact_form) {
$submission = WPCF7_Submission::get_instance();

if ( $submission ) {

/*** POST variabelen ***/    
$posted_data = $submission->get_posted_data();

/*** SOAP settings ***/    
ini_set("soap.wsdl_cache_enabled", "0");

/*** variabelen opzetten ***/
define('BROADCAST_URL','XXX');
define('SIM_LOGIN', 'XXX');
define('SIM_PASSWORD', 'XXX');
define('ENV_KEY', 'XXX');

/*** login parameters ***/
$params = array(
  'username' => SIM_LOGIN,
  'password' => SIM_PASSWORD,
  'environmentKey' => ENV_KEY,
);

/*** client opzetten ***/
$client = new SoapClient(
  BROADCAST_URL,
  array(
    'soap_version' => SOAP_1_1
  )
);

/*** Parameters ***/
$address["box"]           = $posted_data["box"];
$address["country"]       = $posted_data["country"];
$address["number"]        = $posted_data["streetnumber"];
$address["postalcode"]    = $posted_data["postalcode"];
$address["street"]        = $posted_data["street"];
$address["town"]          = $posted_data["town"];

$birthdate                = $posted_data["birthdate"] . "T00:00:00";
$email                    = $posted_data["email"];

$firstname                = $posted_data["firstname"];
$lastname                 = $posted_data["lastname"];
$phone                    = $posted_data["phone"];

/*** STDClass aanmaken met gevraagde data ***/
$std = new stdClass();

$std->Firstname           = $firstname;
$std->Lastname            = $lastname;
$std->Birthdate           = $birthdate;
$std->Phone               = $phone;
$std->Email               = $email;

$std->Address = new stdClass();

$std->Address->Street         = $address["street"];
$std->Address->Number         = $address["number"];
$std->Address->Box            = $address["box"];
$std->Address->PostalCode     = $address["postalcode"];
$std->Address->Town           = $address["town"];
$std->Address->Country        = $address["country"];

if(!empty($_FILES['cv'])){

    $std->Files = new stdClass();

    $std->Files->File["FileName"]     = $_FILES["cv"]["name"];
    $std->Files->File["DataFile"]     = base64_encode($_FILES["cv"]["tmp_name"]);
    $std->Files->File["FileType"]     = "CV";
}

/*** Functie OpenSession ***/
try{
  $token = $client->OpenSession($params);
}catch(SoapFault $ex){
  // ABORT OVER HERE
}

$token                = $token->OpenSessionResult;

/*** Functie AddApplication ***/
try{
  $result = $client->AddApplication(array("token" => $token, "application" => $std));
}catch(SoapFault $ex){
  // ABORT OVER HERE
}

if($result->AddApplicationResult->Status == "Success"){
  // ABORT OVER HERE
}


/*** Functie CloseSession ***/
try{
  $app = $client->CloseSession($token);
}catch(SoapFault $ex){
  // ABORT OVER HERE
}

}
}
like image 301
Warre Buysse Avatar asked Sep 26 '22 16:09

Warre Buysse


1 Answers

You can skip the mail by using:

add_filter( 'wpcf7_skip_mail',  '__return_true' );

Since you've disabled the javascript with

add_filter( 'wpcf7_load_js', '__return_false' );

then you can use:

add_filter( 'wpcf7_form_response_output', 'wpse_form_response_output', 10, 4 );

within your wpcf7_before_send_mail action callback, where your custom error response is:

function wpse_form_response_output( $output, $class, $content, $object )
{
    return sprintf(
        '<div class="wpcf7-response-output wpcf7-display-none wpcf7-mail-sent-ng" 
            role="alert" style="display: block;">%s</div>',
        __( 'SOAP ERROR - Mail not sent!' )
    );
}
like image 160
birgire Avatar answered Nov 15 '22 04:11

birgire