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
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.
The email body is in the class
WPCF7_ContactForm
which is passed to the hookwpcf7_before_send_mail
Use the method
prop
to accessmail
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.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.