Php file upload from form to email - php

I am trying to get a file to upload as part of a contact form, i would like the uploaded file to end up as an attachement to an email that i can then download once i have received said email. I have got the form to work but instead of coming through as an attachment my file comes through like this;
--==Multipart_Boundary_xc99bf5ef644b3e51650e68e4b9538cffx
Content-Type: application/pdf;
name="360x260mm Side Seam Pocket 01 - 1up B2.pdf"
Content-Transfer-Encoding: base64
JVBERi0xLjUNJeLjz9MNCjggMCBvYmoNPDwvTWV0YWRhdGEgOSAwIFIvT0NQcm9wZXJ0aWVzPDwv
RDw8L09OWzEwIDAgUl0vT3JkZXIgMTEgMCBSL1JCR3JvdXBzW10+Pi9PQ0dzWzEwIDAgUl0+Pi9Q
YWdlcyAyIDAgUi9UeXBlL0NhdGFsb2c+Pg1lbmRvYmoNOSAwIG9iag08PC9MZW5ndGggMTM0NTEv
U3VidHlwZS9YTUwvVHlwZS9NZXRhZGF0YT4+c3RyZWFtDQo8P3hwYWNrZXQgYmVnaW49Iu+7vyIg
aWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/Pgo8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2Jl
Om5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjAtYzA2MCA2MS4xMzQ3NzcsIDIw
MTAvMDIvMTItMTc6MzI6
Here is my php;
<?php
if(isset($_POST['email'])) {
$email_to = "matthew.p#cawthornes.co.uk";
$email_subject = "website html form submissions";
function died($error) {
// error code
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['service']) ||
!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
$service = $_POST['service']; // not required
$comments = $_POST['comments']; // required
/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
$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);
}
if (file($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
}
$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 .= "Service: ".clean_string($service)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
/* Adding attchment-file to message*/
$email_message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\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);
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
<!-- success html -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
die();
?>
Thanks in advance.

Why dont you use Swift Mailer
Docs : Swift Mailer Documentation
Downloads : Swift-5.1.0
require_once 'lib/swift_required.php';
// Create the message
$message = Swift_Message::newInstance()
// Give the message a subject
->setSubject('Your subject')
// Set the From address with an associative array
->setFrom(array('john#doe.com' => 'John Doe'))
// Set the To addresses with an associative array
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
// Give it a body
->setBody('Here is the message itself')
// And optionally an alternative body
->addPart('<q>Here is the message itself</q>', 'text/html')
// Optionally add any attachments
->attach(Swift_Attachment::fromPath('my-document.pdf'));

Related

Email showing Array instead of image

I have a contact from that allows users to add an attachment but the email I receive shows Array where the image id should be.
Form details below.
First Name: gffffffffffffffffffffffffffffffffffffffffffffff
Last Name: williams
Email: jessie747williams#gmail.com
Subject: hello
Image: Array (HERE) shousd show the image like apple.jpg
Comments: fd
//Settings
$max_allowed_file_size = 100; // size in KB
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp");
//Validations
if($size_of_uploaded_file > $max_allowed_file_size )
{
$errors .= "\n Size of file should be less than $max_allowed_file_size";
}
//------ Validate the file extension -----
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++)
{
if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
{
$allowed_ext = true;
}
}
if(!$allowed_ext)
{
$errors .= "\n The uploaded file is not supported file type. ".
" Only the following file types are supported: ".implode(',',$allowed_extensions);
}
if(!empty($_FILES['uploaded_file']))
{
$path = "uploads/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
header("Location: https://www.website.com/thank-you-image/");
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "xxx#gmail.com";
$email_subject = "Add my image to xxx";
header("Location: https://www.website.com/thank-you-image/");
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['subject']) ||
!isset($_POST['email']) ||
!isset($_FILES['uploaded_file']) ||
!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
$subject = $_POST['subject']; // required
$uploaded_file = $_FILES['uploaded_file']; // 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(!preg_match($string_exp,$subject)) {
$error_message .= 'The Subject 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 .= "Subject: ".clean_string($subject)."\n";
$email_message .= "Image: ".clean_string($uploaded_file)."\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.
So change
"Image: ".clean_string($uploaded_file)."\n";
to
"Image: ".clean_string($uploaded_file['name'])."\n";
Update:
Also, add <script> and </script> to your $bad variable in clean_string() function.
$bad = array("content-type","bcc:","to:","cc:","href","<script>","</script>");
Although email service providers strip Javascript code, but just an extra security measure.
Simply change:
$uploaded_file = $_FILES['uploaded_file'];
to
$uploaded_file = $_FILES['uploaded_file']['name'];

