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

    As stated, i'm using a win2k box (no uuencode apparently). On a Linux box the above code runs fine (the pdf isn't corrupted).

      Oops I did miss that. Looks like my server isen't the only one that needs a memory upgrade.

      However you could just install uuencode on windows

      http://208.227.179.202/uu.html

      Winzip 6.2 and above also supports uuencoding. I imagine this could be run from the command line but am unsure as to how.

        7 days later

        I ended up using phpmailer class from sourceforge, and guess what... exact same problem when using php mail() function on win2k. As soon as you pass trough smtp or use it on a Linux box, no problem. So my conclusion is win2k apache/php base64_encode + mail() = bad combo

          I've just experienced exactly the same problem. It's not actually the base64_encode that's the trouble, it's the mail() function in the windows distribution of PHP. It is bugged and is documented as such - which you of course only find out wayyy after you've analyed the cack out of your code.

          I was ripping my hair out, and even emailed an attachment to myself and pasted the already created headers in and the already encoded gif. If you analyse the email when it comes back you'll notice that mail() sometimes eats characters from the rows.

          After using fsocket with my SMTP server everything went fine.

          You can also use your own SMTP emailer to use authenticated servers. Send with fputs

          AUTH LOGIN\r\n

          followed by your user name base64 encoded."\r\n", then your password base encode."\r\n".

            Write a Reply...