Chapter 9. Zend_Mail

Table of Contents

9.1. Introduction
9.2. Sending via SMTP
9.3. Sending Multiple Mails per SMTP Connection
9.4. Using Different Transports
9.5. HTML E-Mail
9.6. Attachments
9.7. Adding Recipients
9.8. Controlling the MIME Boundary
9.9. Additional Headers
9.10. Character Sets
9.11. Encoding
9.12. SMTP Authentication

9.1. Introduction

Zend_Mail provides generalized functionality to compose and send both text and MIME-compliant multipart e-mail messages. Mail can be sent with Zend_Mail via the php built-in mail() function or via direct SMTP connection.

Example 9.1. Simple E-Mail with Zend_Mail

A simple e-mail consists of some recipients, a subject, a body and a sender. To send such a mail using the PHP mail() function, do the following:

require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('', 'Some Sender');
$mail->addTo('', 'Some Recipient');

[Note] Note
In order to send an e-mail with Zend_Mail you have to specify at least one recipient, a sender (e.g., with setFrom()), and a message body (text and/or HTML).

For most mail attributes there are "get" methods to read the information stored in the mail object. For further details, please refer to the API documentation. A special one is getRecipients(). It returns an array with all recipient e-mail addresses that were added prior to the method call.

For security reasons, Zend_Mail filters all header fields to prevent header injection with newline (\n) characters.