none
MIME header with attachment

    Question

  • We have Exchange Server 2003 running and I am trying to send  an e-mail with an attachment through SMTP. I believe I am having trouble declaring the MIME headers correctly, as the attachment arrives corrupt. I notice that the recipient receives the e-mail with another message from the SPAM filter server. My code is in PHP but it irrelevant why am I am using this specific code. I just want to be sure that I am declaring the headers correctly. I have tested through gmail and the attachment arrives without problems.

     

     

    				
    
    $newLine = "\r\n";
    $attachment="myFile.zip";
    $message = "<br><h1><center>TEST MESSAGE</center></h1>" ; 
    
    <span style="white-space:pre">				</span>$headers .= "Content-Type: multipart/alternate;" . $newLine;
                                   $headers .= "     boundary=\"----=_fm_next_part_000000_pA4G8lah000901\"" . $newLine . $newLine;
    
                                   $headers .= "\"------=_fm_next_part_000000_pA4G8lah000901\"" . $newLine;
                                   $headers .= "Content-Type: multipart/alternative;" . $newLine . $newLine;
    
                                   //Attachment
    
                                   $headers .= "------=_fm_next_part_000000_pA4G8lah000901" . $newLine;
    
                                   $headers .= "Content-Transfer-Encoding: base64" . $newLine;
    
                                   $headers .= "Content-Type: text/plain;" . $newLine;
    
                                   $headers .= "Content-Disposition: attachment;" . $newLine;
    
                                   $headers .= "    filename=" . $attachment . $newLine . $newLine;
    
                                  
    
                                   $handle = fopen($attachment, "rb");
    
                                   $contents = '';
    
                                   while (!feof($handle)) {
    
                                                   $contents .= fread($handle, filesize($attachment));
    
                                   }
    
                                   fclose($handle);
    
                                  
    
                                   $contents = base64_encode($contents);
    
                                   $headers .= $contents . $newLine;
    
                                  
    
                                  
    
                                   //Body
    
                                   $headers .= "------=_fm_next_part_000000_pA4G8lah000901" . $newLine;
    
                                   $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
    
                                   $headers .= "Content-Transfer-Encoding: quoted-printable" . $newLine;
    
                                   $headers .= "     boundary=\"_7fdd1316-6f68-41bb-93f7-134933fc9aad_\"" . $newLine . $newLine;
    
                                  
    
                                   $headers .=  $message . $newLine;                      
    
                                  
    
                                   // End of message
    
                                   $headers .= "------=_fm_next_part_000000_pA4G8lah000901--" . $newLine;
    
    

                                  

     

    When I look at the headers of the e-mail sent to the recipient it looks this way:


     

    Microsoft Mail Internet Headers Version 2.0
    
    Received: from mx.domain.com ([200.5.3.7]) by asem-07.domain.com with Microsoft SMTPSVC(6.0.3790.4675);
    
                     Fri, 4 Nov 2011 14:31:36 -0400
    
    Received: from 127.0.0.1 (ns1.domain.com [])
    
                    by mx.domain.com  with SMTP id pA4IoT7T014670-pA4IoT7U014670
    
                    for <e-mail@domain.com>; Fri, 4 Nov 2011 14:50:29 -0400
    
    Date: Fri, 4 Nov 2011 14:50:29 -0400
    
    Message-Id: <201111041850.pA4IoT7T014670-pA4IoT7U014670@mx.domain.com>
    
    To: e-mail@domain.com
    
    From: e-mail@domain.com
    
    Subject: Email test
    
    MIME-Version: 1.0
    
    Content-Type: multipart/mixed; boundary="----=_fm_next_part_000000_pA4IoWvf000901"
    
    Content-Transfer-Encoding: 7bit
    
    Content-Disposition: inline
    
    Return-Path: e-mail@domain.com
    
    X-OriginalArrivalTime: 04 Nov 2011 18:31:36.0078 (UTC) FILETIME=[FBFBDAE0:01CC9B1F]
    
     
    
    ------=_fm_next_part_000000_pA4IoWvf000901
    
    Content-Type: multipart/alternate;
    
         boundary="----=_fm_next_part_000000_pA4G8lah000901"
    
     
    
    ------=_fm_next_part_000000_pA4G8lah000901
    
    Content-Transfer-Encoding: base64
    
    Content-Type: text/plain;
    
    Content-Disposition: attachment;
    
        filename=myFile.zip
    
     
    
    ------=_fm_next_part_000000_pA4G8lah000901
    
    Content-type: text/html; charset=iso-8859-1
    
    Content-Transfer-Encoding: quoted-printable
    
         boundary="_7fdd1316-6f68-41bb-93f7-134933fc9aad_"
    
     
    
     
    
    ------=_fm_next_part_000000_pA4G8lah000901--
    
    ------=_fm_next_part_000000_pA4IoWvf000901
    
    Content-Type: text/plain; charset="utf-8"
    
    Content-Transfer-Encoding: base64
    
    Content-Disposition: inline
    
     
    
     
    
    ------=_fm_next_part_000000_pA4IoWvf000901--

    Friday, November 04, 2011 6:43 PM

Answers