SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Encoding emails with attachments from within PHP

    Hi guys!

    I've got some code for sending emails with attachments, which I'm seeing fine, but are displaying in raw format for people using Microsoft Outlook.

    I don't know enough about encoding emails and I'm just fumbling around, so I thought I'd ask you guys!

    Here's the code:
    PHP Code:
        function doComposeEmail ($arrayFileInvoicePDF$arrayActions) {

            
    $arrayBookingMessage = array (

                
    'email' => $arrayActions['HotelEmail'],
                
    'subject' => "Invoice",
                
    'message' => "",
                
    'headers' => "From: email@address.com" "\r\n" "Reply-To: email@address.com" "\r\n" "CC: email@address.com"

                                        
    ); // end array

            
    $file fopen($arrayFileInvoicePDF['path'].$arrayFileInvoicePDF['file'],'rb');
            
    $data fread($file,filesize($arrayFileInvoicePDF['path'].$arrayFileInvoicePDF['file']));

            
    fclose($file);

            
    // Generate a boundary string
            
    $semi_rand md5(time());
            
    $mime_boundary "==Multipart_Boundary_x".$semi_rand."x";
            
    //$mime_boundary = "PHP-mixed-".$semi_rand."";
     
            // Add the headers for a file attachment
            
    $arrayBookingMessage['headers'] .= "\r\n".'MIME-Version: 1.0'."\r\n".'Content-Type: multipart/mixed;'."\r\n".'boundary="'.$mime_boundary.'"';

            
    // Add a multipart boundary above the plain message
            
    $arrayBookingMessage['message']  = "This is a multi-part message in MIME format."."\n\n".$mime_boundary."\r\n".'Content-Type: text/plain; charset="iso-8859-1"'."\r\n"."Content-Transfer-Encoding: 7bit"."\r\n".$arrayBookingMessage['message']."\r\n";

            
    // Base64 encode the file data
            
    $data chunk_split(base64_encode($data));

            
    // Add file attachment to the message
            
    $arrayBookingMessage['message'] .= "--".$mime_boundary."\r\n"."Content-Type: pdf;"."\r\n".'name="'.$arrayFileInvoicePDF['file'].'"'."\r\n"."Content-Disposition: attachment;"."\r\n".' filename="'.$arrayFileInvoicePDF['file'].'"'."\r\n"."Content-Transfer-Encoding: base64"."\r\n".$data."\r\n".$mime_boundary."\r\n";

            
    $arrayBookingMessage['message'] .= "Please find enclosed the current invoice." "\r\n";

            
    mail($arrayBookingMessage['email'], $arrayBookingMessage['subject'], $arrayBookingMessage['message'], $arrayBookingMessage['headers']);

        } 
    // end function doComposeEmail


            // http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment

        
    $arrayFileInvoicePDF = array (

            
    'path' => "library/temporary/",
            
    'file' => "invoice-" strtolower($_SESSION['CompanyCodeActive']) . ".pdf",
            
    'code' => $_SESSION['CompanyCodeActive']

                                    ); 
    // end array

        
    doGeneratePDF($arrayFileInvoicePDF$this->arrayActions); // creates the PDF that's attached
        
    doComposeEmail($arrayFileInvoicePDF$this->arrayActions); 
    Any help would be great!
    Last edited by Forbes; Dec 4, 2008 at 08:43. Reason: Typos

  2. #2
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No takers for this? I could really do with some help!

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I recomend a mailing library such as swiftmailer or phpmailer.

    I never bothered to learn much about the formats of emails, but a quick glance at your code makes me wonder if you should be using a new line after your headers, and a different boundary value for each part.

  4. #4
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, thanks for the pointers!

    I've zero clue how this stuff works. The code I have has been lifted from some tutorial.

    I'll follow up and see what's what!


Tags for this Thread

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
  •