Page 1 of 1

Sending multipart/alternative emails with attachments

Posted: Sat Apr 09, 2005 10:16 pm
by clem_c_rock
Hello,
Is is possible to send a multipart/alternative html mail w/ attachments at the same time?

Here's some of the headings I have used w/ attacments:

Code: Select all

$border_random = md5(time()); 
$mail_boundary = "x{$border_random}x";

$headers  = "From: $from \r\n" ;
$headers .= "To: {$from}\r\n";
$headers .= "Reply-to: {$from}\r\n";    

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; \r\n";
$headers .= " boundary=\"".$mime_boundary."\"";

$message .= "This is a multi-part message in MIME format.\r\n";
$message .= "\r\n";
$message .= "--".$mime_boundary."\r\n";

$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit \r\n";
$message .= "\r\n";
$message .= "<h1>$_REQUEST[form_message]</h1> \r\n";
$message .= "--".$mime_boundary."\r\n";

if(is_uploaded_file( $file_attachment ) ) 
{
       $fp = fopen($file_attachment,"rb");
       $file_data = fread($fp,$file_attachment_size);
       fclose($fp);
       $file = base64_encode($file_data);
       $file = chunk_split($file);
       $message .= "Content-Type: application/octet-stream;\r\n";
       $message .= "Content-type: {$file_attachment_type}; name=\"$file_attachment_name\"\r\n";
       $message .= "Content-Transfer-Encoding:base64 \r\n";
       $message .= "Content-Disposition: attachment; filename=\"$file_attachment_name\"\r\n";
       $message .= $file."\r\n";
}
 
$message .= "--{$mail_boundary}--\r\n";
This has worked great except for the fact that it doesn't seem to send images properly.

The next one sends a multipart/alternative email successfully:

Code: Select all

$mime_boundary = "==Multipart_Boundary_x".md5(mt_rand())."x";

$headers  = "From: $from\r\n" ;
$headers .= "Cc: $bcc  \r\n";
$headers .= "Bcc: $bcc \r\n";
$headers .= "MIME-Version: 1.0\r\n" ;
$headers .= "Content-Type:multipart/alternative;\n";
$headers .= " boundary=\"{$mime_boundary}\"\r\n";

$message =  "This is a multi-part message in MIME format.\n\n";
$message .= "--{$mime_boundary}\n" ;
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n" ;
$message .= "Content-Transfer-Encoding: 7bit\n\n" ;
$message .= "HTML E-mail\n\nThis is the text portion of an HTML e-mail\n" ;
$message .= "--{$mime_boundary}\n" .
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n" ;
$message .= "Content-Transfer-Encoding: 7bit\n\n" ;
            
$message .= "<html><body bgcolor=red>
              <table width=200 bgcolor=red>
               <tr><td>This is the <b>HTML portion</b> of the mixed message.</td></tr>
             </table></body></html>";
Is there a successfull way to blend the functionality of both?

Posted: Sun Apr 10, 2005 12:28 am
by ol4pr0
Something that works for me ! i dont have trouble with images or whatever

Code: Select all

$message = &quote;&lt;html&gt;&lt;body&gt;Something to send&lt;br/&gt;&lt;img src=&quote;path/to/image.jpg&quote; alt=&quote;&quote; /&gt;&lt;br/&gt;Enjoy &lt;/body&gt;&lt;/html&gt;&quote;;
$array = explode(&quote;/&quote;, $string);
$fileatt      = &quote;FILE TO INCLUDE&quote;;
$fileatt_type = 'WHAT FILE TYPE ?';
$fileatt_name = $string = $array&#1111;sizeof($array)-1];
$headers = &quote;From:&quote;. $_POST&#1111;'from'];

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

$semi_rand = md5(time());
$mime_boundary = &quote;==Multipart_Boundary_x{$semi_rand}x&quote;;
		  
$headers .= &quote;\nMIME-Version: 1.0\n&quote; .
	&quote;Content-Type: multipart/mixed;\n&quote; .
	&quote; boundary=\&quote;{$mime_boundary}\&quote;&quote;;

$message .= &quote;This is a multi-part message in MIME format.\n\n&quote; .
	   &quote;--{$mime_boundary}\n&quote; .
	   &quote;Content-Type: text/html; charset=\&quote;iso-8859-1\&quote;\n&quote; .
	   &quote;Content-Transfer-Encoding: 7bit\n\n&quote; .
$message . &quote;\n\n&quote;;

$data = chunk_split(base64_encode($data));

$message .= &quote;--{$mime_boundary}\n&quote; .
	&quote;Content-Type: {$fileatt_type};\n&quote; .
	&quote; name=\&quote;{$fileatt_name}\&quote;\n&quote; .
	&quote;Content-Disposition: attachment;\n&quote; .
	&quote; filename=\&quote;{$fileatt_name}\&quote;\n&quote; .
	&quote;Content-Transfer-Encoding: base64\n\n&quote; .
	$data . &quote;\n\n&quote; .
	&quote;--{$mime_boundary}--\n&quote;;

Posted: Sun Apr 10, 2005 11:22 am
by clem_c_rock
Nice,This works great. Now have you ever been able to do all this as a multipart/alternative?

Thanks a lot