I just got finished with an update to one of my sites, the Informative Post, which uses the Zend_Mail component frequently. Zend_Mail is a component that is apart of the Zend Framework, and if you haven’t ever used it, now is as good of a time as any!
It is my first time working with Zend_Mail and I must say that I am impressed. This component alone would influence me to use the Zend Framework. It makes emailing from your site so simple. You don’t have to deal with the HTTP headers or anything. Let me show you the code that I used to send a simple email notifying a member that I have received an article that they just submitted.
<?php
include_once 'Zend/Mail.php';
$subject = 'We Have Received Your Article';
$fromEmail = 'admin@gmail.com';
$fromName = 'Administrator';
$toEmail = 'author@email.com';
$toName = 'Author Name';
$message= <<<EMAIL
Hi $toName!
I just wanted to notify you and let you know that I received your article and it will be reviewed shortly.
Regards,
Douglas Brown
Follow Me On Twitter: http://www.twitter.com/dlbrown06
EMAIL;
$mail = new Zend_Mail();
$mail->setSubject($subject);
$mail->setFrom($fromEmail, $fromName);
$mail->addTo($toEmail, $toName);
$mail->setBodyText($message);
$mail->send();
?>
It is as simple as that. Not too bad is it? You can also send an HTML email. It is just as simple. You would just add setBodyHtml($messageHTML). $messageHTML would contain the HTML and text that you would like to email. You should still add the setBodyText method so that if someone who opens up their email doesn’t not want to display the email in HTML, they have the alternative to view it in plain text.
Zend_Mail Tutorial Conclusion
I know that you are thinking that this is a relatively short tutorial, and your right! The Zend Framework’s Zend_Mail is very simple to use with a minimal amount of code to produce to let you take better advantage of your available time. You can send an email in 6 lines of code if you would like! If you feel that I made a mistake in the above code or that you have a better method to use, please let me know, as I would love to learn how to use this component more efficiently.
In my next post, I am planning on showing you how to use the Flickr API to bring your photos to your own site with ease!
Recent Comments