PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

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 590. 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
$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();

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. Double quotation is changed to single quotation and angle brackets to square brackets in the name of sender and recipients. If the marks are in email address, the marks will be removed.

You also can use most methods of the Zend_Mail object with a convenient fluent interface.

<?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();

Zend Framework