PHP CookBook Free Open Book

PHP CookBook

Previous Section Next Section

Recipe 17.3 Sending MIME Mail

17.3.1 Problem

You want to send MIME email. For example, you want to send multipart messages with both plain-text and HTML portions and have MIME-aware mail readers automatically display the correct portion.

17.3.2 Solution

Use the Mail_mime class in PEAR:

require 'Mail.php';
require 'Mail/mime.php';

$to = 'adam@example.com, sklar@example.com';

$headers['From'] = 'webmaster@example.com';
$headers['Subject'] = 'New Version of PHP Released!';

// create MIME object
$mime = new Mail_mime;

// add body parts
$text = 'Text version of email';
$mime->setTXTBody($text);

$html = '<html><body>HTML version of email</body></html>';
$mime->setHTMLBody($html);

$file = '/path/to/file.png';
$mime->addAttachment($file, 'image/png');

// get MIME formatted message headers and body
$headers = $mime->headers($headers);
$body = $mime->get();

$message =& Mail::factory('mail');
$message->send($to, $headers, $body);

17.3.3 Discussion

PEAR's Mail_mime class provides an object-oriented interface to all the behind-the-scenes details involved in creating an email message that contains both text and HTML parts. The class is similar to PEAR's Mail class, but instead of defining the body as a string of text, you create a Mail_mime object and call its methods to add parts to the body:

// create MIME object
$mime = new Mail_mime;

// add body parts
$text = 'Text version of email';
$mime->setTXTBody($text);

$html = '<html><body>HTML version of email</body></html>';
$mime->setHTMLBody($html);

$file = '/path/to/file.txt';
$mime->addAttachment($file, 'text/plain');

// get MIME formatted message headers and body
$headers = $mime->headers($headers);
$body = $mime->get();

The Mail_mime::setTXTBody( ) and Mail_mime::setHTMLBody( ) methods add the plaintext and HTML body parts, respectively. Here, we pass in variables, but you can also pass a filename for Mail_mime to read. To use this option, pass true as the second parameter:

$text = '/path/to/email.txt';
$mime->setTXTBody($text, true);

To add an attachment to the message, such as a graphic or an archive, call Mail_mime::addAttachment( ) :

$file = '/path/to/file.png';
$mime->addAttachment($file,'image/png');

Pass the function to the location to the file and its MIME type.

Once the message is complete, do the final preparation and send it out:

// get MIME formatted message headers and body
$headers = $mime->headers($headers);
$body = $mime->get( );

$message =& Mail::factory('mail');
$message->send($to, $headers, $body);

First, you have the Mail_mime object provide properly formatted headers and body. You then use the parent Mail class to format the message and send it out with Mail_mime::send( ) .

17.3.4 See Also

Section 17.2 for sending regular email; Section 17.4 for more on retrieving mail; the PEAR Mail_Mime class at http://pear.php.net/package-info.php?package=Mail_Mime.

    Previous Section Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Z]


         Main Menu
    Main Page
    Table of content
    Copyright
    Preface
    Chapter 1. Strings
    Chapter 2. Numbers
    Chapter 3. Dates and Times
    Chapter 4. Arrays
    Chapter 5. Variables
    Chapter 6. Functions
    Chapter 7. Classes and Objects
    Chapter 8. Web Basics
    Chapter 9. Forms
    Chapter 10. Database Access
    Chapter 11. Web Automation
    Chapter 12. XML
    Chapter 13. Regular Expressions
    Chapter 14. Encryption and Security
    Chapter 15. Graphics
    Chapter 16. Internationalization and Localization
    Chapter 17. Internet Services
    17.1 Introduction
    Recipe 17.2 Sending Mail
    Recipe 17.3 Sending MIME Mail
    Recipe 17.4 Reading Mail with IMAP or POP3
    Recipe 17.5 Posting Messages to Usenet Newsgroups
    Recipe 17.6 Reading Usenet News Messages
    Recipe 17.7 Getting and Putting Files with FTP
    Recipe 17.8 Looking Up Addresses with LDAP
    Recipe 17.9 Using LDAP for User Authentication
    Recipe 17.10 Performing DNS Lookups
    Recipe 17.11 Checking if a Host Is Alive
    Recipe 17.12 Getting Information About a Domain Name
    Chapter 18. Files
    Chapter 19. Directories
    Chapter 20. Client-Side PHP
    Chapter 21. PEAR
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele