Trouble formatting "from" Header in PHP contact form

A

Anonymous

Guest
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@MYSERVER.web-hosting.com"

ORIGINAL 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']);

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

NEW 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 )) {


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.

https://docs.google.com/document/d/13kU6jhQHTNGEXPuwvDcoJOLkRX54QeUQbLOS8ir-IAA/edit?usp=sharing
 
In your new code, there's this:

Code:
$headers = 'From: $email' . "\r\n"

I don't know about anything else that might be wrong with your code, but $variables don't work inside strings with single quotes. Instead, try:

Code:
$headers = "From: {$email}\r\n"
 
$input_email = strip_tags($_POST['email']);

$headers = "From: $input_email\r\n";
$headers .= "Reply-To: $input_email\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();

// Use $headers in the mail() function
mail($your_email_address, $input_subject, $input_message, $headers);

Remember to replace $your_email_address, $input_subject, and $input_message with your code's appropriate variables or values.

It's important to note that proper validation, sanitization, and additional error handling should be implemented based on your specific requirements and security considerations.
 
Well, I searched for it on the internet and I can see some logical error with your code.
Lets try below code to fix the error.

<?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: ' . $input_email . "\r\n" .
'Reply-To: ' . $input_email . "\r\n" .
'X-Mailer: PHP/' . phpversion();

//MAIL function
if(mail($your_email_address, $input_subject, $input_message, $headers)) {
// Email sent successfully
// You can add your success message or redirection here
} else {
// Email sending failed
// You can handle the error case here
}
?>

Thanks
 
Back
Top