When someone fills out a form on our website it uses the person filling out the forms email address. Our hosting company is blocking the emails because the person filling out the form is set as the 'From'. From our hosting company:
"You'll need to ensure that the form is permanently set with a DreamHost
hosted email address so that this form works properly and isn't rejected.
It may be set so that the person filling out the form is being set as the
'From', which will result in the rejection above.
Just edit the form so that the 'From' is permanently set to a DH user so
that the form is no longer rejected due to policy reasons."
What changes do I need to make to the below code to make this acceptable to Dreamhost? Any help would be greatly appreciated.
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "eileenw#ourdomain.com";
$email_subject = "Quiz";
$your_email = "info#ourdomain.com";
function died($error) {
// your error code can go here
echo "<p>We are very sorry, but there were error(s) found with the form you submitted.</p> ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "<p>Please go back and fix these errors.<br /><br /></p>";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$address = $_POST['address']; // not required
$city = $_POST['city']; // not required
$state = $_POST['state']; // not required
$vehicleyear = $_POST['vehicleyear']; // not required
$vehiclemake = $_POST['vehiclemake']; // not required
$vehiclemodel = $_POST['vehiclemodel']; // not required
$purchase_or_lease = $_POST['purchase_or_lease']; // not required
$deliverydate = $_POST['deliverydate']; // not required
$mileage_at_delivery = $_POST['mileage_at_delivery']; // not required
$current_mileage = $_POST['current_mileage']; // not required
$seller = $_POST['seller']; // not required
$citystate = $_POST['citystate']; // not required
$Bank_Finance = $_POST['Bank_Finance']; // not required
$dealer_arranged = $_POST['dealer_arranged']; // not required
$employee_discount = $_POST['employee_discount']; // not required
$warranty = $_POST['warranty']; // not required
$length_contract = $_POST['length_contract']; // not required
$comments = $_POST['comments']; // not required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Address: ".clean_string($address)."\n";
$email_message .= "City: ".clean_string($city)."\n";
$email_message .= "State: ".clean_string($state)."\n";
$email_message .= "Vehicle Year: ".clean_string($vehicleyear)."\n";
$email_message .= "Make: ".clean_string($vehiclemake)."\n";
$email_message .= "Model: ".clean_string($vehiclemodel)."\n";
$email_message .= "Purchase or Lease: ".clean_string($purchase_or_lease)."\n";
$email_message .= "Delivery date: ".clean_string($deliverydate)."\n";
$email_message .= "Mileage at Delivery: ".clean_string($mileage_at_delivery)."\n";
$email_message .= "Current Mileage: ".clean_string($current_mileage)."\n";
$email_message .= "Selling Dealer: ".clean_string($seller)."\n";
$email_message .= "Seller City and State: ".clean_string($citystate)."\n";
$email_message .= "Bank or Finance Company: ".clean_string($Bank_Finance)."\n";
$email_message .= "Dealer Arranged: ".clean_string($dealer_arranged)."\n";
$email_message .= "Employee Discount? ".clean_string($employee_discount)."\n";
$email_message .= "Warranty: ".clean_string($warranty)."\n";
$email_message .= "Contract Terms: ".clean_string($length_contract)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: ' . $your_email . "\r\n";
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
According to your hosting provider:
Just edit the form so that the 'From' is permanently set to a DH user
So if I were to guess, I suspect that the change you need to make is to modify the From header. This is where you set it:
$headers = 'From: ' . $your_email . "\r\n";
So set it to something else:
$headers = 'From: someknownaddress#dreamhost.com' . "\r\n";
Related
I am relatively new to php but learning as I go. I am creating a tenant application form for my business, and have got the form working with basic text, dropdown fields, etc. Now I need to add a file upload to the form that will be sent to my email.
Can anyone please spare the time to add in the file upload as an attachment as I've tried a few different code snippets but struggled with it.
the HTML for the form is quite lengthy, I will post it if required but i have added enctype="multipart/form-data" to the form tab, name for inout is file_upload.
Heres my form handler, which may well be a long way round but it works presently
if(isset($_POST['email'])) {
$email_to = "REMOVED";
$email_subject = "TENANT APPLICATION";
function died($error) {
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
$property = $_POST['property'];
$title = $_POST['title'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$dateofbirth = $_POST['dateofbirth'];
$nationality = $_POST['nationality'];
$email_from = $_POST['email'];
$telephone = $_POST['telephone'];
$natins = $_POST['natins'];
$smoker = $_POST['smoker'];
$bankrupt = $_POST['bankrupt'];
$ccj = $_POST['ccj'];
$currentaddress = $_POST['currentaddress'];
$tenancy = $_POST['tenancy'];
$timeataddress = $_POST['timeataddress'];
$employmentstatus = $_POST['employmentstatus'];
$company = $_POST['company'];
$positionheld = $_POST['positionheld'];
$salary = $_POST['salary'];
$timeatemployer = $_POST['timeatemployer'];
$selfemployedinfo = $_POST['selfemployedinfo'];
$selfemployedincome = $_POST['selfemployedincome'];
$otheroccupantsover18 = $_POST['otheroccupantsover18'];
$otheroccupantsunder18 = $_POST['otheroccupantsunder18'];
$pets = $_POST['pets'];
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Property applied for: ".clean_string($property)."\n\n";
$email_message .= "APPLICANT DETAILS.\n\n";
$email_message .= "Title: ".clean_string($title)."\n";
$email_message .= "First name: ".clean_string($first_name)."\n";
$email_message .= "Last name: ".clean_string($last_name)."\n";
$email_message .= "Date of birth: ".clean_string($dateofbirth)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "National insurance : ".clean_string($natins)."\n";
$email_message .= "Smoker : ".clean_string($smoker)."\n";
$email_message .= "Bankrupt : ".clean_string($bankrupt)."\n";
$email_message .= "CCJ's : ".clean_string($ccj)."\n\n";
$email_message .= "CURRENT ADDRESS DETAILS.\n\n";
$email_message .= "Current address: ".clean_string($currentaddress)."\n";
$email_message .= "Type of tenancy: ".clean_string($tenancy)."\n";
$email_message .= "Time at current address: ".clean_string($timeataddress)."\n\n";
$email_message .= "PREVIOUS ADDRESS DETAILS.\n\n";
$email_message .= "Previous address 1: ".clean_string($previousaddressone)."\n";
$email_message .= "Previous address 2: ".clean_string($previousaddresstwo)."\n\n";
$email_message .= "EMPLOYMENT.\n\n";
$email_message .= "Employment status: ".clean_string($employmentstatus)."\n\n";
$email_message .= "EMPLOYED DETAILS.\n\n";
$email_message .= "Company: ".clean_string($company)."\n";
$email_message .= "Position held: ".clean_string($positionheld)."\n";
$email_message .= "Salary: ".clean_string($salary)."\n";
$email_message .= "Time with employer: ".clean_string($timeatemployer)."\n\n";
$email_message .= "SELF EMPLOYED DETAILS.\n\n";
$email_message .= "Details of self employment: ".clean_string($selfemployedinfo)."\n";
$email_message .= "Average net income of past 3 years: ".clean_string($selfemployedincome)."\n\n";
$email_message .= "OTHER OCCUPANTS.\n\n";
$email_message .= "Other occupants OVER 18: ".clean_string($otheroccupantsover18)."\n";
$email_message .= "Other occupants UNDER 18: ".clean_string($otheroccupantsunder18)."\n";
$email_message .= "Details of any pets: ".clean_string($pets)."\n\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<?php include 'thankyouapplicationform.php';?>
<?php
}
I have successfully got the following code working but this was a code snippet downloaded from somewhere, and i failed to merge the file upload part into my form handler
''' <?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/*
Tested working with PHP5.4 and above (including PHP 7 )
*/
require_once './vendor/autoload.php';
use FormGuide\Handlx\FormHandler;
$pp = new FormHandler();
$validator = $pp->getValidator();
$validator->fields(['Name','Email'])->areRequired()->maxLength(50);
$validator->field('Email')->isEmail();
$validator->field('Message')->maxLength(6000);
$pp->attachFiles(['image']);
$pp->sendEmailTo('REMOVED'); // ↠Your email here
echo $pp->process($_POST); '''
Edit Here are the attempts I've made so far...
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I have looked for a solution to my problem all over but cant seem to get it to work.
I have a form that gets data from the user and then emails it to me when they click submit.
I would also like them to receive an confirmation email to the address they provided, and this is where I am stuck.
If the form is required please ask and I will send, but here is the PHP...
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "webhost#compumodsa.com";
$email_subject = "CompuMod Web-site Order";
function died($error) {
// your error code can go here
echo "Something was entered incorrectly…";
echo "Please correct the following.<br /><br />";
echo $error."<br /><br />";
echo "After correcting, please try again.<br /><br />";
die();
}
// validation expected data exists
if(
!isset($_POST['site']) ||
!isset($_POST['page']) ||
!isset($_POST['domain']) ||
!isset($_POST['host']) ||
!isset($_POST['service']) ||
!isset($_POST['firstname']) ||
!isset($_POST['lastname']) ||
!isset($_POST['company']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['idnumber']) ||
!isset($_POST['address']) ||
!isset($_POST['suburb']) ||
!isset($_POST['city']) ||
!isset($_POST['province']) ||
!isset($_POST['postcode']) ||
!isset($_POST['comments'])) {
died('Something was entered incorrectly…');
}
$site = $_POST['site']; // required
$page = $_POST['page']; // required
$domain = $_POST['domain']; // required
$host = $_POST['host']; // required
$service = $_POST['service']; // required
$firstname = $_POST['firstname']; // required
$lastname = $_POST['lastname']; // required
$company = $_POST['company']; // not required
$email = $_POST['email']; // required
$telephone = $_POST['telephone']; // required
$idnumber = $_POST['idnumber']; // required
$address = $_POST['address']; // required
$suburb = $_POST['suburb']; // not required
$city = $_POST['city']; // required
$province = $_POST['province']; // required
$postcode = $_POST['postcode']; // required
$comments = $_POST['comments']; // not required
$error_message = "";
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$site)) {
$error_message .= 'Your “Base Site Design” was not Selected.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$page)) {
$error_message .= 'Your “Extra Pages” was not Selected.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$domain)) {
$error_message .= 'Your “Domain Registration” was not Selected.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$host)) {
$error_message .= 'Your “Host Server Size” was not Selected.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$service)) {
$error_message .= 'Your “Service Package” was not Selected.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$firstname)) {
$error_message .= 'Your “First Name” was not entered or entered incorrectly.<br />';
}
if(!preg_match($string_exp,$lastname)) {
$error_message .= 'Your “Last Name” was not entered or entered incorrectly.<br />';
}
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= 'Your “E-Mail” was not entered or entered incorrectly.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$telephone)) {
$error_message .= 'Your “Phone/Cell” was not entered or entered incorrectly.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$idnumber)) {
$error_message .= 'Your “SA ID Number” was not entered or entered incorrectly.<br />';
}
$string_exp = "/^[A-Za-z0-9 .'-]+$/";
if(!preg_match($string_exp,$address)) {
$error_message .= 'Your “Street Address” was not entered or entered incorrectly.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$city)) {
$error_message .= 'Your “City” was not entered or entered incorrectly.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$province)) {
$error_message .= 'Your “Povince” was not entered or entered incorrectly.<br/>';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$postcode)) {
$error_message .= 'Your “Postal Code” was not entered or entered incorrectly.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Client request details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
//request email variables
$email_message .= "Base Site Design: - ".clean_string($site)."\n";
$email_message .= "Extra Pages: - ".clean_string($page)."\n";
$email_message .= "Domain Registration: - ".clean_string($domain)."\n";
$email_message .= "Host Server Size: - ".clean_string($host)."\n";
$email_message .= "Service Package: - ".clean_string($service)."\n";
$email_message .= "First Name: - ".clean_string($firstname)."\n";
$email_message .= "Last Name: - ".clean_string($lastname)."\n";
$email_message .= "Company: - ".clean_string($company)."\n";
$email_message .= "E-Mail: - ".clean_string($email)."\n";
$email_message .= "Phone/Cell: - ".clean_string($telephone)."\n";
$email_message .= "SA ID Number: - ".clean_string($idnumber)."\n";
$email_message .= "Address: - ".clean_string($address)."\n";
$email_message .= "Suburb: - ".clean_string($suburb)."\n";
$email_message .= "City: - ".clean_string($city)."\n";
$email_message .= "Province: - ".clean_string($province)."\n";
$email_message .= "Postal Code: - ".clean_string($postcode)."\n";
$email_message .= "Extra Requests: - ".clean_string($comments)."\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
//conformation email variables
$subject = "Welcome & Thanks!";
$email_message = "Thank you ".clean_string($firstname)." ".clean_string($lastname)." for your request\n\n";
$email_message .= "A Compumod representative will contact you as soon as posable.\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email, $subject, $email_message, $headers);
header('Location: http://www.compumodsa.com/index.php/component/k2/item/21');
?>
<!-- include your own success html here -->
<?php
}
?>
The form details gets emailed to me with...
//request email variables
$email_message .= "Base Site Design: - ".clean_string($site)."\n";
$email_message .= "Extra Pages: - ".clean_string($page)."\n";
$email_message .= "Domain Registration: - ".clean_string($domain)."\n";
$email_message .= "Host Server Size: - ".clean_string($host)."\n";
$email_message .= "Service Package: - ".clean_string($service)."\n";
$email_message .= "First Name: - ".clean_string($firstname)."\n";
$email_message .= "Last Name: - ".clean_string($lastname)."\n";
$email_message .= "Company: - ".clean_string($company)."\n";
$email_message .= "E-Mail: - ".clean_string($email)."\n";
$email_message .= "Phone/Cell: - ".clean_string($telephone)."\n";
$email_message .= "SA ID Number: - ".clean_string($idnumber)."\n";
$email_message .= "Address: - ".clean_string($address)."\n";
$email_message .= "Suburb: - ".clean_string($suburb)."\n";
$email_message .= "City: - ".clean_string($city)."\n";
$email_message .= "Province: - ".clean_string($province)."\n";
$email_message .= "Postal Code: - ".clean_string($postcode)."\n";
$email_message .= "Extra Requests: - ".clean_string($comments)."\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
The conformation email witch is suppose to go to the user...
//conformation email variables
$subject = "Welcome & Thanks!";
$email_message = "Thank you ".clean_string($firstname)." ".clean_string($lastname)." for your request\n\n";
$email_message .= "A Compumod representative will contact you as soon as posable.\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email, $subject, $email_message, $headers);
no email is being received by the user.
All help would be so much appreciated...
Not sure what you are trying to do here, but this would likely be where your problem lies...
if($_POST['email'] == "post")
{
//email variables
$subject = "Indie Rally - Welcome & Thanks!";
$email_message = "Thank you ".clean_string($firstname)." ".clean_string($lastname)."for your request\n\n";
$email_message .= "A Compumod representative will contact you as soon as posable.\n";
// create email headers
$headers = 'From: webhost#compumodsa.com' . "\r\n" .
'Reply-To: $email' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email, $subject, $email_message, $headers);
}
Assuming that $_POST['email'] is the email address of the person filling out the form, seeing if it equals the obviously invalid email address of "post" is either A) always going to fail the conditional check or B) pass the conditional check and pass an invalid "To:" address to the PHP mail function.
Since you have already performed the tests previously to ensure that the email address is both existing and valid, you should not require that conditional around the auto-responder functionality at all. Just send the email.
This will leave you with this...
/*
* Notice the removal of the `if()` statement
*/
//email variables
$subject = "Indie Rally - Welcome & Thanks!";
$email_message = "Thank you ".clean_string($firstname)." ".clean_string($lastname)."for your request\n\n";
$email_message .= "A Compumod representative will contact you as soon as posable.\n";
// create email headers
$headers = 'From: webhost#compumodsa.com' . "\r\n" .
'Reply-To: $email' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email, $subject, $email_message, $headers);
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = 'name <name#domain.com>, name <name#domain.com>,name <name#domain.com>';
$email_subject = "Enquiry for you";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
You forgot a space.
Change:
$email_to = 'name <name#domain.com>, name <name#domain.com>,name <name#domain.com>';
To:
$email_to = 'name <name#domain.com>, name <name#domain.com>, name <name#domain.com>';
How can I style an e-mail that's sent from a contact form?
I'd like to place it into nice tables with some decent styling. My front-end is just a HTML page that the user fills out a form. The form, upon submit, calls this PHP page to send the data. I have all of that portion working perfectly.
Now, I'd just like to style how the e-mail looks to the person receiving the e-mail since right now it's all just text & data.
My current PHP code is:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email#email.com";
$email_subject = "TEST - NO STYLE JUST DATA- Product Research Request";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['ProductMgr']) ||
!isset($_POST['ProductDesc'])||
!isset($_POST['email'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$ProductMgr = $_POST['ProductMgr']; // required
$email_from = $_POST['email']; // required
$ProductDesc = $_POST['ProductDesc']; // required
$ProjLaunchDate = $_POST['ProjLaunchDate']; // required
$ProdCat = $_POST['ProdCat']; // required
$ProdSubCat = $_POST['ProdSubCat']; // required
$SuggVendor = $_POST['SuggVendor']; // required
$VPartNum = $_POST['VPartNum']; // required
$EstLandCost = $_POST['EstLandCost']; // required
$EstRetail = $_POST['EstRetail']; // required
$GMPercent = $_POST['GMPercent']; // required
$GMDollar = $_POST['GMDollar']; // required
$ForeUSales = $_POST['ForeUSales']; // required
$ForeDSales = $_POST['ForeDSales']; // required
$WholesalePot = $_POST['WholesalePot']; // required
$CompProdPrice = $_POST['CompProdPrice']; // required
$CompCompany = $_POST['CompCompany']; // required
$SampleAvail = $_POST['SampleAvail']; // required
$ProdDims = $_POST['ProdDims']; // required
$ProdColors = $_POST['ProdColors']; // required
$EstProdWeight = $_POST['EstProdWeight']; // required
$Features = $_POST['Features']; // required
$AMLBenefits = $_POST['AMLBenefits']; // required
$ProBenefits = $_POST['ProBenefits']; // required
$Restrictions = $_POST['Restrictions']; // required
$GKS = $_POST['GKS'];
$ProdMgr2 = $_POST['ProdMgr2']; // required
$ProdDesc2 = $_POST['ProdDesc2']; // required
$PossVend = $_POST['PossVend']; // required
$ProjReTime = $_POST['ProjReTime']; // required
$EstLandCost2 = $_POST['EstLandCost2']; // required
$ProtoExpDate = $_POST['ProtoExpDate']; // required
$ProdExpDate = $_POST['ProdExpDate']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Product Manager: ".clean_string($ProductMgr)."\n";
$email_message .= "Product Description: ".clean_string($ProductDesc)."\n";
$email_message .= "Projected Launch Date: ".clean_string($ProjLaunchDate)."\n";
$email_message .= "Product Category: ".clean_string($ProdCat)."\n";
$email_message .= "Product Subcategory: ".clean_string($ProdSubCat)."\n";
$email_message .= "Suggested Vendor: ".clean_string($SuggVendor)."\n";
$email_message .= "Vendor Part Number: ".clean_string($VPartNum)."\n";
$email_message .= "Estimated Landed Cost: ".clean_string($EstLandCost)."\n";
$email_message .= "Estimated Retail: ".clean_string($EstRetail)."\n";
$email_message .= "Gross Margin %: ".clean_string($GMPercent)."\n";
$email_message .= "Gross Margin $: ".clean_string($GMDollar)."\n";
$email_message .= "Forecasted Unit Sales: ".clean_string($ForeUSales)."\n";
$email_message .= "Forecasted Dollar Sales: ".clean_string($ForeDSales)."\n";
$email_message .= "Wholesale Potential: ".clean_string($WholesalePot)."\n";
$email_message .= "Competition Product & Price: ".clean_string($CompProdPrice)."\n";
$email_message .= "Competitor Company: ".clean_string($CompCompany)."\n";
$email_message .= "Sample Available: ".clean_string($SampleAvail)."\n";
$email_message .= "Product Dimensions: ".clean_string($ProdDims)."\n";
$email_message .= "Product Colors: ".clean_string($ProdColors)."\n";
$email_message .= "Estimated Product Weight: ".clean_string($EstProdWeight)."\n";
$email_message .= "Features: ".clean_string($Features)."\n";
$email_message .= "Benefits to AML: ".clean_string($AMLBenefits)."\n";
$email_message .= "Benefits to Pro Customers: ".clean_string($ProBenefits)."\n";
$email_message .= "Any Restrictions: ".clean_string($Restrictions)."\n";
$email_message .= "GKS Approval To Go: ".clean_string($GKS)."\n";
$email_message .= "Product Manager: ".clean_string($ProdMgr2)."\n";
$email_message .= "Product Description: ".clean_string($ProdDesc2)."\n";
$email_message .= "Possible Vendors: ".clean_string($PossVend)."\n";
$email_message .= "Projected Research Time: ".clean_string($ProjReTime)."\n";
$email_message .= "Estimated Landing Cost: ".clean_string($EstLandCost2)."\n";
$email_message .= "Prototypes Expected Date: ".clean_string($ProtoExpDate)."\n";
$email_message .= "Production Expected Date: ".clean_string($ProdExpDate)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
}
header("Location: ThankYou.html"); //Redirect to Thank You HTML page after email is sent
?>
The beginning of the body of your message starts with the following line of code:
$email_message = "Form details below.\n\n";
Then the rest of the message is merely adding each value on a new line in the format of "label: $value" beginning with:
$email_message .= "Product Manager: ".clean_string($ProductMgr)."\n";
and ending with
$email_message .= "Production Expected Date: ".clean_string($ProdExpDate)."\n";
Assuming that you want all these in a table you could either add a line before or after the field assignments or prepend/append it to the above statements. A very basic example:
$email_message .="<table>";
$email_message .= "Product Manager: ".clean_string($ProductMgr)."\n";
.
.
.
$email_message .= "Production Expected Date: ".clean_string($ProdExpDate)."\n";
$email_message .= "</table>";
Add the header, rows, and columns as needed to get the layout you want if you wish to go the table route.
Basically you are just adding the html to the $email_message. You might find it easier to get the html working first, then use that as the template for the way you need to concatonate your $email_message strings to get the same result.
Good luck.
I have the following php email contact form, I want to make it possible for the users to include an image which would be sent together with the other text. for example instead of the first_name thing to have an image. thanks in advance!
<?php
if(isset($_POST['email'])) {
$email_to = "youremail#someone.com";
$email_subject = "yourSubject";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
You probably want to make an html email.
Add: $headers .= "Content-type: text/html\r\n";
And then use a standard img tag for your image
<img src="your_url />"
This is what you're looking for I believe. Please see section with the heading "Sending Email with Attachment"
http://webcheatsheet.com/php/send_email_text_html_attachment.php