Include date, IP and HTTP_HOST in Support form - php

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'] . "\r\n" . 'Reply-To: ' . $_POST['email'];
$date = date('M jS, Y # h:i a');
$IP = $_SERVER['REMOTE_ADDR'];
$Host = $_SERVER['HTTP_HOST'];
$message = 'Name: ' . $_POST['name'] . "\n" .
'E-mail: ' . $_POST['email'] . "\n" .
'Subject: ' . $_POST['subject'] . "\n" .
'Department: ' . $_POST['department'] . "\n" .
'Message: ' . $_POST['message'] . "\n" .
'Date: ' . $_POST['$date'] . "\n" .
'IP: ' . $_POST['$IP'] . "\n" .
'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'] . "\n" .
'IP: ' . $_POST['$IP'] . "\n" .
'Host: ' . $_POST['$Host'];
to
'Date: ' . $date . "\n" .
'IP: ' . $IP . "\n" .
'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'] . "\n" .
'E-mail: ' . $_POST['email'] . "\n" .
'Subject: ' . $_POST['subject'] . "\n" .
'Department: ' . $_POST['department'] . "\n" .
'Message: ' . $_POST['message'] . "\n" .
'Date: ' . $date. "\n" .
'IP: ' . $IP. "\n" .
'Host: ' . $Host;

Related

MIME email, blank message body

So I tried searching but couldn't find an answer that I was able to apply to my code. I have had a developer updating a page for me and these emails come through on my mobile device, but the body is always blank on my computer email, Thunderbird. The code in question looks like this:
$mailBody = "Dog Registration Form Data" . PHP_EOL . $strDogCName . ";" . $strDogFName . ";" . $strBreed . ";" . $strGender . ";" . $strHt . ";" . $strBdmm . "/" . $strBddd . "/" . $strBdyy . ";" . $strFName . ";" . $strLName . ";" . $strAddr1 . ";" . $strCity . ";" . $strState . ";" . $strZip . ";" . $strCountry . ";" . $strPhone . ";" . $strEMail . ";" . $member_num. ";" . $dog_num;
// injection test before setting message headers
$sender_name = $strFName . " " . $strLName;
$sender_name = injection_test($sender_name);
$sender_email = injection_test($strEMail);
// Set headers and send. This should be moved to a reusable function
$mime_boundary = md5(time());
$headers = '';
$msg = '';
$headers .= 'From: ' . $sender_name . ' <' . $sender_email . '>' . PHP_EOL;
$headers .= 'Reply-To: ' . $sender_name . ' <' . $sender_email . '>' . PHP_EOL;
$headers .= 'Return-Path: ' . $sender_name . ' <' . $sender_email . '>' . PHP_EOL;
$headers .= "Message-ID: <" . time() . "cform#" . $_SERVER['SERVER_NAME'] . ">" . PHP_EOL;
$headers .= 'X-Sender-IP: ' . $_SERVER["REMOTE_ADDR"] . PHP_EOL;
$headers .= "X-Mailer: PHP v" . phpversion() . PHP_EOL;
$headers .= 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Content-Type: multipart/related; boundary="' . $mime_boundary . '"';
$msg .= '--' . $mime_boundary . PHP_EOL;
$msg .= 'Content-Type: text/plain; charset="UTF-8"' . PHP_EOL;
$msg .= 'Content-Transfer-Encoding: 8bit' . PHP_EOL . PHP_EOL;
$msg .= $mailBody . PHP_EOL . PHP_EOL;
$msg .= '--' . $mime_boundary . '--' . PHP_EOL . PHP_EOL;
ini_set('sendmail_from', $sender_email);
$msg = "Thank you for registering with NADAC.". PHP_EOL. " Here is the info you provided." . PHP_EOL ."Callname: ". $strDogCName . PHP_EOL. "Registered Name: " . $strDogFName . PHP_EOL . "Breed: " . $strBreed .PHP_EOL . "Gender: " . $strGender .PHP_EOL . "Height: " . $strHt .PHP_EOL . "Birthday: " . $strBdmm . "/" . $strBddd . "/" . $strBdyy .PHP_EOL . "Owner First Name: " . $strFName .PHP_EOL . "Owner Last Name: " . $strLName .PHP_EOL . "Address: " . $strAddr1 .PHP_EOL . "City: " . $strCity .PHP_EOL . "State: " . $strState .PHP_EOL . "Zip Code: " . $strZip .PHP_EOL . "Country: " . $strCountry .PHP_EOL . "Phone: " . $strPhone . PHP_EOL . "Email: " . $strEMail .PHP_EOL . "Associate number: " . $member_num. PHP_EOL . "Dog Number: " . $dog_num;
$mailSubject = "Thank you for registering.";
$send_status = mail($mailTo, $mailSubject, $msg, $headers);
$mailTo = $strEMail;
mail($mailTo, $mailSubject, $msg, $headers);
ini_restore('sendmail_from');
// should check send_status here and do something - TODO
unset($_POST['submitted']);
// Done with the mail, display confirmation
I'm sure it's something simple that I'm just missing. But I can't find it, and the programmer working on it doesn't seem to believe the issue. I don't believe it's a local issue with my email provider.
Have you tried with Content-Type: multipart/alternative instead of 'multipart/related'?
The weird thing if the double mail() calling and $send_status to unknown $mailTo var

