From Swift Mailer to Zend_Mail

I’ve recently switched from Swift Mailer to Zend_Mail and to be honest, I’m loving it. Finally someone developed a lightweight, powerful and easy to use email component! Zend_Mail creates and sends e-mail messages with the format text or HTML and it allows e-mail to be sent in an easy form while preventing mail injection. Zend_Mime is a support set for handling multi-part MIME messages.

Simple email with Zend_Mail

$mail = new Zend_Mail();
$mail->setBodyText("This is the text of the mail");
$mail->setFrom("sender@example.com", "Some Sender");
$mail->addTo("recipient@example.com", "Some Recipient");
$mail->setSubject("Test Subject");
$mail->send();

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.

Zend_Mail fluent interface

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

Zend_Mail messages can be sent via SMTP, so Zend_Mail_Transport_SMTP needs to be created and registered with Zend_Mail before the send() method is called.

Sending email via SMTP

$tr = new Zend_Mail_Transport_Smtp("mail.example.com")
Zend_Mail::setDefaultTransport($tr);

$mail = new Zend_Mail();
$mail->setBodyText("This is the text of the mail.");
$mail->setFrom("sender@example.com", "Some Sender")
$mail->addTo("recipient@example.com", "Some Recipient");
$mail->setSubject("Test Subject");
$mail->send();

Sending multiple emails per SMTP connection

$mail = new Zend_Mail();
$tr = new Zend_Mail_Transport_Smtp("mail.example.com");
Zend_Mail::setDefaultTransport($tr);

$tr->connect();
for ($i = 0; $i < 5; $i++) {
        $mail->setBodyText("This is the text of the mail.");
        $mail->setFrom("sender@example.com", "Some Sender");
        $mail->addTo("recipient@example.com", "Some Recipient");
        $mail->setSubject("Test Subject");
        $mail->send();
}
$tr->disconnect();

To send an e-mail in HTML format, set the body using the method setBodyHTML() instead of setBodyText(). The MIME content type will automatically be set to text/html then. If you use both HTML and Text bodies, a multipart/alternative MIME message will automatically be generated:

Sending an HTML email

$mail = new Zend_Mail();
$mail->setBodyText("My Nice Test Text");
$mail->setBodyHtml("My Nice Test Text");
$mail->setFrom("sender@example.com", "Some Sender");
$mail->addTo("recipient@example.com", "Some Recipient");
$mail->setSubject("TestSubject");
$mail->send();

How easy was that!? If you haven’t used any of the Zend Framework components yet, then this is a great opportunity to do so.

Articles

About these ads

8 thoughts on “From Swift Mailer to Zend_Mail

  1. SwiftMailer is complex and over-engineered. It solves the problem of sending an email, but adds extra complexity to the system. This is something that is not always taken into consideration when selecting the right library.

  2. Zend Mail is cool. Zend framework rocks. I have migrated to PHP world in 2007 after working in Java for 10 years!
    The first thing I did after migrating to PHP is chose Zend Framework.
    For me Zend Framework gives the same “flexibility” as Spring framework in Java.
    -Rapid Style development
    -Use your own MVC/Enterprise app development pattern
    -Mix & match what you need
    -Future is secure :) as other frameworks make you “tightly coupled” and your application is stuck.

    That’s my 2cents on why I love Zend Framework

  3. Pingback: Enviando correo con Zend_Mail « CuatroXL - Cuatro Xl

  4. How about Mass mailings? Is Zend_Mail up for that? How would I go about sending a personal e-mail to say 10,000 recipients?

  5. I think that’s up to you to implement a batch-queuing system. Ideally, you’ll want to send them in batches to optimize resources.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s