I have been advised to add headers to my php code as my emails are coming through to my inbox with blank/empty form fields even though the user has iputted them on the webpage before clicking submit. I will be greatly appreciative if somebody can tell me if i have done this right?
<?php
$EmailFrom = "Quote@mydomian.co.uk";
$EmailTo = "me@mydomian.co.uk";
$Subject = "Online contact form";
$fullName = Trim(stripslashes($_POST['fullName']));
$contactNo = Trim(stripslashes($_POST['contactNo']));
$message = Trim(stripslashes($_POST['message']));
// prepare email body text
$Body = "";
$Body .= "fullName: ";
$Body .= $fullName;
$Body .= "
";
$Body .= "contactNo: ";
$Body .= $contactNo;
$Body .= "
";
$Body .= "message: ";
$Body .= $message;
$Body .= "
";
$headers = 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
$success = mail($EmailTo, $Subject, $Body, $headers, $EmailFrom");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=Thankyou.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
?>
You should use the php header() function:
header('Content-type: application/html');
as this will be send first to the browser...
here is example how to set from/sender/reply-to headers in e-mail message:
$headers = array(
'From: sender name <senderemail@example.com>',
'Sender: senderemail@example.com',
'To: '.$to,
'Reply-To: senderemail@example.com',
'MIME-Version: 1.0',
'Content-Type: text/html; charset=utf-8',
'Content-Transfer-Encoding: 8bit',
);
if ($cc)
$headers[] = 'CC: ' . $cc;
if ($bcc)
$headers[] = 'BCC: ' . $bcc;
$response = @mail($to, $subject, $content, implode("
", $headers), '-f senderemail@example.com');
NOTE: as you see there are many duplicate of sender's email address, but I've noticed that mail clients and mail senders (like sendmail) behave differently, so this is needed to handle possible configurations