Sending mail with attachment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • berei
    New Member
    • Nov 2012
    • 3

    Sending mail with attachment

    Can any one explaine why this code can't send zip-files, but work ok for xml and xls files?

    The function call return OK, but I don't receive any mail.
    Code:
    <?php
    function mail_attachment($filename, $userfile, $mail_to, $from_mail, $from_name, $replyto, $mail_subject, $message)
    {
      $fp = fopen($userfile, "rb");
      $file = fread($fp, filesize($userfile));
      fclose($fp);
      $file = chunk_split(base64_encode($file));
    
      $mail_boundary = 	md5(uniqid(time()));
      $mail_boundary = "==Multipart_Boundary_x{$mail_boundary}x";
      
      $mail_headers  = "From: \"".$from_name."\" <".$from_mail.">\r\n";
      $mail_headers .= "Reply-To: <".$from_mail.">\r\n";
      $mail_headers .= "MIME-Version: 1.0\r\n";
      $mail_headers .= "Content-type: multipart/mixed;boundary=\"{$mail_boundary}\"\r\n";
      $mail_headers .= "\r\n";
    
      $mail_body  = "--{$mail_boundary}\r\n";
      $mail_body .= "Content-type: text/plain; charset=euc-kr\r\n";
      $mail_body .= "Content-transfer-encoding: 8bit\r\n";
      $mail_body .= "\r\n";
      $mail_body .= "$message";
      $mail_body .= "\r\n";
      $mail_body .= "--{$mail_boundary}\r\n";
    
      $mail_body .= "Content-type: application/octet-stream; name=$filename\r\n";  
      $mail_body .= "Content-transfer-encoding:base64\r\n";
      $mail_body .= "\r\n";
      $mail_body .= $file;
    
      if (mail($mail_to, $mail_subject, $mail_body, $mail_headers))
      {
        echo "OK";
      }
      else
      {
        echo "ERROR";
      }  
    }
    ?>
    Last edited by Niheel; Mar 15 '13, 08:19 AM.
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    i copied your code, and its sending a zip-file without problems....

    i think you need to check the logfiles of your mailserver?

    it could fail because of the size of the attachment

    Comment

    • berei
      New Member
      • Nov 2012
      • 3

      #3
      Specials characters in filename

      Originally posted by Luuk
      i copied your code, and its sending a zip-file without problems....

      i think you need to check the logfiles of your mailserver?

      it could fail because of the size of the attachment
      It don't work when the filename contains specials characters i.t. danish æøå.
      Hot do I overcome this limitation?

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        You could try to encode the filename,
        just as they did with the subject here:

        Comment

        Working...