Chapter 25. Zend_Mail

Table of Contents

25.1. Introduction
25.1.1. Getting started
25.1.2. Configuring the default sendmail transport
25.2. Sending via SMTP
25.3. Sending Multiple Mails per SMTP Connection
25.4. Using Different Transports
25.5. HTML E-Mail
25.6. Attachments
25.7. Adding Recipients
25.8. Controlling the MIME Boundary
25.9. Additional Headers
25.10. Character Sets
25.11. Encoding
25.12. SMTP Authentication
25.13. Securing SMTP Transport
25.14. Reading Mail Messages
25.14.1. Simple example using Pop3
25.14.2. Opening a local storage
25.14.3. Opening a remote storage
25.14.4. Fetching messages and simple methods
25.14.5. Working with messages
25.14.6. Checking for flags
25.14.7. Using folders
25.14.8. Advanced Use
25.14.8.1. Using NOOP
25.14.8.2. Caching instances
25.14.8.3. Extending Protocol Classes
25.14.8.4. Using Quota (since 1.5)

25.1. Introduction

25.1.1. Getting started

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 default Zend_Mail_Transport_Sendmail transport or via Zend_Mail_Transport_Smtp.

Example 25.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 Zend_Mail_Transport_Sendmail, do the following:

<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('somebody@example.com', 'Some Sender');
$mail->addTo('somebody_else@example.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send();
            

[Note] Minimum definitions

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.

You also can use most methods of the Zend_Mail object with a convenient fluent interface. A fluent interface means that each method returns a reference to the object on which it was called, so you can immediately call another method.

<?php
require_once 'Zend/Mail.php';
$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.')
    ->setFrom('somebody@example.com', 'Some Sender')
    ->addTo('somebody_else@example.com', 'Some Recipient')
    ->setSubject('TestSubject')
    ->send();
        

25.1.2. Configuring the default sendmail transport

The default transport for a Zend_Mail instance is Zend_Mail_Transport_Sendmail. It is essentially a wrapper to the PHP mail() function. If you wish to pass additional parameters to the mail() function, simply create a new transport instance and pass your parameters to the constructor. The new transport instance can then act as the default Zend_Mail transport, or it can be passed to the send() method of Zend_Mail.

Example 25.2. Passing additional parameters to the Zend_Mail_Transport_Sendmail transport

This example shows how to change the Return-Path of the mail() function.

<?php
require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Transport/Sendmail.php';

$tr = new Zend_Mail_Transport_Sendmail('-freturn_to_me@example.com');
Zend_Mail::setDefaultTransport($tr);

$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('somebody@example.com', 'Some Sender');
$mail->addTo('somebody_else@example.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send();
            

[Note] Safe mode restrictions

The optional additional parameters will be cause the mail() function to fail if PHP is running in safe mode.