PHP Email form bold for headings on email message items

I have a php form that sends an email, however I would like to put the headings of each section in bold but can't seem to get it to work from everywhere I've searched.
The form:
$email_message .= "Name: ".clean_string($firstlastname)."\n\n";
$email_message .= "Email: ".clean_string($email_from)."\n\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n\n";
$email_message .= "Property Type: ".clean_string($ptype)."\n\n";
$email_message .= "From Address: ".clean_string($faddress)."\n\n";
$email_message .= "To Address: ".clean_string($taddress)."\n";
$email_message .= "Moving Items Description: ".clean_string($itemsdescription)."\n\n";
$email_message .= "Dissasembling Requirements: ".clean_string($disassemble)."\n\n";
$email_message .= "Packaging Requirements: ".clean_string($packaging)."\n\n";
$email_message .= "Preferred Date and Time: ".clean_string($dateandtime)."\n\n";
$email_message .= "Property Access: ".clean_string($paccess)."\n\n";
$email_message .= "Stair Count: ".clean_string($staircount)."\n\n";
$email_message .= "Message: ".clean_string($message)."\n\n";
// create email headers
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\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);
?>
I've tried adding the bold and strong tags to the headings;
$email_message .= "<b>Name:</b> ".clean_string($firstlastname)."\n\n";
Still not working, including setting it outside of the heading of the text.
Edit: this is the full form
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email#email.com";
$email_subject = "Contact Form Submission";
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['firstlastname']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['ptype']) ||
!isset($_POST['faddress']) ||
!isset($_POST['taddress']) ||
!isset($_POST['itemsdescription']) ||
!isset($_POST['disassemble']) ||
!isset($_POST['packaging']) ||
!isset($_POST['dateandtime']) ||
!isset($_POST['paccess']) ||
!isset($_POST['staircount']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$firstlastname = $_POST['firstlastname'];
$email_from = $_POST['email'];
$telephone = $_POST['telephone'];
$ptype = $_POST['ptype'];
$faddress = $_POST['faddress'];
$taddress = $_POST['taddress'];
$itemsdescription = $_POST['itemsdescription'];
$disassemble = $_POST['disassemble'];
$packaging = $_POST['packaging'];
$dateandtime = $_POST['dateandtime'];
$paccess = $_POST['paccess'];
$staircount = $_POST['staircount'];
$message = $_POST['message'];
$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,$firstlastname)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 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 .= "Name: ".clean_string($firstlastname)."\n\n";
$email_message .= "Email: ".clean_string($email_from)."\n\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n\n";
$email_message .= "Property Type: ".clean_string($ptype)."\n\n";
$email_message .= "From Address: ".clean_string($faddress)."\n\n";
$email_message .= "To Address: ".clean_string($taddress)."\n";
$email_message .= "Moving Items Description: ".clean_string($itemsdescription)."\n\n";
$email_message .= "Dissasembling Requirements: ".clean_string($disassemble)."\n\n";
$email_message .= "Packaging Requirements: ".clean_string($packaging)."\n\n";
$email_message .= "Preferred Date and Time: ".clean_string($dateandtime)."\n\n";
$email_message .= "Property Access: ".clean_string($paccess)."\n\n";
$email_message .= "Stair Count: ".clean_string($staircount)."\n\n";
$email_message .= "Message: ".clean_string($message)."\n\n";
// create email headers
$headers = "Content-Type: text/html; charset=ISO-8859-1\r\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);
?>
<!-- include your own success html here -->
<script>window.location.replace("index.html");</script>
<?php
}
?>
You missed a dot to append more headers. This overwrites your Content-Type and that is why it is parsing the email as text and not has HTML
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\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);
?>
Could you try :
$email_message .= "<strong>Name:</strong> ".clean_string($firstlastname)."\n\n";
or
$email_message .= "<b>Name:</b> ".clean_string($firstlastname)."\n\n";
or
$email_message .= "<p style="font-weight: bold;">Name:</b> ".clean_string($firstlastname)."\n\n";
Use PHP Mailer plugin.
Download Plugin in following link:
https://github.com/PHPMailer/PHPMailer/releases/tag/v5.2.26
$mail_message='Dear '.$row[0]['branch_name'].','. "\r\n";
$mail_message.='Thanks for contacting regarding to forgot password,<br> Your <b>Password</b> is <b>'.$passwordplain.'</b>'."\r\n";
$mail_message.='<br>Please Update your password.';
$mail_message.='<br>Thanks & Regards';
$mail_message.='<br>Test group of company';
date_default_timezone_set('Etc/UTC');
require FCPATH.'application/libraries/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
// $mail->isSMTP();
$mail->SMTPSecure = "ssl";
$mail->Debugoutput = 'html';
$mail->Host = "(HOST SERVER)";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "(USERNAME)";
$mail->Password = "(PASSWORD)";
$mail->setFrom('(FROM MAIL ADDRESS)', 'Test group of company');
$mail->IsHTML(true);
$mail->addAddress($email);
$mail->Subject = 'New password for login';
$mail->Body = $mail_message;
if (!$mail->send()) {
$data['message_display'] = 'Failed to send password, please try again!';
$this->load->view('branch/forgotPassword', $data);
} else {
$data['message_display'] = 'Password sent to your email!';
$this->load->view('branch/forgotPassword', $data);
}

