As you recall, an email is composed of three basic parts:
An attachment in an email is nothing more than appending file data to the email message (content). The only problem is we need some way to tell the email client where the end of the text portion of our message is and where the beginning of our file data starts. Basically, we need some way to seperate the content segments.
First we need to let the client know that the message may contain multiple Mime-types. We do this by adding more informatio to the headers:
$headers .= "Content-Type: multipart/mixed;\r\n";
Next, we add a special header to our email message called a boundry. This must be a string that will not be repeated elsewhere in the message because the email client will use this "boundry" to "split" the email much the same way "split()" uses a delimiter to seperate a string in to a list of variables.
//Create a unique string. //Here I'm calling the time function to get the current time, then encrypting it. //This will generate a fairly random string //I then add "<<<--==+X[" and "]" just to make sure $mime_boundary = "<<<--==+X[".md5(time())."]"; //Set my boundary identifier in the header portion of the email $headers .= " boundary=\"".$mime_boundary."\"";
Recall from the previous section, that to send an email we construct our message and headers. Now we will add the mime boundary to this like below...
//Create a unique string. //Here I'm calling the time function to get the current time, then encrypting it. //This will generate a fairly random string //I then add "<<<--==+X[" and "]" just to make sure $mime_boundary = "<<<--==+X[".md5(time())."]"; //Set to and subject vars $to = "phpclass@aphion.com"; $subject = "This is a test"; //Declare mime version $headers .= "MIME-Version: 1.0\r\n"; //Let the client know to expect multiple mime-types $headers .= "Content-Type: multipart/mixed;\r\n"; //Let the client know what the boundary is. $headers .= " boundary=\"".$mime_boundary."\"";
Now we start composing the message content. For good measure, we should also let the client know that this message contains an attachement. We do this by adding information to the email message:
$message .= "This is a multi-part message in MIME format.\r\n"; $message .= "\r\n";
Next we begin with identifying a boundary. To do this, we need to prepend our boundary variable with a "--" like so:
$message .= "--".$mime_boundary."\r\n";
Now for each part of the email we need to specify the content type. So we add that information here.
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"; $message .= "Content-Transfer-Encoding: 7bit\r\n"; $message .= "\r\n";
And add our content
$message .= "Email content and what not: \r\n"; $message .= "This is the file you asked for! \r\n";
Then end with another boundary
$message .= "--".$mime_boundary."\r\n";
Now we add an attachement. We again need to declare the content type
$message .= "Content-Type: application/octet-stream;\r\n"; $message .= " name=\"filename.extn\"\r\n"; $message .= "Content-Transfer-Encoding: quoted-printable\r\n"; $message .= "Content-Disposition: attachment;\r\n"; $message .= " filename=\"filename.extn\"\r\n"; $message .= "\r\n"; //give an extra line
Now we read in our file content
$contents=""; $fh = fopen("/home/username/file.ext","r"); while(!feof($fh)) { $contents .= fread($fh,1024); } fclose($fh);
And write it to our email
$message .= $contents; $message .= "\r\n";
Finally we close our boundary
$message .= "--".$mime_boundary."\r\n";
Then we can email our message and attachment
mail($to, $subject, $message, $headers);
Here's a link to some code you can use to attach a file to an email. Source code (.txt file)