Results 1 to 15 of 15
  1. #1
    Join Date
    Feb 2002
    Location
    New York
    Posts
    791

    Need help with php email script with attachment

    I am hoping someone can help me fix the issue I am having with a php script that sends a email with a attachement. The script does generate a email that goes out. However when I recieve it the email has a attachment correctly named but when I open it, for example it is a PDF it sayd is is not a supported file type or because the file has been damaged ???

    Below is my PHP code

    Code:
     //Send email with attachment
    $fileatt = "$fileName"; // Path to the file                  
    $fileatt_type = "application/pdf"; // File Type  
    $fileatt_name = "$fileName"; // Filename that will be used for the file as the attachment  
    $email_from = "[email protected]"; // Who the email is from  
    $email_subject = "File Uploaded To my.jjfl.com"; // The Subject of the email  
    $email_message = "Thanks for uploading test.<br>";
    $email_message .= "Thanks for visiting.<br>"; // Message that the email has in it  
    $email_to = "[email protected]";  
    $headers = "From: ".$email_from;  
    $file = fopen($fileatt,'rb');  
    $data = fread($file,filesize($fileatt));  
    fclose($file);  
    $semi_rand = md5(time());  
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  
        
    $headers .= "\nMIME-Version: 1.0\n" .  
                "Content-Type: multipart/mixed;\n" .  
                " boundary=\"{$mime_boundary}\"";  
    $email_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" .  
    $email_message .= "\n\n";  
    $data = chunk_split(base64_encode($data));  
    $email_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($email_to, $email_subject, $email_message, $headers);
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    just a programmer

  2. #2
    Try Zip, Sometimes there is a problem in transferring attachments

  3. #3
    Join Date
    Aug 2001
    Posts
    5,068
    Unless you have to use it, I would recommend against using the basic mail() function for attachments.
    Sitemeer.com - Is your site up?
    Multi-Location Service Availability Check ● yes, we do HTTPS & IDN!


  4. #4
    Join Date
    Jul 2012
    Posts
    66
    Try mixing this in;

    Edit: Also, I'd recommend using PHPMailer or SwiftMailer.

    Code:
    <?php
        $size = filesize($fileName);
        $file = fopen($fileName, "r");
        $attachment = fread($file, $size);
        fclose($file);
        $attachment = chunk_split(base64_encode($attachment));
    
    	
        $header .= "Content-Type: application/octet-stream; name=\"".$fileName."\"\r\n";
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: attachment; filename=\"".$fileName."\"\r\n\r\n";
        $header .= $attachment."\r\n\r\n";
    
    ?>

  5. #5
    Join Date
    Feb 2002
    Location
    New York
    Posts
    791

    ...

    Thanks icode but your code did not work
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    just a programmer

  6. #6
    Join Date
    Jul 2012
    Posts
    66
    Show me how you've integrated it

  7. #7
    Join Date
    Feb 2002
    Location
    New York
    Posts
    791
    //Send email with attachment
    $size = filesize($fileName);
    $file = fopen($fileName, "r");
    $attachment = fread($file, $size);
    fclose($file);
    $attachment = chunk_split(base64_encode($attachment));

    $header .= "Content-Type: application/octet-stream; name=\"".$fileName."\"\r\n";
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$fileName."\"\r\n\r\n";
    $header .= $attachment."\r\n\r\n";

    $fileatt = "$fileName"; // Path to the file
    $fileatt_type = "application/pdf"; // File Type
    $fileatt_name = "$fileName"; // Filename that will be used for the file as the attachment
    $email_from = "info@***.com"; // Who the email is from
    $email_subject = "File Uploaded To ***.com"; // The Subject of the email
    $email_message = "Thanks for uploading test.<br>";
    $email_message .= "Thanks for visiting.<br>"; // Message that the email has in it
    $email_to = "***@***.com";
    $header .= "From: ".$email_from;
    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";


    $email_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" .
    $email_message .= "\n\n";
    $data = chunk_split(base64_encode($data));
    $email_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($email_to, $email_subject, $email_message, $header);

    //end send email with attachment
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    just a programmer

  8. #8
    Join Date
    Jul 2012
    Posts
    66
    Yeah, that's not right at all.
    You've not added it in properly. I'll do it soon - I'm not really available atm.

  9. #9
    Join Date
    Feb 2002
    Location
    New York
    Posts
    791

    ...

    OK thanks you
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    just a programmer

  10. #10
    As was previously mentioned, you may just want to use SwiftMailer (www.swiftmailer.org), which is essentially a wrapper for sending emails. It allows you to set headers independently of the message, add attachments and such. It is definitely going to be a LOT cleaner than the code you have already:

    PHP Code:
    // Create the message
    $message Swift_Message::newInstance()

      
    // Give the message a subject
      
    ->setSubject('Your subject')

      
    // Set the From address with an associative array
      
    ->setFrom(array([email protected]=> 'John Doe'))

      
    // Set the To addresses with an associative array
      
    ->setTo(array([email protected]'[email protected]=> 'A name'))

      
    // Give it a body
      
    ->setBody('Here is the message itself')

      
    // And optionally an alternative body
      
    ->addPart('<q>Here is the message itself</q>''text/html')

      
    // Optionally add any attachments
      
    ->attach(Swift_Attachment::fromPath('my-document.pdf'))
      ;

      
    // Create the Transport
    $transport Swift_SmtpTransport::newInstance('smtp.example.org'25)
      ->
    setUsername('your username')
      ->
    setPassword('your password')
      ;

    // Create the Mailer using your created Transport
    $mailer Swift_Mailer::newInstance($transport);

    // Send the message
    $result $mailer->send($message); 
    Of course, there is a lot more you can do with it, but this should suffice for your example. If you want specific headers, or to tweak the file attachment, you can definitely do that as well, and there are examples in the documentation.
    Managed Service Provider - www.OpticIP.com
    Public & Private Cloud
    Solutions | SSD SANs | High IOP's | CDN Solutions
    Phoenix/Chandler AZ Colocation | 48U Cabinets | Data Halls | TIA-942 Tier 4 Facility

  11. #11
    Join Date
    Jul 2012
    Posts
    66
    Quote Originally Posted by TimOpticIP View Post
    As was previously mentioned, you may just want to use SwiftMailer (www.swiftmailer.org), which is essentially a wrapper for sending emails. It allows you to set headers independently of the message, add attachments and such. It is definitely going to be a LOT cleaner than the code you have already:

    PHP Code:
    // Create the message
    $message Swift_Message::newInstance()

      
    // Give the message a subject
      
    ->setSubject('Your subject')

      
    // Set the From address with an associative array
      
    ->setFrom(array([email protected]=> 'John Doe'))

      
    // Set the To addresses with an associative array
      
    ->setTo(array([email protected]'[email protected]=> 'A name'))

      
    // Give it a body
      
    ->setBody('Here is the message itself')

      
    // And optionally an alternative body
      
    ->addPart('<q>Here is the message itself</q>''text/html')

      
    // Optionally add any attachments
      
    ->attach(Swift_Attachment::fromPath('my-document.pdf'))
      ;

      
    // Create the Transport
    $transport Swift_SmtpTransport::newInstance('smtp.example.org'25)
      ->
    setUsername('your username')
      ->
    setPassword('your password')
      ;

    // Create the Mailer using your created Transport
    $mailer Swift_Mailer::newInstance($transport);

    // Send the message
    $result $mailer->send($message); 

    I don't think they will be able to do this.

  12. #12
    Quote Originally Posted by iCode View Post
    I don't think they will be able to do this.
    I know... It is much simpler, but its not cut & paste...
    Managed Service Provider - www.OpticIP.com
    Public & Private Cloud
    Solutions | SSD SANs | High IOP's | CDN Solutions
    Phoenix/Chandler AZ Colocation | 48U Cabinets | Data Halls | TIA-942 Tier 4 Facility

  13. #13
    Join Date
    Feb 2002
    Location
    New York
    Posts
    791
    Thank you I got it working.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    just a programmer

  14. #14
    Join Date
    Mar 2013
    Posts
    1,294
    FYI heredocs are recommended as opposed to multiple concatenating assignment operators

  15. #15
    Join Date
    Aug 2001
    Posts
    5,068
    Quote Originally Posted by gingir View Post
    FYI heredocs are recommended as opposed to multiple concatenating assignment operators
    Performance-wise it is negligible, there is barely a difference.
    Sitemeer.com - Is your site up?
    Multi-Location Service Availability Check ● yes, we do HTTPS & IDN!


Similar Threads

  1. php email with attachment and smtp authentication
    By mjfroggy in forum Programming Discussion
    Replies: 4
    Last Post: 12-28-2012, 10:16 PM
  2. Php email attachment
    By Lilmamaisa in forum Programming Discussion
    Replies: 4
    Last Post: 12-01-2009, 05:03 PM
  3. Bash script to send an email with attachment
    By kopite in forum Programming Discussion
    Replies: 11
    Last Post: 06-18-2008, 03:31 PM
  4. Need help how to add attachment in php script
    By cannibal in forum Programming Discussion
    Replies: 6
    Last Post: 02-12-2006, 01:36 PM
  5. PHP Email Attachment
    By mikeman23 in forum Dedicated Server
    Replies: 2
    Last Post: 03-22-2003, 01:11 PM

Posting Permissions

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