A form to send data to an email and send an confirmation email [duplicate]

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 Contact Form - Sending email to multiple recipients

How would I go about making this form able to send to multiple recipients? At the moment it's only allowing me to send it to 1 email only, and when I try typing multiple in (e.g "user1#example.com, user2#example.com") it returns the message for when it's invalid. What do I need to do to fix this?
EDIT: The user has to input the email address it wants to sent to, but it only works with 1, which is why I'm asking for help on how I can edit the code to work with multiple emails/recipients and to be separated with a comma and space.
Here is the code
<?php
if(isset($_POST['email'])) {
$email_to = array($_GET["celebrant_emails"]);
$email_subject = "Email from website Contact Form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the email you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go <a href='http://celebrantsaustralia.asn.au/celebrants-trial.htm'>back</a> 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['comments']) ||
!isset($_POST['celebrant_emails'])) {
died('We are sorry, but there appears to be a problem with the email you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required
$celebrant_emails = $_POST['celebrant_emails']; // 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 Message you entered does not appear to be valid.<br />';
}
if(!preg_match($email_exp,$celebrant_emails)) {
$error_message .= 'The Celebrant Email(s) you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Email 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 .= "Message: ".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 -->
<html>
<head>
<title>Email sent!</title>
</head>
<body>
<b>Thank you for contacting us. We will be in touch with you shortly.</b>
<p>(Page will auto-direct in a moment, if it doesn't click here)</p>
</body>
</html>
<?php
}
?>
There are numerous ways of doing this.
$email_to = "jhewitt#amleo.com,some#other.com,yet#another.net";
$email_to = 'Mary <mary#example.com>, Kelly <kelly#example.com>';
If you need to add emails as CC or BCC, add the following part in the variable you use as for your header :
$headers .= "CC: sombodyelse#noplace.com".PHP_EOL;
$headers .= "BCC: hidden#special.com".PHP_EOL;
Source: PHP form send email to multiple recipients
Also check here: Example #1 Sending mail.
$email=$_POST['email']; // write multiple emails with comma's
$emails=explode(',', $email); // explode email with comma's
foreach($emails as $one_email)
{
$touser=$one_email;
$subjectAdmin= "subject";
$headersAdmin = "From: noreply#talentswype.com\r\n";
$headersAdmin .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$messageuser ='msg here';
$emailSenduser = mail($touser,$subjectAdmin,$messageuser,$headersAdmin);
}
Write one mail function per email.
mail('example1#example.com', 'My Subject', $message);
mail('example2#example.com', 'My Subject', $message);
mail('example3#example.com', 'My Subject', $message);
mail('example4#example.com', 'My Subject', $message);
mail('example5#example.com', 'My Subject', $message);
or
foreach($mail as $mails ){
mail($mail, 'My Subject', $message);
}

Change PHP Form to Permanent Sender

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";

Categories