www.webdeveloper.com
Results 1 to 6 of 6

Thread: Email File Attachment

  1. #1
    Join Date
    Jun 2007
    Posts
    191

    Email File Attachment

    I am trying to send an attachment with an email script. I can send the email but the attachment is not working correctly. I grabbed this script from a template on the net.

    PHP Code:
    $from "Jane Doe <noreply@domain.com>";
            
    $subject "Here is your attachment";
            
    $fileatt "flyer.jpg";
            
    $fileatttype "image/jpg";
            
    $fileattname "Flyer.jpg";
            
    $headers "From: $from";
            
    $file fopen$fileatt'rb' );
            
    $data fread$filefilesize$fileatt ) );
            
    fclose$file );
            
    $semi_rand md5time() );
            
    $mime_boundary "==Multipart_Boundary_x{$semi_rand}x";
            
    $headers .= "nMIME-Version: 1.0n" "Content-Type: multipart/mixed;n" " boundary=\"{$mime_boundary}\"";
            
    $message "This is a multi-part message in MIME format.nn" "{$mime_boundary}n" "Content-Type: text/plain; charset=\"iso-8859-1\"n" "Content-Transfer-Encoding: 7bitnn" $message "nn";
            
    $data chunk_splitbase64_encode$data ) );
            
    $message .= "{$mime_boundary}n" "Content-Type: {$fileatttype};n" " name=\"{$fileattname}\"n" "Content-Disposition: attachment;n" " filename=\"{$fileattname}\"n" "Content-Transfer-Encoding: base64nn" $data "nn" "{$mime_boundary}n";
            if( 
    mail$to$subject$message$headers ) ) {
                echo 
    "<p>The email was sent.</p>";
            }
            else {
                echo 
    "<p>There was an error sending the mail.</p>";
            } 

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,536
    Looks like your script is just using the letter "n" as the mail header separator. It probably is some sort of copy error, and should be "\n" for the newline character. In fact, there is a good probability that you will need to use "\r\n" in each instance (carriage-return/newline).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jun 2007
    Posts
    191

    Changes

    This is what I changed the code to

    PHP Code:
        $from "Jane Doe <noreply@domain.com>"
            
    $subject "Here is your attachment"
            
    $fileatt "flyer.jpg"
            
    $fileatttype "image/jpg"
            
    $fileattname "Flyer.jpg"
            
    $headers "From: $from"
            
    $file fopen$fileatt'rb' ); 
            
    $data fread$filefilesize$fileatt ) ); 
            
    fclose$file ); 
            
    $semi_rand md5time() ); 
            
    $mime_boundary "==Multipart_Boundary_x{$semi_rand}x"
            
    $headers .= "nMIME-Version: 1.0\r\n" "Content-Type: multipart/mixed;\r\n" " boundary=\"{$mime_boundary}\""
            
    $message "This is a multi-part message in MIME format.\r\n" "{$mime_boundary}\r\n" "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" "Content-Transfer-Encoding: 7bit\r\n" $message "\r\n"
            
    $data chunk_splitbase64_encode$data ) ); 
            
    $message .= "{$mime_boundary}\r\n" "Content-Type: {$fileatttype};\r\n" " name=\"{$fileattname}\"\r\n" "Content-Disposition: attachment;\r\n" " filename=\"{$fileattname}\"\r\n" "Content-Transfer-Encoding: base64\r\n" $data "\r\n" "{$mime_boundary}\r\n"
            if( 
    mail$to$subject$message$headers ) ) { 
                echo 
    "<p>The email was sent.</p>"
            } 
            else { 
                echo 
    "<p>There was an error sending the mail.</p>"
            } 
    I get the email minus the attachment now. Any ideas?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,536
    I think you missed one:
    PHP Code:
    //...........vvvv
    $headers .= "\r\nMIME-Version: 1.0\r\n" "Content-Type: multipart/mixed;\r\n" " boundary=\"{$mime_boundary}\""
    Frankly, I hate messing with the mail stuff, and just use PHPMailer when I need to do anything more than send a simple email message.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jun 2007
    Posts
    191
    I have never used PHPMailer. Is it easy to use?

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,536
    "Easy" is relative, I guess. It probably helps if you have a basic understanding of PHP object-oriented syntax, but there are a number of pretty good examples on their site that cover most typical tasks.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles