PHP contact form is sending encoded text on email body. The email contains
an attachment and some text from input fields. The message must be decoded before its sent. How can I decode the "$message" with right text that is inputted?
Please have a look at the code given below:
if (isset($_POST['submit'])) {
if ($_POST['email'] == '' || $_FILES['file_upload'] == '' || $_POST["fname"] == '' || $_POST["lname"] == '' || $_POST["message"] == '') {
echo '<p class="red-info">Please Fill All The Fields</p>';
} else {
$from_email = $_POST['email']; //from mail, it is mandatory with some hosts
$recipient_email = 'myemail#gmail.com'; //recipient email (most cases it is your personal email)
//Capture POST data from HTML form and Sanitize them,
$sender_fname = filter_var($_POST["fname"], FILTER_SANITIZE_STRING); //sender name
$sender_lname = filter_var($_POST["fname"], FILTER_SANITIZE_STRING); //sender name
$sender_phone_1 = filter_var($_POST["phone_1"], FILTER_SANITIZE_STRING); //sender name
$sender_phone_2 = filter_var($_POST["phone_2"], FILTER_SANITIZE_STRING); //sender name
$sender_phone_3 = filter_var($_POST["phone_3"], FILTER_SANITIZE_STRING); //sender name
$sender_phone = $sender_phone_1 . ' ' . $sender_phone_2 . ' ' . $sender_phone_3; //sender name
$reply_to_email = filter_var($_POST["email"], FILTER_SANITIZE_STRING); //sender email used in "reply-to" header
$subject = 'Contact Form'; //get subject from HTML form
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING); //message
/* //don't forget to validate empty fields
if(strlen($sender_name)<1){
die('Name is too short or empty!');
}
*/
//Get uploaded file data
$file_tmp_name = $_FILES['file_upload']['tmp_name'];
$file_name = $_FILES['file_upload']['name'];
$file_size = $_FILES['file_upload']['size'];
$file_type = $_FILES['file_upload']['type'];
$file_error = $_FILES['file_upload']['error'];
if ($file_error > 0) {
die('Upload error or No files uploaded');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("sanwebe");
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:" . $from_email . "\r\n";
$headers .= "Reply-To: " . $reply_to_email . "" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= "<br />First Name:" . $sender_fname;
$body .= "<br />Last Name:" . $sender_lname;
$body .= "<br />Phone:" . $sender_phone;
$body .= "<br />Message:";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary\r\n";
$body .= "Content-Type: $file_type; name=" . $file_name . "\r\n";
$body .= "Content-Disposition: attachment; filename=" . $file_name . "\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "X-Attachment-Id: " . rand(1000, 99999) . "\r\n\r\n";
$body .= $encoded_content;
$sentMail = mail($recipient_email, $subject, $body, $headers);
if (isset($sentMail)) //output success or failure messages
{
echo '<p class="green-info">Your Email Has Been Submitted!We will contact soon.</p>';
echo "<script>document.contact.reset();</script>";
header("location: contect.php");
} else {
die('Could not send mail! Please check your PHP mail configuration.');
}
}
}
Have you tried setting the content type ?
$headers .= "Content-Type: text/html;";
Changed to this and that worked thanks :)
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:" . $from_email . "\r\n";
$headers .= "Reply-To: " . $reply_to_email . "" . "\r\n";
$headers .= "Content-Type: multipart/alternative; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= "\n First Name:" . $sender_fname;
$body .= "\n Last Name:" . $sender_lname;
$body .= "\n Phone:" . $sender_phone;
$body .= "\n Message:" .$message;
$body .= chunk_split(base64_encode());
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 1 year ago.
I made a form with PHP to send mail, When I test it locally it runs fine I receive an email with all the information but when I put it live on a host it says SUCCESS but I never receive the mail. Maybe is something wrong with the code because i have an other form without the ATTACH file and it runs perfecrly
$filenameee = $_FILES['file']['name'];
$fileName = $_FILES['file']['tmp_name'];
$name = $_POST['name'];
$email = $_POST['email'];
$title=$_POST['title'];
$prototip=$_POST['prototip'];
$description = $_POST['descripton'];
$message ="Name = ". $name . "\r\n Email = " . $email . "\r\n \r\n Naslov = ".$title . "\r\n Opis = ".$description;
$subject ="My email subject";
$mailto = 'levchegochev#gmail.com'; //the email which u want to recv this email
$content = file_get_contents($fileName);
$content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filenameee . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "<script>
swal({
title: 'Ви благодариме за вашата апликација!',
text: 'Kе ве контактираме во рок од 24 часа',
icon: 'success',
button: 'Супер!',
});
</script>";
} else {
echo "<script>alert('Mail was not sent. Please try again later');</script>";
}
Updated answer
Set the following parameters in your html form:
<form action="your_target_php_file.php" method="post" enctype="multipart/form-data">
PHP file:
if ($_SERVER['REQUEST_METHOD'] == "POST") {
// file data from html element input[type='file']
$file = (object) [
'tmp_name' => $_FILES['file']['tmp_name'],
'name' => $_FILES['file']['name'],
'size' => $_FILES['file']['size'],
'type' => $_FILES['file']['type'],
'error' => $_FILES['file']['error'],
];
// other POST data
$post = (object) [
'name' => $_POST['name'],
'email' => $_POST['email'],
'title' => $_POST['title'],
'prototip' => $_POST['prototip'],
'description' => $_POST['descripton']
];
// email message
$message = "Name = ". $post->name . "\r\n Email = " . $post->email . "\r\n\r\n Naslov = ";
$message .= $post->title . "\r\n Opis = ".$post->description;
// email subject
$subject = "My email subject";
//the email which u want to recv this email
$mailto = 'levchegochev#gmail.com';
//read from the uploaded file
$handle = fopen($file->tmp_name, "r");
$content = fread($handle, $file->size);
fclose($handle);
$encoded = chunk_split(base64_encode($content));
// boundary
$boundary = md5("random");
// email headers
$headers[] = "MIME-Version: 1.0";
$headers[] = "From: " .explode("#", $post->email)[0]. " <{$post->email}>";
$headers[] = "Reply-To: {$mailto}";
$headers[] = "Content-Type: multipart/mixed;";
$headers[] = "boundary = {$boundary}";
//plain text
$body = "--{$boundary}\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: {$file->type}; name={$file->name}\r\n";
$body .="Content-Disposition: attachment; filename={$file->name}\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n";
// Attaching the encoded file with email
$body .= $encoded;
if (mail($post->email, $subject, $body, implode("\r\n", $headers)))
echo ("<script>
swal({
title: 'Ви благодариме за вашата апликација!',
text: 'Kе ве контактираме во рок од 24 часа',
icon: 'success',
button: 'Супер!',
});
</script>");
} else {
echo "<script>window.alert('Mail was not sent. Please try again later');</script>";
}
}
I changed your code a bit, I believe it will work for you.
I have a html form where i ask my users to fill in their personal and career details and attach their resume. I would like to get those details (form data and the attachment) sent to an email using php.... Any help will be much appreciated....
here is my code for sending mail... Using this i get the file attachment but not the other form data... Pls help where i am going wrong...
<?php
// Settings - working good
$name = "Name goes here";
$email = "test#gmail.com";
$to = "$name <$email>";
$from = "Vijay";
$subject = "Here is your attachment";
$mainMessage = "Hi, here's the file.";
$attachments = $_FILES['attachment'];
if(empty($_POST['title']) ||
empty($_POST['exp']) ||
empty($_POST['skill']) ||
empty($_POST['qual']) ||
empty($_POST['certf']) ||
empty($_POST['domain']) ||
empty($_POST['fname']) ||
empty($_POST['lname']) ||
empty($_POST['phone']) ||
empty($_POST['email']) ||
empty($_POST['csal']) ||
empty($_POST['job']) ||
// empty($_POST['file']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}$title = strip_tags(htmlspecialchars($_POST['title']));
$exp = strip_tags(htmlspecialchars($_POST['exp']));
$skill = strip_tags(htmlspecialchars($_POST['skill']));
$qual = strip_tags(htmlspecialchars($_POST['qual']));
$certf = strip_tags(htmlspecialchars($_POST['certf']));
$domain = strip_tags(htmlspecialchars($_POST['domain']));
$fname = strip_tags(htmlspecialchars($_POST['fname']));
$lname = strip_tags(htmlspecialchars($_POST['lname']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$csal = strip_tags(htmlspecialchars($_POST['csal']));
$job = strip_tags(htmlspecialchars($_POST['job']));
// File
//Get uploaded file data
$file_tmp_name = $_FILES['attachment']['tmp_name'];
$file_name = $_FILES['attachment']['name'];
$file_size = $_FILES['attachment']['size'];
$file_type = $_FILES['attachment']['type'];
$file_error = $_FILES['attachment']['error'];
if($file_error > 0)
{
die('Upload error or No files uploaded');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("sanwebe");
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($Message));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
$Message = "You have received a new resume from your website career application form.\n\n"."Here are the details:\n\n
Title: ".$title."\n
Experience: ".$exp."\n
Skill: ".$skill."\n
Qualification: ".$qual."\n
Domain: ".$domain."\n
First Name: ".$fname."\n
Last Name: ".$lname."\n
Phone: ".$phone."\n
Email: ".$email_address."\n\n
Current Salary: ".$csal."\n\n
Job: ".$job."\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"-{$mime_boundary}-\n";
// Send the email
if(mail($to, $subject, $Message, $headers)) {
echo "The email was sent.";
echo "$fileattname";
}
else {
echo "There was an error sending the mail.";
}
?>
You want to use proven, well tested libraries like Swiftmailer or Zend\Mail instead of writing the code like you do.
I have seen similar questions, but nothing directly on topic...
I have a multi part mail script with attachments. The attachments get sent fine, but the main body text, which is populated from a form, isn't sent. I tried sending with the attachment function commented out and the form elements went through. My code is:
if (empty($_POST['RadioGroup1'])){
echo "PLease select a version of message";
} else {$selected_msg = $_POST['RadioGroup1'];}
if (isset($_FILES) && (bool) $_FILES){
$files = array();
// Check for attachments
$questions = $_POST['questions'];
//loop through all the files
foreach($_FILES as $name=>$file){
// define the variables
$file_name = $file['name'];
$temp_name = $file['tmp_name'];
//check if file type allowed
$path_parts = pathinfo($file_name);
//move this file to server
$server_file = "reports/$path_parts[basename]";
move_uploaded_file($temp_name, $server_file);
//add file to array of file
array_push($files,$server_file);
}
// define mail var
$to = $email;
$from = "[server]";
$subject = "Closed Case: $case_id $casename";
$headers = "From: $from";
//define boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Header know about boundary
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n";
$headers .= " boundary=\"{$mime_boundary}\"";
// Define plain text mail
$message .= "--{$mime_boundary}\n";
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n" . $message . "\r\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
foreach($files as $file){
$aFile = fopen($file, "rb");
$data = fread($aFile,filesize($file));
fclose($aFile);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"}\n";
$message .= " name=\"$file\"\n";
$message .= "Content-Disposition: attachment;\n";
$message .= " filename=\"$file\"\n";
$message .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
} // END foreach attachment
$message .= $questions;
$message .= $selected_msg;
$ok = #mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p class=\"success\">mail sent to $to!</p>";
The script runs without errors and does sent the file attachments OR the body text. Any insight would be appreciated.
Instead of calling the form variables from within --- $message .= $questions;. $message .= $selected_msg; Hard code the variables into the $message.=
Like-- $message .= "Content-Transfer-Encoding: 7bit\r\n" . $questions . "\r\n" . $selected_msg . "\r\n";
If you like add both text and attachment in email, then used bellow code
$bodyMeaasge = 'Your Body Meaasge';
$filename = yourfile.pdf;
$path = '../';
$mpdf->Output($path.$filename,'F');
$file = $path . "/" . $filename;
$to = "senderemail#gmail.com";
$subject = "My Subject";
$random_hash = md5(date('r', time()));
$headers = "From:your#domain.com\r\n" .
"X-Mailer: PHP" . phpversion() . "\r\n" .
"MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $random_hash\r\n\r\n";
if(file_exists($file))
{
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
//plain text
$body = "--$random_hash\r\n";
$body .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($bodyMeaasge));
//attachment
$body .= "--$random_hash\r\n";
$body .="Content-Type: application/octet-stream; name=".$filename."\r\n";
$body .="Content-Disposition: attachment; filename=".$filename."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $content;
}else{
//plain text
$body = "--$random_hash\r\n";
$body .= "Content-Type: text/html; charset=utf-8\r\n"; // use different content types here
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($bodyMeaasge));
}
if (mail($to,$subject,$body,$headers)) {
header("Location:".$url.'&success=successfully mail send.');
} else {
header("Location:".$url.'&error=There was a problem sending the email.');
}
I want to send attachment with PHP email but not working.
I upload image it's my server's folder, i want to send 2.doc by email on gmail.
Any solution for that.
I getting message "Message sent! " but not received email or attachment file.
<?php
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\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/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$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."--";
// Messages for testing only, nobody will see them unless this script URL is visited manually
if (mail($mailto, $subject, "", $header)) {
echo "Message sent!";
} else {
echo "ERROR sending message.";
}
}
// Only accept POSTs from authenticated source
// EDIT FROM HERE DOWN TO
// CUSTOMIZE EMAIL
// File to attach
$my_file = "2.doc";
$my_path = ''; // $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
// Who email is FROM
$my_name = "Your Name (or) Your Business";
$my_mail = "myemail#yahoo.com";
$my_replyto = "myemail#yahoo.com";
// Whe email is going TO
$to_email = "toemail#gmail..com"; // Comes from Wufoo WebHook
// Subject line of email
$my_subject = "Your file has arrived!";
// Content of email message (Text only)
$requester = $_POST['Field101']; // Comes from Wufoo WebHook
$message = "Hey $requester, Your custom email message goes here";
// Call function to send email
mail_attachment($my_file, $my_path, $to_email, $my_mail, $my_name, $my_replyto, $my_subject, $message);
?>
Are you looking for something like this:
This should work for you for this scenario:
<?php
function mail_attachment($filename, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$body = '';
$headers = '';
$file = $filename;
$separator = md5(time());
$attachment = chunk_split(base64_encode(file_get_contents($file)));
$headers = "From: ".$from_name."<".$from_mail.">" . PHP_EOL;
$headers .= "Reply-To: ".$replyto . PHP_EOL;
$headers .= "MIME-Version: 1.0".PHP_EOL;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . PHP_EOL . PHP_EOL;
$body .= "Content-Transfer-Encoding: 7bit" . PHP_EOL;
$body .= "This is a MIME encoded message." . PHP_EOL;
$body .= "--" . $separator . PHP_EOL;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . PHP_EOL;
$body .= "Content-Transfer-Encoding: 8bit" . PHP_EOL . PHP_EOL;
$body .= $message . PHP_EOL;
$body .= "--" . $separator . PHP_EOL;
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . PHP_EOL;
$body .= "Content-Transfer-Encoding: base64" . PHP_EOL;
$body .= "Content-Disposition: attachment" . PHP_EOL . PHP_EOL;
$body .= $attachment . PHP_EOL;
$body .= "--" . $separator . "--";
if (mail($mailto, $subject, $body, $headers)) {
echo "Message sent!";
} else {
echo "ERROR sending message.";
}
}
$my_file = "2.doc";
$my_name = "Your Name (or) Your Business";
$my_mail = "myemail#yahoo.com";
$my_replyto = "replyemail#gmail.com";
$to_email = "replyemail#gmail.com";
$my_subject = "Your file has arrived!";
$requester = $_POST['Field101'];
$message = "Hey $requester, Your custom email message goes here";
mail_attachment($my_file, $to_email, $my_mail, $my_name, $my_replyto, $my_subject, $message);
?>
I am sending form data through a .swf file into this PHP page.(see below) When the email is sent, if there is an attachment the body of the message will not appear but if there is not an attachment the body appears just fine. I know that all of the variable names are correct because all of the data has been passed from the swf file. Is there something in the way I have set up the email that makes it work that way? Please let me know.
<?php
$to=$_POST['toEmail'];
$subject=$_POST['subject'];
$body = $_POST['messaggio'];
$nome = $_POST['nome'];
$email = $_POST['email'];
$attach = $_POST['attach'];
$headers = "From: $nome<" . $email . ">\n";
if ($attach == 1) {
$tmp_name = $_FILES['Filedata']['tmp_name'];
$type = $_FILES['Filedata']['type'];
$name = $_FILES['Filedata']['name'];
if(is_uploaded_file($tmp_name)){
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
$headers .= "Reply-To: <" . $email . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDARY_main_message\"\n";
$headers .= "X-Sender: $to <" . $to . ">\n";
$headers .= "Return-Path: <" . $email . ">\n";
$headers .= "This is a multi-part message in MIME format.\n";
$headers .= "------=MIME_BOUNDARY_main_message \n";
$headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDARY_message_parts\"\n";
$message = "------=MIME_BOUNDARY_message_parts\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
$message .= $body . "\n";
$message .= "\n";
$message .= "------=MIME_BOUNDARY_message_parts--\n";
$message .= "\n";
$message .= "------=MIME_BOUNDARY_main_message\n";
$message .= "Content-Type: application/octet-stream;\n\tname=\"" . $name . "\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment;\n\tfilename=\"" . $name . "\"\n\n";
$message .= $data;
$message .= "\n";
$message .= "------=MIME_BOUNDARY_main_message--\n";
mail($to, $subject, $message, $headers);
}
} else {
if(mail($to, $subject, $body, $headers)) {
echo "ok=1";
}
}
?>
Building MIME messages, especially with attachments, is painful. You'd be better off using something like PHPMailer, which will handle the whole business for you automatically... You just have to provide the content.
Beyond that, you're slurping the attachments into memory. How big are they? Are you exceeding the script's memory_limit?