Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending multipart/alternative emails with attachments

Status
Not open for further replies.

clemrock

Programmer
May 20, 2004
118
US
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:

$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:

$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?
 
yeah - everyone knows about this one. I am trying to learn something by writing my own code and i'm customizing my emails to handle multipart/alternative w/ attachments. This class is great but, I don't see it offering this. I'm also trying to customize it to send large group emails and I don't see this class being condusive for accomplishing that. Also, trying to figure out that class would take me more time than trying to learn something.
 
If you really want to learn it yourself, start reading RFC2822

However, the phpMailer class offers
sourceforge said:
Multiple fs, string, and binary attachments (those from database, string, etc)
Reading through the class and understanding it would IMO the best way to understand the complex issues posed by Internet Mail with attachments. It will also expose you to classes and objects which would benefit your PHP knowledge tremendously.
But, if you want to do it yourself, start reading!
Cheers.
 
does anyone know if the PHPMail.class works fine with .xls attachments?
 
Why wouldn't it? If it is properly attached you'd be able to send your grandmas digital kitchen sink.

P.S. You might want to start a new thread next time. Posting new questions at the end of an older thread makes it hard for others to find it in case they have the same question.
 
clemrock,

If you want HTML + attachments, you should use multipart/mixed instead.
 
I realize there's easy ways to do this and I can easily send an html email w/ attachments using the multipart/mixed.

The scenario I am trying to achieve, is to

1. Send an Html email w/ attachments.
2. If the user's mail program can't read html, it will
show just the plain text w/out attempting to render the
html.

I can't wimp out and settle for less. I'm too obsessive.


Thanks for your help.

Clem C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top