Trouble formatting "from" Header in PHP contact form

Can anyone please help me to correctly define these $header variables? All of the sample code I’ve founded online does not apply to my situation:

I am trying to correctly format the $headers in my PHP contact form, so that when someone sends me an email from the contact form on my website it is labeled as the “from” address and the “Reply-to” email address in the from header. A snippet labeled ORIGINAL CODE is what was in the original form (that came with a theme that I purchased) and resulted in an error from the server. The solution I’ve tried is labeled as NEW CODE and now the contact form submits sends successfully, but the header reads as: " [email protected]"

ORIGINAL CODE:
[php]<?php // Setup our basic variables $input_name = strip_tags($_POST['name']); $input_email = strip_tags($_POST['email']); $input_subject = strip_tags($_POST['subject']); $input_message = strip_tags($_POST['message']);[/php]

//MAIL function
[php]if(mail($your_email_address, $subject, $message, "From: $input_email")) {[/php]

NEW CODE
[php][code]<?php
// Setup our basic variables
$input_name = strip_tags($_POST[‘name’]);
$input_email = strip_tags($_POST[‘email’]);
$input_subject = strip_tags($_POST[‘subject’]);
$input_message = strip_tags($_POST[‘message’]);
$headers = ‘From: $email’ . “\r\n” .
‘Reply-To: $email’ . “\r\n” .
‘X-Mailer: PHP/’ . phpversion();

//MAIL function
if(mail($your_email_address, $subject, $message, $headers )) {[/code][/php]

The entire code is visible at the following link if you need to see it all to make sense of things. I would be ever so grateful to anyone that could help.

[php] $headers = ‘From: $email’ . “\r\n” .
‘Reply-To: $email’ . “\r\n” .
‘X-Mailer: PHP/’ . phpversion();[/php]

Won’t populate the string variables. PHP is funny in that, a single quote is JUST a string. Double quotes allow string interpolation.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service