Try using this function I've just knocked up (based heavily on the link posted before)...
function mail_attachments ($to, $subject, $body, $attachments, $headers = FALSE, $mailparams = FALSE) {
$mixedboundary = 'boundary-mixed-'.($random_hash = md5(time()));
if (!$headers) $headers = array(); else foreach ($headers as $header => $data) if (strtolower($header) == 'content-type') unset($headers[$header]);
$headers['Content-Type'] = "multipart/mixed; boundary=\"$mixedboundary\"";
$headersarr = array();
foreach ($headers as $header => $data) $headersarr[] = "$header: $data";
$headerstr = implode("\r\n",$headersarr);
$bodystr = "--$mixedboundary\r\n";
if (isset($body['html']) && isset($body['text'])) {
$bodystr .= "Content-Type: multipart/alternative; boundary=\"".($altboundary = "boundary-alt-$random_hash")."\"\r\n\r\n--$altboundary\r\nContent-Type: text/plain; charset=\"iso-8859-1\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n".$body['text']."\r\n\r\n--$altboundary\r\nContent-Type: text/plain; charset=\"iso-8859-1\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n".$body['html']."\r\n\r\n--$altboundary--\r\n\r\n--$mixedboundary";
} else if (($html = isset($body['html'])) || isset($body['text'])) {
$bodystr .= "Content-Type: text/".(($html) ? 'html' : 'plain')."; charset=\"iso-8859-1\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n".(($html) ? $body['html'] : $body['text'])."\r\n\r\n--$mixedboundary";
} else return FALSE;
foreach ($attachments as $attachment => $type) if (is_file($attachment)) $bodystr .= "\r\nContent-Type: $type; name=\"".basename($attachment)."\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment\r\n\r\n".chunk_split(base64_encode(file_get_contents($attachment)))."\r\n--$mixedboundary";
$bodystr .= "--"; print($bodystr."\r\n\r\nHeaders:\r\n".$headerstr);
return ($mailparams) ? @mail($to,$subject,$bodystr,$headerstr,$mailparams) : @mail($to,$subject,$bodystr,$headerstr);
}
I haven't tested it but I reckon it should work.
$to is a string, just like mail()
$subject is a string, just like mail()
$body is an associative array. At least one of $body['text'] and $body['html'] MUST be set (for you, I think you will just put the body of your message in $body['html']). You can set both, but not niether.
$attachments is an associative array, in the form
array ( '/path/to/attachment'=>'mimetype/of-attachment' )
Set as many attachments as you like, but they MUST be local files (so you can just dump the file have created to a temp file, and pass the path to that temp file).
$headers is an associative array, in the form
array( 'header' => 'Value of header' )
Set as many headers as you like.
$mailparams is a string, just like the last parameter of mail()
Returns a Boolean, like mail()
Let me know how you get on 🙂
PS apologies for the lack of comments in that code, I will pretty it up when I know it works...