The screenshot shows a 404 for me, but I’m imagining it as an email input field on your CPT.
The approach I take is usually to skip submitting the email address through the form and instead accessing it from the wpcf7_before_send_mail
hook.
So in your form template, just completely remove the field named kontaktformular-absender-mail
. In the mail template, use whatever email address will satisfy the form validator.
Then you’ll want to add this custom code snippet to pull the email address from your ACF field and set it as the sender in your mail property.
/**
* Change Sender Email for CF7
*
* @param WPCF7_ContactForm $contact_form The current contact form.
* @param bool $abort If the submission is being aborted.
* @param WPCF7_Submission $submission The current submission data.
*
* @return void
*/
function svenkilcher_cf7_acf_email_sender($contact_form, $abort, $submission) {
// Get the form ID
$form_id = $contact_form->id();
// Do something different for this specific form
if($form_id == 8) {
// Get the current post id
$post_id = intval(sanitize_text_field($_POST['_wpcf7_container_post']));
// Bail if id is invalid
if(!$post_id) return;
// Get the ACF email address from the post (replace FIELD_NAME)
$sender_email = trim(sanitize_email(get_field('FIELD_NAME', $post_id)));
// Bail if email address is invalid
if(!$sender_email) return;
// Access the mail properties
$mail_1 = $contact_form->prop('mail');
$mail_2 = $contact_form->prop('mail_2');
// Change the sender's email to your custom value
$mail_1['sender'] = $sender_email;
$mail_2['sender'] = $sender_email;
// Update the mail object with the modified values
$contact_form->set_properties(array(
'mail' => $mail_1,
'mail_2' => $mail_2
));
}
}
add_action('wpcf7_before_send_mail', 'svenkilcher_cf7_acf_email_sender', 10, 3);
This is my preferred method because then email addresses are protected as they are not revealed on the frontend of the site at all.
(please note that there may be syntax issues as I wrote this code directly in this code editor box)