473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

MIME header appearing in the email message body

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
1 2073
omerbutt
638 512MB
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

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

Similar topics

0
by: David Bolen | last post by:
I've noticed that using set_charset() on an email.Message instance will not replace any existing Content-Transfer-Encoding header but will install one if it isn't yet present. Thus, if you...
0
by: hlabbott | last post by:
Anyone know how to read the body of an S/MIME email message that is in Exchange? I can't find any suitable properties to access it via webdav and also tried to access it as an attachment but it...
1
by: Laertes | last post by:
Hi! just managed to generate auto emails using Access & Novell Groupwise. My question is quite simple ... Is there a way to format 'messagetext', i.e. the body of the email you send? e.g. how...
0
by: desi.american | last post by:
I'm using System.Web.Mail to send an email message from an ASP.NET web page. This is the main section of the code. //************* start code ***************************** string mailHost =...
1
by: Bill | last post by:
I have a group header that is appearing at both the beginning and end of the data in my report group. I have looked all through the settings and cannot figure out why this is happening. Can...
2
by: Slippy | last post by:
Hi, python newbie, struggling to learn here. I'm trying to write a simple program which posts messages to my google group (via email). I'm using smtplib, email and email.message to build and send a...
3
by: Steven Allport | last post by:
I am working on processing eml email message using the email module (python 2.5), on files exported from an Outlook PST file, to extract the composite parts of the email. In most instances this...
5
by: jimhill10 | last post by:
I have a perl script that creates an email attachment file from POST data on a web page. This works just fine. I want to customize the email body to contain all of the text data from the file...
4
by: canajien | last post by:
I need to have a php script pull data from a mysql table and send it in the body of an email I know I can get the script to display information in the browser by doing this: $i=0; while ($i...
3
by: Big T | last post by:
I have replaced our domain with example.com and the client's email with email@example.com - Any thoughts on how our MIME os malformed would be greatly appreciated. Thank you. Remote host...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.