Sending Email with Attachments


You can also send email with attachments, but that takes some effort. To show how this works, we'll send an email with our image file from the previous chapter, image.jpg, attached. In this script, phpemailattachment.php, you specify whom the email is to, the subject, the message, the file you want to attach, and its MIME type (change the MIME type from image/jpeg to the correct type if you're not sending a JPEG):

 //Set these variables yourself: $to = "steve@ispname.com"; $subject = "Web mail"; $message = "This email has an attachment."; $attachment = "image.jpg"; $attachment_MIME_type = "image/jpeg"; 

Then the script reads in the file, storing its data in $data:

 $handle = fopen ($attachment, "rb"); $data = fread ($handle, filesize($attachment)); fclose ($handle); 

To send an attachment, you need to create a multipart form and encode the data using the PHP functions chunk_split and base64_encode:

 $boundary = "---Multipart_Boundary---"; $headers = "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"" . $boundary . "\""; $data = chunk_split(base64_encode($data)); 

Then you add the encoded attachment, as in phpemailattachment.php, Example 9-11.

Example 9-11. Sending email with an attachment, phpemailattachment.php
 <HTML>     <HEAD>         <TITLE>Sending email with attachments</TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>Sending email with attachments</H1>             <?php             //Set these variables yourself:             $to = "steve@ispname.com";             $subject = "Web mail";             $message = "This email has an attachment.";             $attachment = "image.jpg";             $attachment_MIME_type = "image/jpeg";             $handle = fopen ($attachment, "rb");             $data = fread ($handle, filesize($attachment));             fclose ($handle);             $boundary = "---Multipart_Boundary---";             $headers = "\nMIME-Version: 1.0\n" .             "Content-Type: multipart/mixed;\n" .             " boundary=\"" . $boundary . "\"";             $data = chunk_split(base64_encode($data));             $text = "--" . $boundary . "\n" .             "Content-Type:text/plain\nContent-Transfer-Encoding: 7bit\n\n" .             $message . "\n\n--" . $boundary . "\n" .             "Content-Type: " . $attachment_MIME_type . ";\n name=\"" .             $attachment . "\"\nContent-Transfer-Encoding: base64\n\n" .             $data . "\n\n--" . $boundary . "--\n";             $result = @mail($to, $subject, $text, $headers);             if($result) {                 echo "The email was sent.";             } else {                 echo "The email was not sent.";             }         ?>         </CENTER>     <BODY> </HTML> 

The results appear in Figure 9-10, where we've sent an email with a JPEG attachment from PHP. Very impressive.

Figure 9-10. Sending email with an attachment.




    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net