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.
Next: Attaching a File to a Message >>
More PHP Articles
More By Sams Publishing
|
This article is excerpted from chapter 8 of the Zend PHP Certification Study Guide, written by Zend Technologies (Sams; ISBN: 0672327090). Check it out today at your favorite bookstore. Buy this book now.
|
|