| ./ ../ navigation/ email attachments email viewer file uploads |
To send email attachments in PHP you've got to mess with the header, and do a few bits and bobs. I'm not going to explain the basics, there's plenty coverage of that elsewhere.
The first thing you need to do, is specify what will seperate the data in your email. This needs to be something quite unique, so I use some nice looking garbage with an md5 timestamp int the middle. It will do just fine.
$mime_boundary = "<<<--==+X[".md5(time())."]";
Sort out your basic headers as usual, by piling them into a $headers string.
$headers .= "From: Automatic <an.e.mail@domain.net>\r\n";
$headers .= "To: SomeName <an.e.mail@domain.net>\r\n";
Then you've got to specify the type of content you're dealing with. This is also put into the $headers string. In this case, it will be mixed between text, and some kind of attachment, hence the Content-Type bit. The boundary bit uses the boundary variable you made earlier:
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$mime_boundary."\"";
Next, get on with piling the message into a $message string. You need quite a bit of header looking stuff in here. The first bit is what is displayed if the client receiveing the email doesn't support MIME, followed by the boundary string (preceded by "--", not sure why).
$message .= "This is a multi-part message in MIME format.\r\n";
$message .= "\r\n";
$message .= "--".$mime_boundary."\r\n";
Next comes the actual content of the email, each part needs it's own mini header describing what it is.
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "\r\n";
$message .= "Email content and what not: \r\n";
$message .= "This is the file you asked for! \r\n";
$message .= "--".$mime_boundary."\r\n";
Finally, a plaintext attachment. In this case I already put your contents into $fileContent.
$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";
$message .= $fileContent;
$message .= "\r\n";
$message .= "--".$mime_boundary."\r\n";
Then send the thing, using the mail function as described in the manual. You will probably have to have a few goes at this before you get it right. The linbreaks need to be exactly right for this to work, and if it doesn't, that's probably why.
$ok = mail("an.e.mail@domain.net", "file by email", $message, $headers);