PHP
  Home arrow PHP arrow 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
    ( Page 1 of 5 )

    In this second part of a two-part article, you'll learn how to manage email headers when creating a PHP email program, attach a file to a message, and more. This article is excerpted from chapter eight of the Zend PHP Certification Study Guide, written by Zend Technlogies (Sams; ISBN: 0672327090).

    Managing Email Headers

    Email headers are lines of text that go at the start of an email message. Headers hold information used by MTAs and MUAs. If you pass a list of additional headers into the mail() function, it will automatically add these headers to your email message. Each header in the list must be separated by \r\n.

    The Cc: and Bcc: Headers

    The Cc: and Bcc: headers allow you to send a copy of an email message to other people. All recipients will be able to see the list of addresses in the To: and Cc: lines of the email, but will not be able to see the list of addresses in the Bcc: line.

    The Cc: and Bcc: headers are optional. If you provide either of them (or both of them), they must follow the same rules about email addresses as the to parameter to mail().

    The From: Header

    The From: header tells the MTA who is sending the email message.

    If you do not provide a From: header, PHP might or might not add one for you:

    • If you are sending email via the sendmail wrapper, PHP will leave it to the MTA to add a default From: header.

    • If you are sending email on Windows without using the sendmail wrapper, PHP will use the sendmail_from setting in php.ini to set a default From: header.

    Setting the Subject

    The second parameter to mail() is a string containing the "subject" of the email. Whatever you put in this string will appear in the Subject: header of the email.

    Formatting an Email Message

    The third parameter to mail() is the email message itself.

    Plain-Text Emails

    Plain-text emails are normal 7-bit US-ASCII strings with each line terminated by \r\n. The following code will send a plain-text email, as long as PHP is configured to correctly send email and the MTA is working, too.


    Note - Some MTAs on UNIX will accept messages that just use \n as the end-of-line sequence. This is nonstandard behavior, and you should expect to have problems eventually.


    <?php
    
    // who is the email going to?
    // change this to be *your* email address ;-)
    
    $to = "stuart";
    
    // what is the message?
    
    $message = "This is my first email message, sent
    from PHP.\r\n" . "This is a second line of text\r\n"; // who else do we want to send the message to? $more_headers = "Cc: stuart\r\n"; // send the email $result = mail ( $to, "My first e-mail sent from PHP", $message, $more_headers ); ?>

    By default, all emails sent via mail() are plain-text emails. If you want to send HTML emails, you need to create a simple MIME-encoded email.

    Basic HTML Emails

    The Multipurpose Internet Mail Extensions (MIME) define a standardized way of sending emails with attachments, and/or with content that isn't 7-bit US-ASCII.

    You can find a reference to the MIME standard in the "Further Reading" section at the end of this chapter.

    To send a basic HTML email, all you need to do is this:

    • Add these additional headers:

    • MIME-Version: 1.0 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit

    • Pass your HTML content in a string as the third parameter to mail().

    Any images, links, stylesheets—anything at all that the web browser has to download—must be full URLs. Note that, for security and privacy reasons, most email clients will not automatically download anything that's referenced in an HTML email.

    If you want to add images to your email, you need to use attachments.



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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