Sending email with Zend_Mail
Posted August 23rd, 2008 in PHP
The Zend Framework is a PHP framework with many individual components that do not require you to use the whole framework. In this post I will look at how to send an email using the Zend Framework's Zend_Mail component.
Before using any component from the Zend Framework you need to add the path of the framework to your include path. This is because the various component require other library files and assume the "Zend" directory is already in the include path.
Basic usage
Having done this, the example below shows the basic usage of sending an email using Zend_Mail:
require_once('Zend/Mail.php');
$mail = new Zend_Mail();
$mail->setBodyText('This is an example message body');
$mail->setFrom('chris@example.com', 'Chris Hope');
$mail->addTo('john@example.com', 'John Smith');
$mail->setSubject('This is an example subject');
$mail->send();
Setting the HTML body text
If you want to add an HTML version of the message body you can do the following.
$mail->setBodyHtml($html);
Note that you don't have to have both an HTML body and a plain text body but you must specify at least one of them. If you don't you'll get a nasty error message along these lines:
Fatal error: Uncaught exception 'Zend_Mail_Transport_Exception' with message 'No body specified' in /path/to/Zend/Mail/Transport/Abstract.php:284
Stack trace:
#0 /path/to/Zend/Mail/Transport/Abstract.php(313): Zend_Mail_Transport_Abstract->_buildBody()
#1 /path/to/Zend/Mail.php(720): Zend_Mail_Transport_Abstract->send(Object(Zend_Mail))
#2 /path/to/test_script.php(15): Zend_Mail->send()
#3 {main} thrown in /path/to/Zend/Mail/Transport/Abstract.php on line 284
To, CC and BCC recipients
You can also specify multiple "to" recipients by calling the addTo() method multiple times:
$mail->addTo('john@example.com', 'John Smith');
$mail->aadTo('jane@example.com', 'Jane Doe');
And also CC and BCC recipienrs like so:
$mail->addCc('john@example.com', 'John Smith');
$mail->addBcc('jane@example.com');
The addTo, setFrom and addCc all take a second optional parameter in which you can specify the person's name, the first parameter being to set their email address. The addBcc method only has the email address parameter because the name is not set in the actual email for Bcc recipients.
Chaining it altogether
It's possible to chain the whole call together and send an email as follows instead. A lot of this style comes down to personal preference:
$mail = new Zend_Mail();
$mail->setBodyText('this is a test')
->setFrom('chris@example.com', 'Chris Hope')
->addTo('chris@example.com', 'Chris Hope')
->setSubject('This is a test subject')
->send();
Further reading
In future posts I will look at adding attachments to emails using Zend Framework Zend_Mail and specifying the transport to use i.e. sendmail vs an SMTP server.
Related posts:
- PHP email validation with filter_var (Monday, July 6th 2009)
- Using PHP IMAP functions to download email (Thursday, February 5th 2009)
- Using the Zend_Registry in PHP (Monday, January 19th 2009)
- Method chaining with PHP (Sunday, December 7th 2008)
- Database error handling with the Zend Framework (Sunday, May 11th 2008)
- Zend Framework Controller Router example (Saturday, April 12th 2008)
Subscribe / Follow / Email / Bookmark / Share
Use the buttons below to subscribe to my RSS feed to be notified next time something is posted, share this post with others, or subscribe by email to have my posts sent in a daily email, follow me on Twitter or follow me on Facebook.
At least one new post is usually made every day. See my posting schedule for more details.
