Images not sent to inbox, using PHP mail() - php

I have an online property submission area in my website which lets users send properties in the form of text with images, to my email address:
But I'm not receiving the images. I only receive the text. I am trying to figure out why this is happening, but I couldn't find out anything that would cause this. Can someone help me find out the issue?
Code:
<?php
if (isset($_POST['propertystatus'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "contact#scale-property.com";
$email_subject = "New Property From Customer";
$email_subject_to_user = "Copy of your email";
$propery_Information = 'PROPERTY INFORMATION';
$propery_Location = 'PROPERTY LOCATION';
$contact_Information = 'CONTACT INFORMATION';
function died($error)
{
// your error code can go here
echo "We are very sorry, but there were errors found with the form you submitted.<br>";
echo $error . "<br />";
die();
}
// validation expected data exists
if (!isset($_POST['propertystatus']) || !isset($_POST['currencytype']) || !isset($_POST['currency']) || !isset($_POST['rooms']) || !isset($_POST['bathroom']) || !isset($_POST['kitchen']) || !isset($_POST['yard']) || !isset($_POST['housedesproperty']) || !isset($_POST['drop_1']) || !isset($_POST['drop_2']) || !isset($_POST['drop_3']) || !isset($_POST['region']) || !isset($_POST['street']) || !isset($_POST['name']) || !isset($_POST['emailadd']) || !isset($_POST['conemailadd']) || !isset($_POST['phone1']) || !isset($_POST['phone2']) || !isset($_FILES['uploadimages'])) {
died('You have not properly selected the fields.');
}
// start code of attachement
if (empty($_FILES['uploadimages']['name'])) {
echo 'You have\'nt Entered Value for upload field';
exit();
} else {
$attachment = $_FILES['uploadimages']['tmp_name'];
$attachment_name = $_FILES['uploadimages']['name'];
if (is_uploaded_file($attachment)) {
$fp = fopen($attachment, "rb");
$data = fread($fp, filesize($attachment));
$data = chunk_split(base64_encode($data));
fclose($fp);
}
$headers = "From: $email<$email>\n";
$headers .= "Reply-To: <$email>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "X-Priority: 3\n";
$headers .= "Return-Path: <$email>\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDRY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";
$message = "------=MIME_BOUNDRY_message_parts\n";
$message .= "Content-Type: text/html; charset=\"utf-8\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_message_parts--\n";
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message\n";
$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
$message .= $data; //The base64 encoded message
$message .= "\n";
$message .= "------=MIME_BOUNDRY_main_message--\n";
mail($email_to, $email_subject, $message, $headers);
}
// end code of attachment
$property_status = $_POST['propertystatus']; // required
$currency_type = $_POST['currencytype']; // required
$currency = $_POST['currency']; // required
$rooms = $_POST['rooms']; // required
$bathroom = $_POST['bathroom']; // required
$kitchen = $_POST['kitchen']; // required
$yard = $_POST['yard']; // required
$housedesproperty = $_POST['housedesproperty']; // required
$drop_1 = $_POST['drop_1']; // required
$drop_2 = $_POST['drop_2']; // required
$drop_3 = $_POST['drop_3']; // required
$region = $_POST['region']; // required
$street = $_POST['street']; // required
$name = $_POST['name']; // required
$emailadd = $_POST['emailadd']; // required
$conemailadd = $_POST['conemailadd']; // required
$phone1 = $_POST['phone1']; // required
$phone2 = $_POST['phone2']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp, $emailadd) && !preg_match($email_exp, $conemailadd)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$email_message = "Property details below.\n\n";
function clean_string($string)
{
$bad = array(
"content-type",
"bcc:",
"to:",
"cc:",
"href"
);
return str_replace($bad, "", $string);
}
$email_message .= "" . clean_string($propery_Information) . "\n";
$email_message .= "--------------------------------------------" . "\n";
$email_message .= "Property Status: " . clean_string($property_status) . "\n";
$email_message .= "Total Price: " . clean_string($currency_type) . " " . (number_format($currency, 2, '.', ',')) . "\n";
$email_message .= "Rooms: " . clean_string($rooms) . "\n";
$email_message .= "Bathrooms: " . clean_string($bathroom) . "\n";
$email_message .= "Bathrooms: " . clean_string($kitchen) . "\n";
$email_message .= "Bathrooms: " . clean_string($yard) . "\n";
$email_message .= "Description: " . clean_string($housedesproperty) . "\n\n";
$email_message .= "" . clean_string($propery_Location) . "\n";
$email_message .= "--------------------------------------------" . "\n";
$email_message .= "Province: " . clean_string($drop_1) . "\n";
$email_message .= "District: " . clean_string($drop_2) . "\n";
$email_message .= "PD(Nahya): " . clean_string($drop_3) . "\n";
$email_message .= "Bathrooms: " . clean_string($region) . "\n";
$email_message .= "Street: " . clean_string($street) . "\n\n";
$email_message .= "" . clean_string($contact_Information) . "\n";
$email_message .= "--------------------------------------------" . "\n";
$email_message .= "Name: " . clean_string($name) . "\n";
$email_message .= "Email ID: " . clean_string($emailadd) . "\n";
$email_message .= "Con-Email ID: " . clean_string($conemailadd) . "\n";
$email_message .= "Phone(1): " . clean_string($phone1) . "\n";
$email_message .= "Phone(2): " . clean_string($phone2) . "\n";
// create email headers
$headers = 'From: ' . $emailadd . "\r\n" . 'Reply-To: ' . $emailadd . "\r\n" . 'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
#mail($emailadd, $email_subject_to_user, $email_message, "From: $email_to");
?>
<!-- include your own success html here -->
Your Property has been Posted please check your email address.
<?php
}
?>

Try this,this for multiple image attachment
http://www.emanueleferonato.com/2008/07/22/sending-email-with-multiple-attachments-with-php/

Related

PHP form giving Errors

This is my PHP code which I'm using and Error is coming between the First Echo command in the code. Can anyone please Help?
<?php
if(isset($_POST["submit"])){
// Checking For Blank Fields..
if($_POST["name"]==""||$_POST["email"]==""||$_POST["phone_number"]==""||$_POST["city"]==""||$_POST["gre_score"]==""||$_POST["toefl_score"]==""||$_POST["eng_marks"]==""||$_POST["country"]==""||$_POST["course_type"]==""||$_POST["department"]==""||$_POST["exp"]==""||$_POST["ug_details"]==""||$_POST["pg_details"]){
echo "Something Went Please Try Again!";
}else{
// Check if the "Sender's Email" input field is filled out
$email=$_POST['email'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Invalid Sender's Email";
}
else{
$name = '$name';
$subject = 'Registration Form';
$headers = 'From: '. $email . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Phone Number:</strong> </td><td>" . strip_tags($_POST['phone_number']) . "</td></tr>";
$message .= "<tr><td><strong>City: </strong></td><td>" . strip_tags($_POST['city']) . "</td></tr>";
$message .= "<tr><td><strong>GRE Score:</strong> </td><td>" . strip_tags($_POST['gre_score']) . "</td></tr>";
$message .= "<tr><td><strong>TOEFL/IELTS Score:</strong> </td><td>" . strip_tags($_POST['toefl_score']) .
"";
$message .= "Engineering Marks: " . strip_tags($_POST['eng_marks']) .
"";
$message .= "Country Planning: " . strip_tags($_POST['country']) . "";
$message .= "Department: " . strip_tags($_POST['department']) . "";
$message .= "Job Experience: " . strip_tags($_POST['exp']) . "";
$message .= "Under Graduation Details: " . strip_tags($_POST['ug_details']) .
"";
$message .= "Post Graduation Details:" . strip_tags($_POST['pg_details']) .
"";
$message .= "Course Selected: " . strip_tags($_POST['course_type']) .
"";
$message .= "</body></html>";
// Send Mail By PHP Mail Function
mail("yash119ambaskar#gmail.com", $subject, $message, $headers);
echo "Your mail has been sent successfuly ! You will be contacted Shortly!";
}
}
}
?>
Hello Syntax error in your first if condition.Your post pg_details, you have not compare it with empty string.
if($_POST["name"]==""||$_POST["email"]==""||$_POST["phone_number"]==""||$_POST["city"]==""||$_POST["gre_score"]==""||$_POST["toefl_score"]==""||$_POST["eng_marks"]==""||$_POST["country"]==""||$_POST["course_type"]==""||$_POST["department"]==""||$_POST["exp"]==""||$_POST["ug_details"]==""||$_POST["pg_details"] == ""){

mail function does not echo value in email

this php code is correct.this code having no errors but when user submits form ,in received email only shows file attachment in email.it does not show all input fields values. what is required to do ???
<?php
if(isset($_FILES) && (bool) $_FILES) {
$AllowedExtensions = ["pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt"];
$files = [];
$server_file = [];
foreach($_FILES as $name => $file) {
$file_name = $file["name"];
$file_temp = $file["tmp_name"];
foreach($file_name as $key) {
$path_parts = pathinfo($key);
$extension = strtolower($path_parts["extension"]);
if(!in_array($extension, $AllowedExtensions)) { die("Extension not allowed"); }
$server_file[] = "uploads/{$path_parts["basename"]}";
}
for($i = 0; $i<count($file_temp); $i++) { move_uploaded_file($file_temp[$i], $server_file[$i]); }
}
$from = "example#gmail.com";
$headers = "From: $from";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n";
$message .= "--{$mime_boundary}\n";
$FfilenameCount = 0;
for($i = 0; $i<count($server_file); $i++) {
$afile = fopen($server_file[$i],"rb");
$data = fread($afile,filesize($server_file[$i]));
fclose($afile);
$data = chunk_split(base64_encode($data));
$name = $file_name[$i];
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
}
/** Your submit block **/
if(isset($_POST['submit']))
{
$name = htmlspecialchars($_REQUEST['name']);
$email = htmlspecialchars($_REQUEST['email']);
$mobile = htmlspecialchars($_REQUEST['mobile']);
$company = htmlspecialchars($_REQUEST['company']);
$qty = htmlspecialchars($_REQUEST['qty']);
$msg = htmlspecialchars($_REQUEST['msg']);
$subject = "Order Information";
$message .= "Name: " . $name . "\n";
$message .= "Email: " . $email . "\n";
$message .= "ContactNo: " . $mobile . "\n";
$message .= "Company: " . $company . "\n";
$message .= "Quantity: " . $qty . "\n";
$message .= "Message: " . $msg . "\n";
if(mail($from, $subject, $message, $headers)) {
echo 'thank you';
}
else {
echo 'error';
}
}
?>
I think you need to convert part where you send post data to multipart also. Otherwise your mail client will probably ignore it (I think you may find it at the bottom of mail in "view mail source" mode).
It should be something like (only $_POST part):
if(isset($_POST['submit']))
{
$name = htmlspecialchars($_REQUEST['name']);
$email = htmlspecialchars($_REQUEST['email']);
$mobile = htmlspecialchars($_REQUEST['mobile']);
$company = htmlspecialchars($_REQUEST['company']);
$qty = htmlspecialchars($_REQUEST['qty']);
$msg = htmlspecialchars($_REQUEST['msg']);
$to="amar.ghodke30#gmail.com";
$subject = "Order Information";
$message .= "--{$mime_boundary}\n"; //$mime_boundary should be the same as for attachments.
$message .= "Content-type: text/plain;charset=utf-8\n\n";
$message .= "Name: " . $name . "\n";
$message .= "Email: " . $email . "\n";
$message .= "ContactNo: " . $mobile . "\n";
$message .= "Company: " . $company . "\n";
$message .= "Quantity: " . $qty . "\n";
$message .= "Message: " . $msg . "\n";
$message .= "--{$mime_boundary}\n\n";
if(mail($from, $subject, $message, $headers)) {
echo 'thank you';
}
else {
echo 'error';
}
}

Send checkbox value to email

I have three checkboxes for requesting catalogs. I would like to get value for all three of them in each email.
Here is my HTML:
<input type="checkbox" name="catalog" value="Grower"/> Grower Supply Catalog <br><br>
<input type="checkbox" name="catalog" value="Specialty"/> Specialty Catalog<br><br>
<input type="checkbox" name="catalog" value="Plant"/> Plant Source Catalog
Here is my PHP:
$name = #$_POST["name"];
$email = #$_POST["email"];
$street = #$_POST["street"];
$city = #$_POST["city"];
$state = #$_POST["state"];
$zip = #$_POST["zip"];
$email = #$_POST["email"];
$phone = #$_POST["phone"];
$message = #$_POST["comment"];
$catalog =#$_POST["catalog"];
foreach($_POST['catalog'] as $value) {
$check_msg .= "Checked: $value\n";
}
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
$mailBody = "You have been contacted by $name" . PHP_EOL . PHP_EOL;
$mailBody .= (!empty($company))?'Company: '. PHP_EOL.$company. PHP_EOL . PHP_EOL:'';
$mailBody .= (!empty($quoteType))?'project Type: '. PHP_EOL.$quoteType. PHP_EOL . PHP_EOL:'';
$mailBody .= "Street :" . PHP_EOL;
$mailBody .= $street . PHP_EOL . PHP_EOL;
$mailBody .= "City :" . PHP_EOL;
$mailBody .= $city . PHP_EOL . PHP_EOL;
$mailBody .= "State :" . PHP_EOL;
$mailBody .= $state . PHP_EOL . PHP_EOL;
$mailBody .= "Zip :" . PHP_EOL;
$mailBody .= $zip . PHP_EOL . PHP_EOL;
$mailBody .= "Phone :" . PHP_EOL;
$mailBody .= $phone . PHP_EOL . PHP_EOL;
$mailBody .= $check_msg .= "Catalog : $catalog\n";
$mailBody .= "Message :" . PHP_EOL;
$mailBody .= $message . PHP_EOL . PHP_EOL;
$mailBody .= "You can contact $name via email, $email.";
$mailBody .= (isset($phone) && !empty($phone))?" Or via phone $phone." . PHP_EOL . PHP_EOL:'';
if(mail($to, $subject, $mailBody, $headers)){
echo '<div class="alert alert-success">Success! Your message has been sent.</div>';
}
}
How can I send the checkbox values to email?
Use array syntax for those element names to send those values as an array:
<input type="checkbox" name="catalog[]" value="Grower"/> Grower Supply Catalog <br><br>
<input type="checkbox" name="catalog[]" value="Specialty"/> Specialty Catalog<br><br>
<input type="checkbox" name="catalog[]" value="Plant"/> Plant Source Catalog
I think You Should type properly foreach() method, type in camelCase Like this forEach()

PHP Header not redirecting from Form Submit action page

On submitting form, user is taken to following action page. On this page email is sent along with attachment.
I am receiving email and attachment but header('Location: ...') is not working. Action page is not redirecting and keeps on showing loading sign in broweser. On debugging found no errors on page.
*This problem only comes when a file is attached.
PHP form processing page:-
<?php
require_once 'settings.php';
if (( isset($_POST)) && ( empty($_POST))) {
header('Location: ../career.html');
die ;
}
foreach ($_POST as $key => $value) {
if (empty($value)) {
header('Location: ../career.html#error');
die ;
}
if (ini_get('magic_quotes_gpc')) {
$value = stripslashes($value);
}
$val[$key] = htmlspecialchars(strip_tags(trim($value)));
}
$tmp = date('r');
$message = "<!DOCTYPE html><html><body>";
$message .= "<p><strong>Name: </strong>{$val['name']}</p>";
$message .= "<p><strong>Email: </strong>{$val['email']}</p>";
$message .= "<p><strong>Phone: </strong>{$val['phone']}</p>";
$message .= "<p><strong>Work Experience: </strong>{$val['workex']}</p>";
$message .= "<p><strong>Career: </strong>{$val['career']}</p><br>";
$message .= "<p> <i> This form was submited from {$_SERVER["HTTP_HOST"]} on $tmp by IP {$_SERVER['REMOTE_ADDR']} </i> </p>";
$message .= "</body></html>";
$uid = md5(uniqid(time()));
$header = "From: " . $val['name'] . " <" . $val['email'] . ">\r\n";
$header .= "Reply-To: " . $val['name'] . " <" . $val['email'] . ">\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--" . $uid . "\r\n";
$header .= "Content-type:text/html; charset=utf-8\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message . "\r\n\r\n";
$header .= "--" . $uid . "\r\n";
if (isset($_FILES["file"]["name"]) && !empty($_FILES["file"]["name"])) {
if ($_FILES["file"]["size"] > $fileSize) {
header('Location: ../career.html?#error-size');
die ;
}
$filename = $_FILES["file"]["name"];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (!in_array($ext, $fileType)) {
header('Location: ../career.html?#error-type');
die ;
}
$content = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"])));
$header .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n";
$header .= $content . "\r\n\r\n";
$header .= "--" . $uid . "--";
}
if (mail($mailTo, $careerSubject, $message, $header)) {
header('Location: ../career.html#success');
die ;
} else {
header('Location: ../career.html?#error');
die ;
}
?>
try remove "..", like header('Location: /career.html#error');

sending all data from email with require field

I'm creating a user require form in my site. For this I put some validation on compulsory fields, and when a user fills in the form and presses submit and validation is correct then I receive a email on my email address.
But now I would like all user information in the email, like name, city, budget etc... so what changes do I need to make in my email.php script?
If some fields are not compulsory and the user doesn't fill them in, can they affect my script?
My script is:
<?php
$to = "test#networkers.in";
$subject = "a new requiremnet come!";
$message = "Hi,\n\nyou get a new require";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "Sender-IP: " . $_SERVER["SERVER_ADDR"] . "\r\n";
$headers .= "From: " . stripslashes($name) . " <" . $email . ">" . "\r\n";
$headers .= "Priority: normal" . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
$sent = mail($to, $subject, $message, $headers) ;
if ($sent) {
echo "Your mail was sent successfully";
} else {
echo "We encountered an error sending your mail";
}
?>
and the data I recieve is:
$name = $_POST['fname'].' '.$_POST['lname'];
$email = $_POST['mail'];
$phone = $_POST['ph'];
$country = $_POST['country'];
$pt = $_POST['pt'];
$cwl = $_POST['cwl'];
$dyhyows = $_POST['dyhyows'];
$pb = $_POST['pb'];
$bpd = $_POST['bpd'];
$hdyhau = $_POST['hdyhau'];
You can add fields to the message body by concatenating them like so:
$message = "Hi,\n\nyou get a new require";
$message .= "\n Name: " . $name;
$message .= "\n Email: " . $email;
$message .= "\n Phone: " . $phone;
$message .= "\n Country: " . $country;
$message .= "\n pt: " . $pt;
$message .= "\n cwl: " . $cwl;
$message .= "\n dyhyows: " . $dyhyows;
$message .= "\n pb: " . $pb;
$message .= "\n bpd: " . $bpd;
$message .= "\n hdyhau: " . $hdyhau
Any fields that weren’t filled in by the user will simply be blank.

Categories