Hi @netschmiede24 I apologize, your thread fell through the cracks a little, so thank you for your patience.
It looks like your request has two goals:
- Pre-select the subject in a select field for the support page
- Determine the email recipient based on the subject without revealing the email address
I like to work backwards for some reason, so I’m going to start with your second goal. For that, I have a blog article that covers this here. For your situation, your code snippet will look like this:
/**
* Custom Email Behavior on Contact Form 7 Form Submission
*
* @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 netschmiede24_before_send_mail($contact_form, &$abort, $submission)
{
// Get the submitted value of the subject from your select field
$subject = strtolower(trim(sanitize_text_field($_POST['your-subject'])));
// Define the allowed subjects and their associated email addresses
$allowed = array(
'general requiry' => '[email protected]',
'support' => '[email protected]',
'project' => '[email protected]',
'technology' => '[email protected]'
);
// Validate their choice, or choose the first one so an email address is always selected
if(!array_key_exists($subject, $allowed)) {
$subject = array_keys($allowed)[0];
}
// Get the email address
$email = sanitize_email($allowed[$subject]);
// Update recipient so they receive it instead of whatever was set
$mail = $contact_form->prop('mail');
$mail['recipient'] = $email;
$contact_form->set_properties(array('mail' => $mail);
}
add_action('wpcf7_before_send_mail', 'netschmiede24_before_send_mail', 10, 3);
Then in your email configuration, you can put whatever email address you want that passes validation, because it will be ignored and replaced by the email address set in the code snippet above. Since this happens after the form is submitted, the user will not see the recipient.
To achieve your first goal, we can simplify your select field’s options to only the subjects since the email addresses are being sorted after. So in your mail template, you can use this:
[dynamic_select* your-subject include_blank dtx_disable_blank "General Requiry" "Support" "Project" "Technology"]
For your support page, we can do it two ways, we can set it as a query in the URL so when the button is clicked, it’s preselected, but that won’t preselect it if the user gets to the support page through another means. So instead, I’m going to use the page’s title. This would also work for pages named “Project”, “Technology” and “General Requiry”. To do this, we’re going to use the default attribute and it does have to be encoded like @h1lbert said in order to work. So now your select field tag will look like this:
[dynamic_select* your-subject include_blank default:CF7_get_post_var%20key%3D%26%2339%3Bpost_title%26%2339%3B dtx_disable_blank "General Requiry" "Support" "Project" "Technology"]
So now whenever someone opens that form on your Support page, regardless of how they arrived, it will pre-select the “Support” subject and when submitted, will send to your [email protected] address without revealing the address to the end user.