SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict mmelen's Avatar
    Join Date
    Jul 2005
    Location
    NJ/NY USA
    Posts
    243
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mail with File Attachment via PHP

    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:
    PHP Code:
    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); 
    Method 2:
    PHP Code:
    $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); 

  2. #2
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the first code, I have no idea what you are doing. What is -php-alt?

    The second code is closer, but having single space characters at the start of header lines will break it.
    e.g.
    PHP Code:
    " boundary=\"{$mime_boundary}\""
    should be
    PHP Code:
    "boundary=\"{$mime_boundary}\""
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  3. #3
    SitePoint Addict mmelen's Avatar
    Join Date
    Jul 2005
    Location
    NJ/NY USA
    Posts
    243
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tip but still doesn't work... probably fix one of the problems though. Any other suggestions?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    How about taking advantage of others who have already solved this problem, by using something like PHP Mailer.
    http://phpmailer.worxware.com/
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Evangelist
    Join Date
    Jul 2002
    Location
    Italy
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you solved your problem yet?

    I have a similar prob. I've not had time to look at your entire script but have you tried
    PHP Code:
    "Content-Type: multipart/mixed; boundary=\"{$mime_boundary}\""
    ie all on one line on option 2?
    blessed are the cracked for they let in the light


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •