[nycphp-talk] Multi part related emails
DeWitt, Michael
mjdewitt at alexcommgrp.com
Fri Aug 1 13:39:34 EDT 2003
I had this same need and found this bit of code on the web which I modified
a bit to suit my need. I can't remember where I got it, and I would like to
give credit back to the original author, but they didn't put anything into
the comments.
The function should send any kind of file as part of a multi-part email. I
used it for a gif. In testing it, it seemed to work fine. In terms of
compatibility, I did find that one webmail client (IMP) didn't handle the
attachment correctly, but forwarding it to another mail client from IMP was
fine. Outlook 97 handles it fine.
Hope this helps.
Mike
Here it is.
<?php
// Read POST request params into global vars
function mail_attachment
($to,$from,$subject,$message,$file_location,$filename) {
/*
if you want to play with the function, just uncomment what you want from
this section and shove some values in.
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to="someone at somewhere.com";
$subject="test attachment";
$message="this is just a test.";
$file_location="/www/yourserver/somewhere/";
$filename="yourfile.gif";
*/
//File uploads in PHP 4.1 are placed in a special $_FILES array, so we fetch
the values we need out of it:
// Obtain file upload vars
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
//For the sake of brevity, we'll assume that the required parameters ($to
and $from) now have valid values (email addresses) in them. Normally we
would check their format with regular expressions.
//Next, we use the $from value to begin building the extra headers for the
email:
$headers = "From: $from";
//Next we check the $fileatt variable, which may or may not contain the path
and filename to an uploaded file attachment. We use PHP's is_uploaded_file
function to find out:
if (file_exists($file_location.$filename)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($file_location.$filename,'rb');
$data = fread($file,filesize($file_location.$filename));
fclose($file);
//Having read in the data for the file attachment, we need to set up the
message headers to send a multipart/mixed message:
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
//Now for the message body itself. This works just as we saw for the text
part of a mixed message in the previous section:
// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
//Now, to allow for binary file types, we need to use Base64 encoding to
convert the (possibly binary) file attachment data to a text-only format
suitable for sending by email. All email programs in popular use support
Base64 encoding of file attachments, so this is the best way to go.
Fortunately, PHP provides a function for Base64 encoding:
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
//We now have everything we need to write the portion of the message that
contains the file attachment. Here's the code:
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$filename}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$filename}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}
//That completes the modifications necessary to accommodate a file
attachment. We can now send the message with a quick call to mail:
// Send the message
$ok = @mail($to, $subject, $message, $headers);
/*if ($ok) {
echo "<p>Mail sent! Yay PHP!</p>";
} else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
*/
return $ok;
} //end of function
?>
> Hello all!
>
> I'm wondering if anyone has experience with multipart-related html emails.
>
> I'd like to send out an html email with embedded images, that do NOT
> reference
> an external img src, but rather another "part" of the email.
>
> For eaxample, the first part is the html code, which references an image
> thusly (it's quoted-printable):
> *************************************************
>
> *************************************************
>
> as a second part, the image is encoded:
> *************************************************
> ------=_NextPart_000_0000_01C35826.C5C0BC10
> Content-Type: image/gif
> Content-Transfer-Encoding: base64
> Content-Location: file:email2.gif
>
> R0lGODlh8wF3AdUAAP///wAAAP8B/wAA/wD/AP8AAAH///7/AKWlpUNDQzHDw39/f/+5uf96eq
> am
> <snip>
> *************************************************
>
> My questions are:
> 1. Does anyone know how well supported across email clients
> "multipart-related" emails are?
>
> 2. Does anyone know of a freely available script for authoring these?
>
> Thanks all!
>
> --
> WF
>
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk
More information about the talk
mailing list