Programmer's Reference Guide

Zend_Mail

Introduction

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 #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:

  1. $mail = new Zend_Mail();
  2. $mail->setBodyText('This is the text of the mail.');
  3. $mail->setFrom('somebody@example.com', 'Some Sender');
  4. $mail->addTo('somebody_else@example.com', 'Some Recipient');
  5. $mail->setSubject('TestSubject');
  6. $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. 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.

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

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 #2 Passing additional parameters to the Zend_Mail_Transport_Sendmail transport

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

  1. $tr = new Zend_Mail_Transport_Sendmail('-freturn_to_me@example.com');
  2. Zend_Mail::setDefaultTransport($tr);
  3.  
  4. $mail = new Zend_Mail();
  5. $mail->setBodyText('This is the text of the mail.');
  6. $mail->setFrom('somebody@example.com', 'Some Sender');
  7. $mail->addTo('somebody_else@example.com', 'Some Recipient');
  8. $mail->setSubject('TestSubject');
  9. $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.

Warning

Sendmail Transport and Windows

As the PHP manual states the mail() function has different behaviour on Windows and on *nix based systems. Using the Sendmail Transport on Windows will not work in combination with addBcc(). The mail() function will sent to the BCC recipient such that all the other recipients can see him as recipient!

Therefore if you want to use BCC on a windows server, use the SMTP transport for sending!


Zend_Mail

Comments

On Windows there is another Problem, at least if you have Mercury Mailserver listening at localhost port 25 for php giving him mails to send.

Because of the line

if (0 === strpos(PHP_OS, 'WIN')) {

in the file /Zend/Mail/Transport/Sendmail.php and the code following the name you give to

$mail->addTo('somebody_else@example.com', 'Some Recipient');

in this case 'Some Recipient' will not be added correctly to your mailheader.

If you change the line to

if (0 === strpos(PHP_OS, 'xxxxxxxxxx')) {

and therefore treat the windows / mercury -combination the same way as you treat i. e. linux / postfix, than everything works fine.

To bad. Is that a bug? I do not understand, what for the line

if (0 === strpos(PHP_OS, 'WIN')) {

than is altogether.

Regards,
Marc
Hi all,

I've just learned about this topic.
I wanna ask, how to add recipients from a variable?

Case:

$rcpt = array('oo@gmail.com', 'cc@gmail.com', 'kk@gmail.com', 'yy@gmail.com');

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

The error is 501 invalid address.

Can you help me, please?

Thank you
@Ocky Harliansyah:

Given your $rcpt array, just:

$mail->setFrom('somebody@example.com', 'Some Sender');
foreach($rcpt as $recip) {
  $mail->addTo($recip, '');
}


Hope it helps.

+ Add A Comment

Please do not report issues via comments; use the ZF Issue Tracker.

If you have a JIRA/Crowd account, we suggest you login first before commenting.

  • BBCode is allowed in the comment markup

  • Select a Version

    Languages Available

    Components

    Search the Manual