I've been following a few tutorials to create my own mail function. I have two needs : send txt+hml formated mails and attach a pdf (pdf newsletter generated from a products catalogue db). My problem resides in sending the attached pdf. It seems to be sent correctly, but the pdf file is corrupt. When I compare the mail source with a test mail sent with Outlook I notice several minor differences in the resulting base64 encoding of the pdf and "Content-Disposition: attachment" comes out as "Content-Disposition: attahment" wich is plain weird. However if I echo $data after processing but before sending, it looks fine. I'm using a Win2k box with apache and php 4.3.3. And here is my function :
function mail_html($destinataire, $sujet , $messtxt, $messhtml , $from, $fileName = '0', $data = '0')
{
$messtxt = stripslashes($messtxt);
$messhtml = stripslashes($messhtml);
$limite = md5(uniqid(rand()));
$from_name = "$from";
$from_email = "$from";
$to_name = "$destinataire";
$to_email = "$destinataire";
$subject = "$sujet";
$headers = "From: $from_name<$from_email>\n";
$headers .= "Reply-To: <$from_email>\n";
$headers .= "MIME-Version: 1.0\n";
// check for attachement or just txt+html
if ($fileName == '0') {
$headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=".$limite."_main_message\"\n";
} else {
$headers .= "Content-Type: multipart/mixed;\n";
$headers .= " boundary=\"----=".$limite."_main_message\"\n";
}
//
$headers .= "X-Sender: $from_name<$from_email>\n";
$headers .= "X-Mailer: PHP4\n"; //mailer
$headers .= "X-Priority: 3\n"; //1 UrgentMessage, 3 Normal
$headers .= "Return-Path: <$from_email>\n";
$headers .= "This is a multi-part message in MIME format.\n\n";
$headers .= "------=".$limite."_main_message \n";
// txt section
$headers .= "Content-Type: multipart/alternative;\n";
$headers .= " boundary=\"----=".$limite."_message_parts\"\n";
$texte_simple = $sujet."\n\n";
$texte_simple .= $messtxt;
$message = "------=".$limite."_message_parts\n";
$message .= "Content-Type: text/plain;\n charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
// txt message here
$message .= "$texte_simple\n";
$message .= "\n";
// html section
$message .= "------=".$limite."_message_parts\n";
$message .= "Content-Type: text/html;\n charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: quoted-printable\n";
$message .= "\n";
// html message here
$message .= "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
$message .= "<html><HEAD>\n";
$message .= "<style>\n";
$message .= "<!--\n";
$message .= "td {\n";
$message .= " FONT-SIZE: 12px; MARGIN: 4px; COLOR: #000000; LINE-HEIGHT: 16px; FONT-FAMILY: Arial,Helvetica,Geneva; TEXT-ALIGN: left\n";
$message .= "}\n";
$message .= "//-->\n";
$message .= "</style>\n";
$message .= "</HEAD<BODY leftmargin=3D0 topmargin=3D0 marginwidth=3D0 marginheight=3D0>\n";
$message .= '<table width=3D690 border=3D0 cellspacing=3D2 cellpadding=3D2>'."\n";
$message .= '<tr>'."\n";
$message .= '<td>'.$sujet.'</td>'."\n";
$message .= '</tr>'."\n";
$message .= '<tr>'."\n";
$message .= '<td>'.$messhtml.'</td>'."\n";
$message .= '</tr>'."\n";
$message .= '</table>'."\n";
$message .= ''."\n";
$message .= "</BODY></HTML>\n";
$message .= "\n";
$message .= "------=".$limite."_message_parts--\n";
$message .= "\n";
//check for existence of attachment
if (($fileName != '0')&&($data != '0')){
$message .= "------=".$limite."_main_message\n";
$message .= "Content-Type: application/pdf;\n name=\"".$fileName."\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= 'Content-Disposition: attachment;'."\n".' filename="'.$fileName.'"'."\n\n";
$message .= $data."\n";
}
//message end
$message .= "------=".$limite."_main_message--\n";
return mail($to_email, $subject, $message, $headers);
}
$filename= 'file.pdf';
$fh=fopen($filename,"rb");
$data=fread($fh,filesize($filename));
$data = chunk_split(base64_encode($data));
mail_html('test@domain.com', 'Newsletter' , 'txt : See attached file', 'html : See attached file' , 'admin@globalcar.be', $filename, $data ) ;
Any pointers would help