We are running a competition whereby we need to send emails out to email addresses that are entered on a form. I’m using Contact Form 7 to do this.
I thought this would be straightforward but the form will not accept the syntax of:
[friend1-email], [friend2-email], [friend3-email], [friend4-email], [friend5-email]
In the To or Bcc field.
I think the solution might be to just hard-code a value in the To field and then override it with a hook, but I can’t work out what that filter/hook might be.
Any help appreciated.
1 Answer
No need to write any code, Contact form 7 has features of Additional Headers in the Mail
section. In that you just need to write the email’s header inside the Additional headers textbox in Mail
(Second Tab) section.
Put this inside the Additional Headers textbox.
Cc: [friend1-email], [friend2-email], [friend3-email], [friend4-email], [friend5-email]
OR
You can alter the email header data by hooks wpcf7_before_send_mail
try below code.
add_action('wpcf7_before_send_mail','dynamic_addcc');
function dynamic_addcc($WPCF7_ContactForm){
// Check contact form id.
if (33 == $WPCF7_ContactForm->id()) {
$currentformInstance = WPCF7_ContactForm::get_current();
$contactformsubmition = WPCF7_Submission::get_instance();
if ($contactformsubmition) {
$cc_email = array();
/* -------------- */
// replace with your email field's names
if(is_email($_POST['friend1-email'])){
array_push($cc_email, $_POST['friend1-email']);
}
if(is_email($_POST['friend2-email'])){
array_push($cc_email, $_POST['friend2-email']);
}
/* -------------- */
// saparate all emails by comma.
$cclist = implode(', ',$cc_email);
$data = $contactformsubmition->get_posted_data();
if (empty($data))
return;
$mail = $currentformInstance->prop('mail');
if(!empty($cclist)){
$mail['additional_headers'] = "Cc: $cclist";
}
// Save the email body
$currentformInstance->set_properties(array(
"mail" => $mail
));
// return current cf7 instance
return $currentformInstance;
}
}
}
wpcf7_before_send_mail
hook runs before email send, you can modify the form data.