Php Form - Why are all fields required?

I have a contact form that will only submit if all the fields are completed. I don't want this as many fields are optional. I believe it has to do with this part of my code, but I am unsure how to change it and removing it will post the form automatically when the page opens.
if (isset($_POST['first_name'], $_POST['last_name'], $_POST['address'], $_POST['address_line_2'], $_POST['city_state_zip'], $_POST['phone_number'], $_POST['email_address'], $_POST['bedrooms'], $_POST['baths'], $_POST['square_feet'], $_POST['basement'], $_POST['garage'], $_POST['house_style'], $_POST['price_range'], $_POST['construction'], $_POST['heat'], $_POST['features'], $_POST['comments']))
For context here is the rest of the code.
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
include_once "/usr/share/pear/Swift/swift_required.php";
// Check if the form has been posted
if (isset($_POST['first_name'], $_POST['last_name'], $_POST['address'], $_POST['address_line_2'], $_POST['city_state_zip'], $_POST['phone_number'], $_POST['email_address'], $_POST['bedrooms'], $_POST['baths'], $_POST['square_feet'], $_POST['basement'], $_POST['garage'], $_POST['house_style'], $_POST['price_range'], $_POST['construction'], $_POST['heat'], $_POST['features'], $_POST['comments'])) {
// The email address the email will be sent to
$to = 'emailaccount#website.com';
// Set the from address for the email
$from = 'emailaccount#website.com';
$headers = "Reply-To: ".$_POST["email"]."\r\n";
// The email subject
$subject = 'Contact Form Submission';
// Build the body of the email
$mailbody = "The contact form has been filled out.\n\n"
. "first_name: " . $_POST['first_name'] . "\n"
. "last_name: " . $_POST['last_name'] . "\n"
. "address: " . $_POST['address'] . "\n"
. "address_line_2: " . $_POST['address_line_2'] . "\n"
. "city_state_zip: " . $_POST['city_state_zip'] . "\n"
. "phone_number: " . $_POST['phone_number'] . "\n"
. "email_address: " . $_POST['email_address'] . "\n"
. "bedrooms: " . $_POST['bedrooms'] . "\n"
. "baths: " . $_POST['baths'] . "\n"
. "square_feet: " . $_POST['square_feet'] . "\n"
. "basement: " . $_POST['basement'] . "\n"
. "garage: " . $_POST['garage'] . "\n"
. "house_style: " . $_POST['house_style'] . "\n"
. "price_range: " . $_POST['price_range'] . "\n"
. "construction: " . $_POST['construction'] . "\n"
. "heat: " . $_POST['heat'] . "\n"
. "features: " . $_POST['features'] . "\n"
. "comments:\n" . $_POST['comments'];
// Create the mail transport
$transport = Swift_SmtpTransport::newInstance('smtp.domainhere.com', 587);
$transport->setUsername('emailaccount#website.com');
$transport->setPassword('123456');
$swift = Swift_Mailer::newInstance($transport);
// Create the mail
$message = new Swift_Message($subject);
$message->setFrom($from);
$message->setTo($to);
$message->setBody($mailbody);
// Send the mail
$result = $swift->send($message);
}
if ($result)
{
header('Location: http://www.domainhere.com/thankyou.html'); }
?>
Your thoughts would be greatly appreciated.
This line:
if (isset($_POST['first_name'], $_POST['last_name'], $_POST['address'], $_POST['address_line_2'], $_POST['city_state_zip'], $_POST['phone_number'], $_POST['email_address'], $_POST['bedrooms'], $_POST['baths'], $_POST['square_feet'], $_POST['basement'], $_POST['garage'], $_POST['house_style'], $_POST['price_range'], $_POST['construction'], $_POST['heat'], $_POST['features'], $_POST['comments']))
requires everything there to be filled in.

