help debugging MIME encoded email....



I'm trying to teach myself how to MIME encode HTML emails with attachments, and I was hoping some of you wouldn't mind helping me out....

Here is the code I've written so far:


/* attachments */ 
$attach[0] = "Newsletter/0105e2.gif"; 
$attach[1] = "Newsletter/0105e3.gif"; 
$attach[2] = "Newsletter/0105e4.jpg"; 
$attach[3] = "Newsletter/ACF2D3.jpg"; 
$attach[4] = "Newsletter/logo3.jpg"; 
$attach[5] = "Newsletter/logo4.jpg"; 
$attach[6] = "Newsletter/mainarrow_white.gif"; 
$attach[7] = "Newsletter/spacer.gif"; 

for($i=0; $i <= 7; $i++) 
    $fp = fopen($attach[$i],'rb'); 
    $attachment[$i] = fread($fp,filesize($attach[$i])); 
    //$attachment[$i] = chunk_split(base64_encode($attachment[$i])); 

/* subject */ 
$subject = "Media Moguls Newsletter"; 

// Generate a boundary string 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

/* headers */ 
$headers = "From: Admin<>\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: multipart/mixed; boundary=\"{$mime_boundary}\"\r\n"; 

// Add a multipart boundary above the plain message 
$body = "This is a multi-part message in MIME format.\r\n"; 
$body .= "--{$mime_boundary}\r\n"; 
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"; 
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 

/* HTML message */ 
$body .= ' 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""> 


.....blah, blah, blah, insert HTML here.... 


<span align="center" valign="top"><a href="" target="blank"><img src="'; 

// Add inline file attachment to the message 
$body .= "--{$mime_boundary}\r\n"; 
$body .= "Content-Type: $attach[5]_type;\r\n" . 
  " name=\"$attach[5]_name\"\r\n"; 
$body .= "Content-Transfer-Encoding: base64\r\n"; 
$body .= "Content-Disposition: inline;\r\n" . 
  " filename=\"$attach[5]_name\"\r\n"; 

$body .= "$attachment[5]"; 

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

$body .= ' 
" border="0"></span> 

...blah, blah, blah, more HTML.... 


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

/* and now mail it */ 
mail($mail_to, $subject, $body, $headers); 


The first couple of HTML lines look OK (in a test email), and then when I get to the first attachment, things get fouled up...instead of seeing the attachment, I see a bunch of code.....

If someone could tell me what I'm doing wrong, I'd appreciate it...

Attachments in e-mails have to be encoded in base64 (ergo "Content-Transfer-Encoding: base64"), which you can do using base64_encode().

Of course, you'd be a lot better off using a nice flexible PHP mail package such as the highly-recommended PHPMailer.
Thanks...I decided to look into this PHPMailer thing.....

Before I get too involved with it, can you tell me if there is a way to configure "inline" email attachments....I just want to be sure before I get started....I need "inline" as opposed to regular attachments....

Thanks again.... :)
IIRC "inline" attachments are done with HTML. But that's all I know about it. Google it.
if you check the header of an inline attachment...
it is
Content-Disposition: inline; filename=""