skip to Main Content

Is there a hook that will allow me to see exactly what is being sent out to an email?

I’ve tried using 'wpcf7_mail_sent' and all it contains is array of the data and fields.

For example it has "first-name": "John", "last-name": "Smith", ... etc. but not template.

What I want to get is the mail template merged with that data… aka the final email as HTML:

For example: Hello, John Smith! <br> Thanks for contacting us.

Edit: Clarification

(Using the example that @Howard E provided)

add_action( 'wpcf7_before_send_mail', 'dd_handle_form_submission', 10, 3 );
function dd_handle_form_submission( $contact_form,$abort,$submission ) {

    $template = $contact_form->prop('mail')['body'];
    $posted_data = $submission->get_posted_data();

    //Outputs:
    //$template: <h2> First Name: [first-name] </h2>    <h2> Last Name: [last-name] </h2> ...
    //$posted_data: Array ( [first-name] => 'John', [last-name] => 'Smith' ... )
}

That’s the template but with shortcodes… and the data but as an array.

My original question was how do I get the final output of the merged $posted_data + $template so an $html var would look like this:

"<h2> First Name: John </h2> <h2> Last Name: Smith </h2>"

2

Answers


  1. For security reasons contact form 7 doesn’t allow html but using below hook you can get the form data & send email in html format.

    // define the wpcf7_before_send_mail callback 
    function action_wpcf7_before_send_mail( $contact_form ) {
     
        // Use wp_mail() to send an email. To send html in body first add_filter with content type text/html within this function
    
    add_filter('wp_mail_content_type', function( $content_type ) {
                return 'text/html';
    });
    //Send email..... Even you can put php variable values within html
    wp_mail( '[email protected]', 'your_subject', '<div>The message</div>' );
    
    }; 
             
    // add the action 
    add_action( 'wpcf7_before_send_mail', 'action_wpcf7_before_send_mail', 10, 1 ); 
    
    Login or Signup to reply.
  2. The email body is in the class WPCF7_ContactForm which is passed to the hook wpcf7_before_send_mail

    Use the method prop to access mail from there.

    At this point, you can hook the mail and update the output however you want. Use $contact_form->set_properties(array('mail' => $mail)); to update the mail body to whatever you want. There is no need to return the function, as you’re updating the object directly.

    add_action( 'wpcf7_before_send_mail', 'dd_handle_form_submission' );
    function dd_handle_form_submission( $contact_form ) {
        $mail = $contact_form->prop('mail')['body'];
    
        // Output the content to the error log of your website.
        ob_start();
        echo $mail;
        error_log(ob_get_clean());
    
        // Use this to push new content to the mail before sending
        $mail = $new_mail_content // whatever you set it to
        $contact_form->set_properties(array('mail' => $mail));
    
    }
    

    UPDATED Answer:

    To get the content of the email after variable replacement, you have to use the filter wpcf7_mail_components

    $components is an array of the mail components

    ['subject', 'sender', 'body', 'recipient', 'additional_headers', 'attachments']

    Since this function has no output to the screen, you would have to send it to the error log to debug it.

    add_filter('wpcf7_mail_components', 'filter_mail_components', 10, 3);
    function filter_mail_components($components, $current_form, $mail_class){
        ob_start();
        print_r($components['body']);
        error_log(ob_get_clean());
        return $components;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search