Linebreaks in PHP mail(): \n, \\n and \r\n dont work

This is on a LAMP server.
All the answers I've found online suggest that one of the above should work, but I cant get it working.
My code is:
$to = $email_to;
$subject = 'Website Form';
$message = 'From: ' . $name .
' \r\n Date: ' . $date .
' \r\nText: ' . $text .
'\r\n Use on Presentations?: ' . $presentations .
'\r\n Use on Websites?: ' . $website .
'\r\n Use on Case Studies?: ' . $casestudy;
$headers = 'From: website#website.com' . "\r\n" .
'Reply-To: no-reply#website.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Would anyone know what I've done wrong?
You are using single quotes, if you want to use \n or \r you have to put them in double quotes, like:
$value = "here is some text, and a $variable, and a line end\r\n";
I do a lot of php mailer stuff and \r\n has to be in "..." or it's not executed as an escaped character, but rather as a literal.
Ie:
echo ".\n.";
# is this:
.
.
#but
echo '.\n'.;
#is this:
.\n.
that's all.
For that you can set header like $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; and then user <br /> in place of \n.
Why are you using \r\n, use <BR>
$message = 'From: ' . $name .
' <BR> Date: ' . $date .
' <BR>Text: ' . $text .
' <BR>Use on Presentations?: ' . $presentations .
' <BR>Use on Websites?: ' . $website .
' <BR>Use on Case Studies?: ' . $casestudy;

How to Keep People from Randomly Accessing Form Action Page

