Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

help debugging MIME encoded email....

the mail() function

Moderators: macek, egami, gesf

help debugging MIME encoded email....

Postby chump2877 » Fri Feb 04, 2005 4:17 pm

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:

Code: Select all
<?

/* 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]));
    fclose($fp);
}


/* 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<rjaffe@mediamogulsweb.com>\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" "http://www.w3.org/TR/html4/loose.dtd">

<HTML>

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

<!-- left column -->

<br>
<span align="center" valign="top"><a href="http://www.mediamogulsweb.com" 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>
</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...

thanks.
__________________
R.J.
http://www.mediamogulsweb.com
chump2877
New php-forum User
New php-forum User
 
Posts: 44
Joined: Mon Nov 08, 2004 10:42 am

Postby swirlee » Fri Feb 04, 2005 4:28 pm

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.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby chump2877 » Fri Feb 04, 2005 11:42 pm

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.... :)
chump2877
New php-forum User
New php-forum User
 
Posts: 44
Joined: Mon Nov 08, 2004 10:42 am

Postby swirlee » Sat Feb 05, 2005 3:24 pm

IIRC "inline" attachments are done with HTML. But that's all I know about it. Google it.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby ruturajv » Sun Feb 06, 2005 7:25 pm

if you check the header of an inline attachment...
it is
Code: Select all
Content-Disposition: inline; filename="best_sceneries.zip"
User avatar
ruturajv
php-forum Super User
php-forum Super User
 
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India


Return to PHP coding => Mail

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.