By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

MIME header appearing in the email message body

P: 1
I have a problem sending php email using the code below. The MIME heades keep apearing in the message and the attachment appears as scribble text in the mesage body not as attachment. what could be the problem?

<?php
session_start();
require_once("includes/functions.php");
require_once("includes/dbconnect.php");
require_once("Mail/mailfunctions.php");



//function_to_be_applied($finaldest_email, $message, $subject, $fromname, $fromemail, $replyto )
function function_to_be_applied($finaldest_email, $key){
//require_once "Mail.php" ;
global $fromemail;
global $message;
global $fromname;
global $subject;
global $replyto;
global $seconds;
global $reprt;
global $headers;

$to = $finaldest_email;
$from = "".$fromname." <$fromemail>";
$subject = $subject;

if(mail($to, $subject, $message, $headers)) {
sleep($seconds);
$reprt .= "Message successfully sent to: ". $to."<br />";
} else {
$reprt .= "Message not successfully sent to: ". $to."<br />";
}

}

if(isset($_POST['submit']))
{
$errors_val = array();
$required_fields = array("subject", "fromemail", "message", "dest_email");
foreach($required_fields as $fieldname)
{
if( !isset($_POST[$fieldname]) || ( empty($_POST[$fieldname]) &&(!is_int($_POST[$fieldname])) ))
{
if($fieldname == "subject")
{$errors_val[0] = "-Sending email without SUBJECT is not allowed";}
if($fieldname == "fromemail")
{$errors_val[1] = "-Sending email without a FROM EMAIL is not allowed";}
if($fieldname == "message")
{$errors_val[2] = "-Sending an empty message is not allow";}
if($fieldname == "dest_email")
{$errors_val[3] = "-There must be at least one email in the destination email address";}

}
}
if(empty($errors_val)){
$errors = array();
if(false == validate_email(trim($_POST['fromemail']))){
$errors[0] = "FROM EMAIL is invalid";
}
if(false == validate_email(trim($_POST['replyto']))){
$errors[1] = "REPLY TO EMAIL is invalid";}
if(!is_numeric(trim($_POST['seconds']))){
$errors[2] = "Seconds between messages must be a number";}

$allowtypes = array("doc", "pdf", "txt", "zip", "gif", "jpeg", "jpg"); //the type of file can be attached
$max_file_size="100"; //describes the size that cab be attached

// checks that we have a file
if((!empty($_FILES["attachment"])) && ($_FILES["attachment"]["error"] == 0)) {
//set a variable $attached = 1
$attached = 1;

// basename -- Returns filename component of path
$filename = basename($_FILES['attachment']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
$filesize=$_FILES['attachment']['size'];
$max_bytes=$max_file_size*1024;

//Check if the file type uploaded is a valid file type.
if (!in_array($ext, $allowtypes)) {
$errors[3]="Invalid extension for your file: <strong>".$filename."</strong>";
unset($attached);
// check the size of each file
} elseif($filesize > $max_bytes) {
$errors[4]= "Your file: <strong>".$filename."</strong> is to big. Max file size is ".$max_file_size."kb.";
unset($attached);
}

}

if(empty($errors)){
//generate a unique boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

if(isset($_POST['seconds']) && ($_POST['seconds'] != ""))
{$seconds = $_POST['seconds'];}else{$seconds = 0.5;}
$subject = trim($_POST['subject']);
$fromname = trim($_POST['fromname']);
$fromemail = trim($_POST['fromemail']);
$from = stripslashes($fromname)."<".stripslashes($fromemai l).">";
$emailmessage = trim($_POST['message']);
$replyto = trim($_POST['replyto']);
$dest_email = trim($_POST['dest_email']);
$emailarray = explode("\r\n", $dest_email, 400);
$finaldest_email = array_unique($emailarray );

$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To:" . $replyto . "\r\n";
$headers .= "Mime-Version: 1.0\r\n";
$headers .= " Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";


//attachment
if(isset($attached)){
$fileatt = $_FILES["attachment"]["tmp_name"];
$fileatt_type = $_FILES["attachment"]["type"];
$fileatt_name = $_FILES["attachment"]["name"];
if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$data = file_get_contents($fileatt);

//$file = fopen($fileatt,'rb');
//$data = fread($file,filesize($fileatt));
//fclose($file);

// Base64 encode the file data
$finaldata = chunk_split(base64_encode($data));
}
$message = "--{$mime_boundary}\r\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "{$emailmessage}\r\n";
$message .= "--{$mime_boundary}\r\n";
$message .= "Content-Type: {$fileatt_type}; name=\"{$fileatt_name}\"\n\n".
"Content-Transfer-Encoding: base64\n\n";
$message .= "Content-Disposition: attachment; filename=\"{$fileatt_name}\"\n\n";

$message .= "{$finaldata} \r\n--{$mime_boundary}--\r\n";

}elseif(!isset($attached)){

$message = "--{$mime_boundary}\r\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\" \r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "{$emailmessage}\r\n";
$message .= "--{$mime_boundary}--\r\n";

}
$reprt = "Preparing to send message..<br />";

if( true == array_walk($finaldest_email, 'function_to_be_applied' )){
$numberofemailsent = count($finaldest_email);

}else{echo "No email sent";}

}else{$string = implode("<br /> -" , $errors); $error_message = $string; }



}else{$string = implode("<br /> -" , $errors_val); $error_message = $string; }



}