I am creating an HTML form that references a PHP file to send an email. However, people keep hitting the form's action page, which then sends me an email with no information filled in. I would like to solve this by somehow protecting that action page from outside hits.
Here is my code:
HTML form:
<form name="homecontact" action="/admin/formactions/writequick.php" method="POST">
<input name="name" placeholder="Name"></input>
<input name="email" placeholder="Email"></input>
<input name="phone" placeholder="Phone"></input>
<textarea name="message" placeholder="Message"></textarea>
<input type="submit" style="background:MidnightBlue; color:white;">
</form>
PHP Script:
<?php
$to = 'myemailaddress#example.com';
$subjectadmin = 'CONTACT FORM from My Website';
$subjectuser = 'Contact Confirmation from My Website';
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['message'];
$messageadmin = 'A customer just submitted a form on Your Website. Here is their message:' . "\n" . "\n" . 'Name:' . ' ' . $name . "\n" . 'Email:' . ' ' . $email . "\n" . 'Phone:' . ' ' . $phone . "\n" . 'Comments:' . ' ' . $comments . "\n";
$messageuser = 'Hello,' . ' ' . $name . '!' . "\n" . "\n" . 'Thank you for contacting My Website! We have received your contact form and we will get back to you as soon as we can.' . "\n" . "\n" . 'Just for your records, here is what you submitted to us:' . "\n" . 'Name:' . ' ' . $name . "\n" . 'Email:' . ' ' . $email . "\n" . 'Phone:' . ' ' . $phone . "\n" . 'Comments:' . ' ' . $comments . "\n" . "\n" . 'Thank you for choosing My Website!';
$headers = 'From: myotheraddress#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subjectadmin, $messageadmin, $headers);
mail($email, $subjectuser, $messageuser, $headers);
?>
The form works perfectly, but I just do not know how to keep people from hitting that action page, unless they go through the form.
"I like that, but will it still send it if only one field is blank? I don't want people to HAVE to put their contact info if they do not want to."
Quickest fix then: name your submit button
<input type="submit" style="background:MidnightBlue; color:white;" name="submit">
and wrap your code inside a conditional statement.
if(isset($_POST['submit'])){
// rest of your PHP/mail code
}
then just use a header to redirect on mail success
...
mail($email, $subjectuser, $messageuser, $headers);
header("Location: http://www.example.com/");
exit;
Use this in your form: (replace it with the one you have now)
<input type="submit" style="background:MidnightBlue; color:white;" name="submit">
Rewrite:
<?php
if(isset($_POST['submit'])) {
$to = 'myemailaddress#example.com';
$subjectadmin = 'CONTACT FORM from My Website';
$subjectuser = 'Contact Confirmation from My Website';
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['message'];
$messageadmin = 'A customer just submitted a form on Your Website. Here is their message:' . "\n" . "\n" . 'Name:' . ' ' . $name . "\n" . 'Email:' . ' ' . $email . "\n" . 'Phone:' . ' ' . $phone . "\n" . 'Comments:' . ' ' . $comments . "\n";
$messageuser = 'Hello,' . ' ' . $name . '!' . "\n" . "\n" . 'Thank you for contacting My Website! We have received your contact form and we will get back to you as soon as we can.' . "\n" . "\n" . 'Just for your records, here is what you submitted to us:' . "\n" . 'Name:' . ' ' . $name . "\n" . 'Email:' . ' ' . $email . "\n" . 'Phone:' . ' ' . $phone . "\n" . 'Comments:' . ' ' . $comments . "\n" . "\n" . 'Thank you for choosing My Website!';
$headers = 'From: myotheraddress#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subjectadmin, $messageadmin, $headers);
mail($email, $subjectuser, $messageuser, $headers);
// redirect after mail sent
header("Location: http://www.example.com/"); // modify it to your site
exit;
}
else{ echo "You can't do that from here."; }
?>
Wrap the code in
if($_SERVER['REQUEST_METHOD']=='POST'){
//Do stuff here
}
This will make sure the user is making a post request before doing anything. Also you will want to verify the variables have values before mailing. Use
if(!empty($_POST['variable_key_here'])){
//Do stuff here
}
you can check $_POST super global array
<?php
if(isset($_POST['name'])) {
$to = 'myemailaddress#example.com';
$subjectadmin = 'CONTACT FORM from My Website';
$subjectuser = 'Contact Confirmation from My Website';
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['message'];
$messageadmin = 'A customer just submitted a form on Your Website. Here is their message:' . "\n" . "\n" . 'Name:' . ' ' . $name . "\n" . 'Email:' . ' ' . $email . "\n" . 'Phone:' . ' ' . $phone . "\n" . 'Comments:' . ' ' . $comments . "\n";
$messageuser = 'Hello,' . ' ' . $name . '!' . "\n" . "\n" . 'Thank you for contacting My Website! We have received your contact form and we will get back to you as soon as we can.' . "\n" . "\n" . 'Just for your records, here is what you submitted to us:' . "\n" . 'Name:' . ' ' . $name . "\n" . 'Email:' . ' ' . $email . "\n" . 'Phone:' . ' ' . $phone . "\n" . 'Comments:' . ' ' . $comments . "\n" . "\n" . 'Thank you for choosing My Website!';
$headers = 'From: myotheraddress#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subjectadmin, $messageadmin, $headers);
mail($email, $subjectuser, $messageuser, $headers);
} else {
die("Access denied");
}
?>
Your code to submit contact details on your mail is correct, but
there is no validation in it.
We have many methods to validate forms :
1.You can use CAPTCHA in your form.
2.Client Side Java Script Validations as well as server side PHP validations to check for correct email pattern , empty field , html special characters and all.
Check this link :
http://www.w3schools.com/php/php_form_url_email.asp
http://www.w3schools.com/js/js_form_validation.asp
There are many methods for validations , just google as per your requirement and your problem will be solved.
You can try this:
<?php
if($_SERVER['REQUEST_METHOD']=='POST' && !empty($_POST)) {
// you should also validate your _POST data (email syntaxis, etc)
$to = 'myemailaddress#example.com';
$subjectadmin = 'CONTACT FORM from My Website';
$subjectuser = 'Contact Confirmation from My Website';
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['message'];
$messageadmin = 'A customer just submitted a form on Your Website. Here is their message:' . "\n" . "\n" . 'Name:' . ' ' . $name . "\n" . 'Email:' . ' ' . $email . "\n" . 'Phone:' . ' ' . $phone . "\n" . 'Comments:' . ' ' . $comments . "\n";
$messageuser = 'Hello,' . ' ' . $name . '!' . "\n" . "\n" . 'Thank you for contacting My Website! We have received your contact form and we will get back to you as soon as we can.' . "\n" . "\n" . 'Just for your records, here is what you submitted to us:' . "\n" . 'Name:' . ' ' . $name . "\n" . 'Email:' . ' ' . $email . "\n" . 'Phone:' . ' ' . $phone . "\n" . 'Comments:' . ' ' . $comments . "\n" . "\n" . 'Thank you for choosing My Website!';
$headers = 'From: myotheraddress#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subjectadmin, $messageadmin, $headers);
mail($email, $subjectuser, $messageuser, $headers);
}
?>
That way, it doesn't matter if a user goes directly to that page (in GET mode). It will have to go through a post form to actually invoke the mail() function. You could also add a simple additional security layer (to avoid posts requests from other sites) by adding a hidden input in the form:
<input type="hidden" name="hash" value="<?=md5(date("Y-m-d H"))?>">
And then just apply this validation at the start of the writequick.php file:
if($_POST['hash'] == md5(date("Y-m-d H"))) {
I hope that helps!
Try this
<form name="homecontact" action="/admin/formactions/writequick.php?action=mail" method="POST">
</form>
in your writequick.php page
if(isset($_GET['action'])){
//place your mail code
}

Redirecting from submission form to thank you page in PHP

I am in the process of changing from recaptcha to a honey pot to avoid spam on my site. www.dentistheadhunter.com. I am using www.dentistheadhunter.com/indexpot.php for my trial run and when I submit my form, the process form is working in inputting into my database and sending me an email, but the Thank you form redirect is not working and the process form (http://www.dentistheadhunter.com/resumefrontpot2.php) just ends in a blank page instead of redirecting to my thank_you.php page.
I have read up on previous questions and removed any white space before the starting php tag and that did not solve the problem.
Here is my code
<?php
require("includes/dbconnect/mob_db.php");
$robotest = $_POST['robotest'];
if($robotest)
echo "Thank you for your inquiry Robot.";
$cand_sql = 'INSERT INTO dent_leads (first_name,last_name,disc_id,city,state,zip,area_code,phone,email,location,setting,experience,timestamp,source,notes,resume_path) VALUES ("' . $_POST['first_name'] . '","' . $_POST['last_name'] . '","' . $_POST['disc'] . '","' . $_POST['city'] . '","' . $_POST['state'] . '","' . $_POST['zip'] . '","' . $_POST['area_code'] . '","' . $_POST['phone1'] . $_POST['phone2'] . '","' . $_POST['email'] . '","' . $_POST['location'] . '","' . $_POST['setting'] .'","' . $_POST['experience'] .'","' . time() . '","DentistHeadHunterFP.com","' . $_POST['comments'] . '","' . $target_path .'")';
$cand_res = mysql_query($cand_sql,$leads_db);
if(is_array($_POST['license'])){
foreach($_POST['license'] as $lic_state){
if($lic_state != ''){
$licensed .= ',("' . $dent_id . '","' . $lic_state . '")';
}
}
if(isset($licensed)){
$lic_sql = 'INSERT INTO dent_lic (dent_id,state) VALUES ' . ltrim($licensed,',');
$lic_res = mysql_query($lic_sql,$leads_db);
}
}
$msg .= 'Name: ' . $_POST['first_name'] . ' ' . $_POST['last_name'] . '<br />' . "\r\n";
$msg .= 'Discipline: ' . $_POST['disc'] . '<br />' . "\r\n";
$msg .= 'City: ' . $_POST['city'] . '<br />' . "\r\n";
$msg .= 'State: ' . $_POST['state'] . '<br />' . "\r\n";
$msg .= 'Zip: ' . $_POST['zip'] . '<br />' . "\r\n";
$msg .= 'Phone: ' . $_POST['area_code'] . '-' . $_POST['phone1'] . '-' . $_POST['phone2'] . '<br />' . "\r\n";
$msg .= 'Email: ' . $_POST['email'] . '<br />' . "\r\n";
$msg .= 'Experience: ' . $_POST['experience'] . '<br />' . "\r\n";
$msg .= 'Lead Source: First page ' . $_POST[''] . '<br />' . "\r\n";
$msg .= 'Settings: ' . $_POST['setting'] . '<br />' . "\r\n";
$msg .= ' ' . $_POST[''] . '<br />' . "\r\n";
$msg .= 'Location: ' . $_POST['location'] . '<br />' . "\r\n";
$msg .= ' ' . $_POST[''] . '<br />' . "\r\n";
$msg .= 'First Page Comments: ' . $_POST['comments'] . '<br />' . "\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Return-Path: <dentistjobs#dentistheadhunter.com> ' . "\r\n";
$headers .= 'From: Dentist Head Hunter Candidate <dentistjobs#dentistheadhunter.com> ' . "\r\n";
$headers .= 'Reply-To: Dentist Head Hunter Candidate <dentistjobs#dentistheadhunter.com> ' . "\r\n";
$headers .= 'Bcc: Dentist Head Hunter Candidate <dentistjobs#dentistheadhunter.com> ' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail('dentistjobs#dentistheadhunter.com','NEW HEADHUNTER LEAD',$msg,$headers);
header( 'Location: http://www.dentistheadhunter.com/thank_you.php' ) ;
?>
Seems you are echo message before header()
If you want to echo the message, then write
ob_start();
echo "Thank you for your inquiry Robot.";
header('Location:http://www.dentistheadhunter.com/thank_you.php');
ob_end_flush();

Categories