Home arrow PHP arrow Email Management Details

Email Management Details

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).

TABLE OF CONTENTS:
  1. Email Management Details
  2. Attaching a File to a Message
  3. Attached Images for HTML Emails
  4. A Word About Email Delivery
  5. Exam Prep Questions
By: Sams Publishing
Rating: starstarstarstarstar / 8
November 16, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: