Skip to content
Advertisement

Contact Form 7 – Get final mail HTML output

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)

JavaScript

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>"

Advertisement

Answer

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.

JavaScript

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.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement