I am trying to include the current date, senders IP and Host in my contact form submission.
This is the code:
<?php
if( isset($_POST['name']) )
{
$to = 'info@mydomain.com'; // Replace with your email
$subject = 'Contact Form Submission'; // Replace with your $subject
$headers = 'From: ' . $_POST['email'] . "
" . 'Reply-To: ' . $_POST['email'];
$date = date('M jS, Y @ h:i a');
$IP = $_SERVER['REMOTE_ADDR'];
$Host = $_SERVER['HTTP_HOST'];
$message = 'Name: ' . $_POST['name'] . "
" .
'E-mail: ' . $_POST['email'] . "
" .
'Subject: ' . $_POST['subject'] . "
" .
'Department: ' . $_POST['department'] . "
" .
'Message: ' . $_POST['message'] . "
" .
'Date: ' . $_POST['$date'] . "
" .
'IP: ' . $_POST['$IP'] . "
" .
'Host: ' . $_POST['$Host'];
mail($to, $subject, $message, $headers);
if( $_POST['copy'] == 'on' )
{
mail($_POST['email'], $subject, $message, $headers);
}
}
?>
The Email is received, but without Date, IP and Host. Any help would be greatly appreciated.
You have already stored(ip, date etc.) in variables so use these variables for mail. also these values are not exists in POST data so try to replace
'Date: ' . $_POST['$date'] . "
" .
'IP: ' . $_POST['$IP'] . "
" .
'Host: ' . $_POST['$Host'];
to
'Date: ' . $date . "
" .
'IP: ' . $IP . "
" .
'Host: ' . $Host;
You should attach like this(bellow) because $date, $IP and $Host are not transmitted in a post request:
$createdDate = new DateTime();
$date= $createdDate->format('d.m.Y H:m');
$message = 'Name: ' . $_POST['name'] . "
" .
'E-mail: ' . $_POST['email'] . "
" .
'Subject: ' . $_POST['subject'] . "
" .
'Department: ' . $_POST['department'] . "
" .
'Message: ' . $_POST['message'] . "
" .
'Date: ' . $date. "
" .
'IP: ' . $IP. "
" .
'Host: ' . $Host;