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.
(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>"
...
The email body is in the classWPCF7_ContactForm
which is passed to the hookwpcf7_before_send_mail
Use the methodprop
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.
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 filterwpcf7_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;
}
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 );
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.