Please Help with Mail with File Attachment via PHP

Discussion in 'PHP' started by mmelen, Mar 17, 2010.

  1. #1
    Hey, I am trying to send email with an attached file via PHP script. I tried two methods (below) but neither one seems to work correctly. In both cases, the file contents are being included in the body and not as an attachment. Can anyone help me with this or see the problem? Thanks!

    Method 1:
    random_hash = md5(date('r', time()));
    
    $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'From: Backlinkbuild <support@backlinkbuild.com>' . "\r\n"; $headers .= 'Cc: Backlinkbuild <orders@backlinkbuild.com>' . "\r\n"; $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
    
    $attachment =
    chunk_split(base64_encode(file_get_contents($_FILES['File1']['tmp_name'])));
    
    
    $fileatt_name = $_FILES['File1']['name'];// Name of file
    
    $output = "
       –PHP-mixed-$random_hash;
       Content-Type: multipart/mixed; boundary='PHP-alt-$random_hash'
       –PHP-alt-$random_hash
       Content-Type: multipart/mixed; charset='iso-8859-1'
       Content-Transfer-Encoding: 7bit
    
       Hello,
       This is testing email with attachment. Please find attachment.
    
       –PHP-alt-$random_hash
       Content-Type: multipart/mixed; charset='iso-8859-1'
       Content-Transfer-Encoding: 7bit
    
       Hello,
       This is testing email with attachment. Please find attachment.
    
       –PHP-alt-$random_hash–
    
       –PHP-mixed-$random_hash
       Content-Type: application/vnd.ms-excel; name=$fileatt_name
       Content-Transfer-Encoding: base64
       Content-Disposition: attachment
    
       $attachment
       –PHP-mixed-$random_hash–";
    
    
    mail($order['Email'],$subj,$output,$headers);
    
    PHP:
    Method 2:
    $attachment =
    chunk_split(base64_encode(file_get_contents($_FILES['File1']['tmp_name'])));
    
    
    $fileatt = $_FILES['File1']['tmp_name']; // Location of file on server
    $fileatt_type = $_FILES['File1']['type'];// Type of file being sent
    $fileatt_name = $_FILES['File1']['name'];// Name of file
    $fileatt_path = $_FILES['File1']['tmp_name'];
    
       // Read the file to be attached ('rb' = read binary)
       $tmp_name1 = $_FILES['file1']['tmp_name'];
       $file = $_FILES['File1']['tmp_name'];
       $data = file_get_contents($_FILES['File1']['tmp_name']);
    
    
    
       // Generate a boundary string
       $semi_rand = md5(time());
       $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
       $headers .= 'From: Backlinkbuild <support@backlinkbuild.com>' . "\r\n";
       $headers .= 'Cc: Backlinkbuild <orders@backlinkbuild.com>' . "\r\n";
       // Add the headers for a file attachment
       $headers .= "\nMIME-Version: 1.0\n" .
       "Content-Type: multipart/mixed;\n" .
       "boundary=\"{$mime_boundary}\"";
       // Add a multipart boundary above the plain message
       $message = "This is a multi-part message in MIME format.\n\n" .
       "--{$mime_boundary}\n" .
       "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
       "Content-Transfer-Encoding: 7bit\n\n" .
       $message . "\n\n";
       $message .="This is an automated email sent from a php script\n";
    
       // Base64 encode the file data
       $data = chunk_split(base64_encode($data));
       // Add file attachment to the message
       $message .= "--{$mime_boundary}\n" .
       "Content-Type: {$fileatt_type};\n" .
       " name=\"{$fileatt_name}\"\n" .
       "Content-Disposition: attachment;\n" .
       " filename=\"{$fileatt_name}\"\n" .
       "Content-Transfer-Encoding: base64\n\n" .
       $data . "\n\n" .
       "--{$mime_boundary}--\n";  
    
    
    
    
    mail($order['Email'], $mail_subject, $message, $headers);
    
    PHP:
     
    Last edited: Mar 17, 2010
    mmelen, Mar 17, 2010 IP