Viewing 2 replies - 1 through 2 (of 2 total)
  • 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)

    Oops, I didn’t comprehend the whole question on my first reply, you mentioned three fields for the sender and the recipient! In which case, we just expand upon the middle bit like so:

    // Get the sender email address from the post via ACF
    $sender_email = trim(sanitize_email(get_field('kontaktformular-empfaenger-mail', $post_id)));

    // Get the sender's name from the post via ACF
    $sender_name = trim(sanitize_text_field(get_field('kontaktformular-absender-name', $post_id)));

    // Get the recipient email address from the post via ACF
    $recipient_email = trim(sanitize_email(get_field('kontaktformular-absender-mail', $post_id)));

    // Bail if either email address is missing
    if(!$sender_email || !$recipient_email) return;

    // Access the mail properties
    $mail_1 = $contact_form->prop('mail');
    $mail_2 = $contact_form->prop('mail_2'); // Optional, only if you enabled it

    // Change the sender's email to your custom value, formatted nicely if name is set
    $mail_1['sender'] = $sender_name ? sprintf('"%s" <%s>', esc_attr($sender_name), esc_attr($sender_email)) : $sender_email;
    $mail_2['sender'] = $mail_1['sender']; // Optional, only if you enabled it

    // Change the recipient's email to the custom value
    $mail_1['recipient'] = $recipient_email;
    $mail_2['recipient'] = $recipient_email; // Optional, only if you enabled it

    // Update the mail object with the modified values
    $contact_form->set_properties(array(
    'mail' => $mail_1,
    'mail_2' => $mail_2 // Optional, only if you enabled it
    ));

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.