PHP
  Home arrow PHP arrow Page 2 - Email Management Details
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Email Management Details
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2006-11-16


    Table of Contents:
  • Email Management Details
  • Attaching a File to a Message
  • Attached Images for HTML Emails
  • A Word About Email Delivery
  • Exam Prep Questions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Email Management Details - Attaching a File to a Message
    ( Page 2 of 5 )

    To attach a file to an email message, you have to create a multipart MIME message.

    • Pass these headers as the fourth parameter to mail():

      MIME-Version: 1.0 Content-Type: multipart/mixed;
      boundary="php-12345"

      Note the boundary on the end of the Content-Type. The boundary is an arbitrary US-ASCII string that tells MUAs when the end of a MIME block has been reached. You can set it to be whatever you want, provided you set it to something that isn't going to appear in your email message.

    • The first part of your message string should be a plain text message like this:

      If you are reading this, it means that your email
      client does not support MIME. Please upgrade your email
      client to one that supports MIME. —php-12345

      Note the boundary marker after the message. This tells MIME that you've reached the end of this particular part of the email message. You can now add more parts to your email message.

    • The second part of your "message" string should be the message itself. You need to add Content-Type and Content-Transfer-Encoding headers to the message, followed by the message itself.

      Content-Type: text/plain; charset="iso-8859-1"
      Content-Transfer-Encoding: 7bit
      Hello, this is my plain text message. If you can see
      this, then hopefully my MIME email is working fine.
      —php-12345

      Note the boundary marker after the message. You have to put the boundary marker at the end of each part of the message.

    • Next come the attachments. Each attachment gets added to the "message" string as well. You need to add Content-Type and Content-Transfer-Encoding headers, and then the attachment.

      If the attachment isn't plain text, you should encode it using base64 encoding:

      Content-Type: image/jpeg
      Content-Transfer-Encoding: base64
      <message goes here>
      —php-12345
    <?php
    
    // who is the email going to?
    // change this to be *your* email address ;-)
    
    $to = "stuart";
    
    // what is the message?
    
    $message = "This is my third email sent from "
             . "<a
    href="http://www.php.net">PHP</a>.\r\n" . "This email has an attached image\r\n"; // don't forget our blank line $blank_line = "\r\n"; // define our boundary $boundary_text = "php-12345"; $boundary = "—" . $boundary_text . "\r\n"; $last_boundary = "—" . $boundary_text . "—\r\n"; // add the MIME headers $more_headers = "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;
    boundary="" . $boundary_text . ""\r\n"; // create the first part of the message $mime_message = "If you are reading this, it means
    that your e-mail client\r\n" . "does not support MIME. Please
    upgrade your e-mail client\r\n" . "to one that does support MIME.\r\n" . $boundary; // add the second part of the message $mime_message .= "Content-Type: text/html;
    charset="iso-8859-1"\r\n" . "Content-Transfer-Encoding:
    7bit\r\n" . $blank_line . $message . $boundary; // now add the attachment $mime_message .= "Content-Type: image/gif;
    name="php.gif"\r\n" . "Content-Transfer-Encoding:
    base64\r\n" . "Content-disposition: attachment;
    file="php.gif"\r\n" . $blank_line . chunk_split(base64_encode
    (file_get_contents("php.gif"))) . "\r\n" . $last_boundary; // send the email $result = mail ( $to, "My first HTML e-mail with an attachment", $mime_message, $more_headers ); ?>

    You can add as many attachments as you want, but remember—they can make the email quite large to download, and many users are still using dial-up rather than broadband!

    The last boundary marker in the email message must end with . For example, if our boundary marker is

    —php-12345

    the last marker in the email message would be

    —php-12345—

    instead.



     
     
    >>> More PHP Articles          >>> More By Sams Publishing
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT