Snippet: Set the “From” Header in the Contact Form to the Submitted Email
Important: Using the submitted user's email address as the "From" header in wp_mail() can cause delivery issues because it violates common email authentication standards like SPF, DKIM, and DMARC. These protocols are designed to prevent email spoofing by ensuring that only authorized servers can send mail on behalf of a domain. When your server sends an email claiming to be from, for example, a Gmail or Yahoo address that it doesn't have permission to use, the recipient's mail server may flag the message as suspicious, mark it as spam, or reject it entirely.
Additionally, many domains enforce strict DMARC policies that instruct mail providers to reject or quarantine messages that fail authentication checks. As a result, pretending to send from a user's email can make your contact form submissions unreliable or undeliverable.
This snippet is NOT recommended!!!!
This snippet customizes the "From" email address used in contact form submissions. It sets the "From" header to the email address entered by the user in the contact form.
add_filter( 'vcex_contact_form_mail_headers', function( $mail_headers, $data ) {
if ( ! empty( $data['email'] ) && is_email( $data['email'] ) ) {
$mail_headers[] = 'From: ' . sanitize_email( $data['email'] );
}
return $mail_headers;
}, 10, 2 );
All PHP snippets should be added via child theme's functions.php file or via a plugin.
We recommend Code Snippets (100% Free) or WPCode (sponsored)
We recommend Code Snippets (100% Free) or WPCode (sponsored)