?>
Dec 5 '11 #1
Share this Question
Share on Google+
1 Reply


omerbutt
100+
P: 638
change this
Expand|Select|Wrap|Line Numbers
  1. $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
to this
Expand|Select|Wrap|Line Numbers
  1. "==Multipart_Boundary_x".md5(mt_rand())."x";
, try to convert the < and > to &lt; and &gt; , i wont recommend using them .
and apart from this you are using text/plain html where as your message contains attachment so there should be following format of your mail.
Expand|Select|Wrap|Line Numbers
  1. $userfile    =    "../docs/file.docx";
  2. // generate a random string to be used as the boundary marker
  3.         $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
  4.  
  5.         $mail_date=date('Ymdhis');
  6.         $headers = 'From:' .$from. "\r\n";
  7.         $headers .= 'Sender:' .$from. "\r\n";
  8.         if(IS_CC==true){$headers .= 'Cc:'.CC_MAIL. "\r\n";}
  9.         if(IS_BCC==true){$headers .= 'Bcc:'.BCC_MAIL. "\r\n";}
  10.          $headers.= "Return-Path: $from "."\r\n"."MIME-Version: 1.0\r\n" ."Content-Type: multipart/mixed;\r\n"." boundary=\"{$mime_boundary}\"";
  11. $message_body = " 
  12.         Dear $to,<br />
  13.  
  14.         ".$sender_name." has sent file(s) to you. Please download the attachment(s) with this email. <br />
  15.  
  16.  
  17.         ____________________________________________ Message from sender _____________________________________<br />
  18.  
  19.         ".$mail_message."<br />
  20.  
  21.         ______________________________________________________________________________________________________<br />
  22.  
  23.  
  24.         Thankyou,<br />
  25.         The ".$teamurl." Team<br />
  26.         www.".$siteUrl." "."\r\n"; 
  27. $message = "This is a multi-part message in MIME format.\n\n" ."--{$mime_boundary}\n" .
  28.         "Content-Type: text/html; charset=\"iso-8859-1\"\n" ."Content-Transfer-Encoding: 7bit\n\n" .
  29.  
  30.         $message_body ."\n\n";
  31.  $tmp_name = $userfile;
  32.           $name = $fileName;
  33.           $size = filesize($userfile);
  34.           $type="application/octet-stream ";
  35.  if (file_exists($tmp_name)){
  36.             // open the file for a binary read
  37.             $file = fopen($tmp_name,'rb');
  38.  
  39.             // read the file content into a variable
  40.             $data = file_get_contents($tmp_name,true) or die("Cant read the file");
  41.  
  42.             // close the file
  43.             fclose($file);
  44.  
  45.             // now we encode it and split it into acceptable length lines
  46.             $data = chunk_split(base64_encode($data));
  47.              // now we'll insert a boundary to indicate we're starting the attachment
  48.              // we have to specify the content type, file name, and disposition as
  49.              // an attachment, then add the file content.
  50.              // NOTE: we don't set another boundary to indicate that the end of the
  51.              // file has been reached here. we only want one boundary between each file
  52.              // we'll add the final one after the loop finishes.
  53.              $message .= "--{$mime_boundary}\n" ."Content-Type: {$type};\n" ." name=\"{$name}\"\n" ."Content-Disposition: attachment;\n" ." filename=\"{$fileatt_name}\"\n" ."Content-Transfer-Encoding: base64\n\n" .
  54.              $data . "\n\n";
  55.           }
  56. // here's our closing mime boundary that indicates the last of the message
  57.         $message.="--{$mime_boundary}--\n";
  58.         // now we just send the message
  59.         if(mail($to, $subject, $message, $headers)){
  60.             $myUser->emailLog($to,$call_id,$subject,'call-group-files-mailed',$mail_date);
  61.             echo "Mail Sent";
  62.         }else{
  63.             echo "Unable to send mail to the reciptant, please try later";
  64.         }
  65.  
regards,
Omer Aslam
Dec 5 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.