I’m still working on this issue, but in the meantime, I have learned about the wpcf7_before_send_mail and wpcf7_mail_components hooks. There might be a way for me to re-send the notification in a loop, swapping out the fields I need.
Here’s a simple example of what I’m trying to accomplish:
A job applicant applies for a job, listing three references. so you would have a field with their name, [applicantName], and the names of their references: [referenceName1], [referenceName2], [referenceName3].
The contact form generates a notification email, like so:
to: [referenceEmail1]
Hi, [referenceName1],
[applicantName] has listed you as a reference.
There are three references in the form. I would need to use hooks to detect a mail notification, then replace all instances of “1” in the mail tags with “2”, then trigger another notification, then replace “1” with “3” and trigger another notification.
This works perfectly, for replacing the fields:
add_action('wpcf7_before_send_mail', 'save_form' );
function save_form( $wpcf7 ) {
$submission = WPCF7_Submission::get_instance();
$mail = $wpcf7->prop('mail');
$mail['recipient'] = str_replace("1]", "2]", $mail['recipient']);
$mail['body'] = str_replace("1]", "2]", $mail['body']);
$wpcf7->set_properties(array("mail" => $mail));
return $wpcf7;
}
Of course, since it’s doing this before sending the mail, it just sends one notification to reference #2, and not three emails to one each.
OK, I got this to work. Here’s how I did it:
add_filter('wpcf7_additional_mail', 'moremails', 10, 2);
function moremails($additional_mail, $contact_form) {
$form_id = $contact_form->posted_data['_wpcf7'];
if ($form_id == 123){
$submission = WPCF7_Submission::get_instance();
$wpcf7 = WPCF7_ContactForm::get_current();
$additional_mail['mail_3'] = $additional_mail['mail_2'];
$additional_mail['mail_4'] = $additional_mail['mail_3'];
$additional_mail['mail_3']['recipient'] = str_replace("1]", "2]", $additional_mail['mail_3']['recipient']);
$additional_mail['mail_3']['body'] = str_replace("1]", "2]", $additional_mail['mail_3']['body']);
$additional_mail['mail_3']['body'] = str_replace("1]", "2]", $additional_mail['mail_3']['body']);
$additional_mail['mail_4']['recipient'] = str_replace("1]", "3]", $additional_mail['mail_4']['recipient']);
$additional_mail['mail_4']['body'] = str_replace("1]", "3]", $additional_mail['mail_4']['body']);
$additional_mail['mail_4']['body'] = str_replace("1]", "3]", $additional_mail['mail_4']['body']);
return $additional_mail;
}
}
I decided to use mail for something else, and mail_2 generates multiple notifications.
I didn’t use a loop, but if you had a lot of emails to send out, you could put this in a loop.