Everything works fine except I'm stuck with a problem which I'm not getting resolved. Perhaps, I'm not catching it correctly? Please let me know what's wrong with my code or what mistake I'm doing?
The Problem:
=============
* When I attach a document with an email:
- It sends email with attachment (works fine)
- It sends email with HTML format (works fine)
- It sends email with TEXT format (works fine)
What it does is that both HTML and TEXT contents are visible in email client. Why is this happening?
However, If I DO NOT attach any document. The email works perfectly fine.
Perhaps something wrong with following line somewhere or logic?:
$headers = "Content-Type: multipart/mixed; boundary=\"$mime_boundary\" \r\n"; $headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\" \r\n";
Here is the Full Code:
<?php
function get_file_extension($file) {
return array_pop(explode('.',$file));
}
function get_mimetype($value='') {
$ct['htm'] = 'text/html';
$ct['html'] = 'text/html';
$ct['txt'] = 'text/plain';
$ct['asc'] = 'text/plain';
$ct['bmp'] = 'image/bmp';
$ct['gif'] = 'image/gif';
$ct['jpeg'] = 'image/jpeg';
$ct['jpg'] = 'image/jpeg';
$ct['jpe'] = 'image/jpeg';
$ct['png'] = 'image/png';
$ct['ico'] = 'image/vnd.microsoft.icon';
$ct['mpeg'] = 'video/mpeg';
$ct['mpg'] = 'video/mpeg';
$ct['mpe'] = 'video/mpeg';
$ct['qt'] = 'video/quicktime';
$ct['mov'] = 'video/quicktime';
$ct['avi'] = 'video/x-msvideo';
$ct['wmv'] = 'video/x-ms-wmv';
$ct['mp2'] = 'audio/mpeg';
$ct['mp3'] = 'audio/mpeg';
$ct['rm'] = 'audio/x-pn-realaudio';
$ct['ram'] = 'audio/x-pn-realaudio';
$ct['rpm'] = 'audio/x-pn-realaudio-plugin';
$ct['ra'] = 'audio/x-realaudio';
$ct['wav'] = 'audio/x-wav';
$ct['css'] = 'text/css';
$ct['zip'] = 'application/zip';
$ct['pdf'] = 'application/pdf';
$ct['doc'] = 'application/msword';
$ct['bin'] = 'application/octet-stream';
$ct['exe'] = 'application/octet-stream';
$ct['class']= 'application/octet-stream';
$ct['dll'] = 'application/octet-stream';
$ct['xls'] = 'application/vnd.ms-excel';
$ct['ppt'] = 'application/vnd.ms-powerpoint';
$ct['wbxml']= 'application/vnd.wap.wbxml';
$ct['wmlc'] = 'application/vnd.wap.wmlc';
$ct['wmlsc']= 'application/vnd.wap.wmlscriptc';
$ct['dvi'] = 'application/x-dvi';
$ct['spl'] = 'application/x-futuresplash';
$ct['gtar'] = 'application/x-gtar';
$ct['gzip'] = 'application/x-gzip';
$ct['js'] = 'application/x-javascript';
$ct['swf'] = 'application/x-shockwave-flash';
$ct['tar'] = 'application/x-tar';
$ct['xhtml']= 'application/xhtml+xml';
$ct['au'] = 'audio/basic';
$ct['snd'] = 'audio/basic';
$ct['midi'] = 'audio/midi';
$ct['mid'] = 'audio/midi';
$ct['m3u'] = 'audio/x-mpegurl';
$ct['tiff'] = 'image/tiff';
$ct['tif'] = 'image/tiff';
$ct['rtf'] = 'text/rtf';
$ct['wml'] = 'text/vnd.wap.wml';
$ct['wmls'] = 'text/vnd.wap.wmlscript';
$ct['xsl'] = 'text/xml';
$ct['xml'] = 'text/xml';
$extension = get_file_extension($value);
if (!$type = $ct[strtolower($extension)]) {
$type = 'text/html';
}
return $type;
}
$to=$_POST['to'];
$subject=$_POST['subject'];
$message=$_POST['message'];
$docs=$_POST['docs'];
$MailerT = "my text here...";
$Mailer = "<p>My HTML <b>contents</b>here.</p>";
// explode email data
$GetAllEmails=explode(',',$to);
// process each email
foreach($GetAllEmails as $em){
$NewEmail = trim($em); // removes extra spaces from email
# Setup mime boundary
$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';
# Add in plain text version
$body .= "--$mime_boundary\n";
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$body .= "Content-Transfer-Encoding: 7bit\n\n";
$body .= $MailerT;
$body .= "\n\n";
# Add in HTML version
$body .= "--$mime_boundary\n";
$body .= "Content-Type: text/html; charset=\"UTF-8\"\n";
$body .= "Content-Transfer-Encoding: 7bit\n\n";
$body .= $Mailer;
$body .= "\n\n";
# if POST contains NO DOCUMENT then send HTML/TEXT email
if (count($docs) == 0) {
$headers = "Content-Type: multipart/alternative; boundary=\"$mime_boundary\" \r\n";
# if POST contains an attachment then send HTML/TEXT email with attachment
} else {
$headers = "Content-Type: multipart/mixed; boundary=\"$mime_boundary\" \r\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\" \r\n";
// Attachment Process
foreach($docs as $xDoc){
$thefile = "member_docs/$xDoc";
// if file exist, then attach it with email
if(file_exists($thefile)){
// MAIL HEADERS with attachment
$file = fopen($thefile, "rb");
$data = fread($file, filesize($thefile));
fclose($file);
$content = chunk_split(base64_encode($data));
// Attachment headers
$body .= "--".$mime_boundary."\r\n";
$body .= "Content-Type: ".get_mimetype($thefile)."; name=\"".basename($thefile)."\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"".basename($thefile)."\"\r\n\r\n";
$body .= $content."\r\n\r\n";
}
}
}
// This two steps to help avoid spam
$headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
# Finish off headers
$headers .= "From: $From\r\n";
$headers .= "X-Sender-IP: $_SERVER[SERVER_ADDR]\r\n";
$headers .= 'Date: '.date('n/d/Y g:i A')."\r\n";
//send the email
@mail($NewEmail, $subject, $body, $headers);
?>

New Topic/Question
Reply




MultiQuote




|