Fortunately, we have onsentok and wpcf7beforesendmail which allow us to run our own custom commands before and after form submission. For example, I recently needed to create a function that needed to dynamically generate an XML file on form submission, before attaching the XML to the Contact Form 7 email. Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time. Austin comments. Arnav did a great job - here is the finished code: // CONTACT FORM 7 removeallfilters ('wpcf7beforesendmail'); addaction( 'wpcf7beforesend. From a layman’s perspective, that “action”, is the mail which is sent. And from a WordPress developer’s perspective, the action in discussion here, is the ‘wpcf7beforesendmail’ or the ‘wpcf7aftersendmail’ hook. Let’s make use of the ‘wpcf7beforesendmail’ hook, to track when a form has been filled. We have onsentok and wpcf7beforesendmail which allow us to run our own custom commands before and after form submission. Onsentok is a JavaScript action hook. By using this hook, you can specify a JavaScript code that you wish to run after the form is successfully submitted.
I have been using Contact Form 7 on a lot of my personal sites, as well as client sites for over a decade now and it’s easy to see why it’s the top plugin in the WordPress repository for ‘contact form’ with over 5 million active installs. It’s easy to customise and comes with a lot of hooks to make developers happy. One such hook I recently used was wpcf7_before_send_mail.

A client had a request to send a contact form’s email notifications to specific email addresses, depending on the ‘enquiry’ dropdown menu. For example – If the enquiry was for Accounts, the email would send the form data to accounts@example.com and so on.
Wpcf7_before_send_mail Return Error
Create a form in Contact Form 7
Start by creating your form and include a dropdown menu with a few options. I have create my enquiry-type dropdown menu with three options:
- General
- Media
- Jobs
If we submitted this form at the moment, the form would be sent to the email address set in the Mail tab in CF7, regardless of which dropdown option is selected. Let’s change that.
Wpcf7_before_send_mail Get Post Id
Dynamically change recipient email
Let’s hook into wpcf7_before_send_mail, get the submitted form data, find the enquiry-type dropdown option and then use an if/else statement to send the notifications to specific email addresses.
And there you have it, with a few lines of code we can dynamically send email notifications to specific email addresses – Very professional.

Wpcf7_before_send_mail Get Form Data
1. Open the CF7 contact form which you want to edit in wordpress Admin area. You set up the [checkbox] snippet which produces the checkbox in the contact form on your website: |
[checkbox contact_sendmail 'Send me a copy of this message'] |
2. Activate Mail(2) |
3. Add to functions.php |
function check_mail_send_contactform($cf7) |
{ |
$submission = WPCF7_Submission::get_instance(); |
if ( $submission ) |
{ |
$posted_data = $submission->get_posted_data(); |
} |
$mail = $cf7->prop( 'mail' ); |
if(!empty($posted_data['contact_sendmail'][0])) |
{ |
$mail2 = $cf7->prop( 'mail_2' ); |
$mail2['recipient'] = $posted_data['your-email']; |
$cf7->set_properties( array( 'mail_2' => $mail2 ) ); |
} |
else { |
$cf7->set_properties( array( 'mail_2' => array() )); |
} |
return $cf7; |
} |
add_action('wpcf7_before_send_mail','check_mail_send_contactform'); |
Wpcf7_before_send_mail Skip Mail
