July 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Sending Email with PHP

  • November 5, 2008
  • By W. Jason Gilmore
  • Send Email »
  • More Articles »

Sending Email Using the Zend Framework

The first two solutions provided in this tutorial progressively solve various obstacles you'll encounter when sending email through a PHP script. However, many unresolved tasks remain. For instance, how can you send HTML-formatted email? Include attachments? If you're looking for the total package when it comes to sending email, capable of doing everything you could possibly conceive in this regards, look no further than the Zend Framework.

The Zend Framework's Zend_Mail component, when coupled with the Zend Framework's approach to minimizing redundancy and maximizing portability, offers the ideal solution for configuring and transmitting email through PHP-driven webpages. In this concluding section, I'll presume you're familiar with the Zend Framework and therefore focus upon my particular approach to using the Zend_Mail component.

First, within the application's config.ini file, I define various parameters used to configure the transmission solution:

; email

email.smtpserver = smtp.gmail.com
email.username   = wj@wjgilmore.com
email.password   = supersecret
email.support    = support@wjgilmore.com

Within the bootstrap.php file, I configure Zend_Mail's transport mechanism, drawing upon the parameters defined in config.ini:

$mailConfigs = array('auth' => 'login',
                     'username' => $config->email->username,
                     'password' => $config->email->password,
                     'ssl' => 'tls');

$tr = new Zend_Mail_Transport_Smtp($config->email->smtpserver,
                                   $mailConfigs);
Zend_Mail::setDefaultTransport($tr);

Although this executes with each request, the overhead required to do so is minimal and will not affect performance. Finally, I invoke code similar to the following from within the appropriate controllers:

try {
   // Create a new mail object
   $mail = new Zend_Mail();

   $mail->setFrom($this->config->email->from_admin);
   $mail->addTo("jason@example.com");
   $mail->setSubject("Your account has been created");

   $email = "Thank you for registering!";

   $mail->setBodyText($email);
   $mail->send();

   $this->view->success = 1;
} catch (Exception $e) {
   $this->view->errors[] = "We were unable to send your
      confirmation email. Please contact
      {$this->config->email->support}.";
}

If you'd like to add an attachment, all you need to do is invoke the $mail object's createAttachment() method:

$mail->createAttachment("brochure.pdf");

If you want to send HTML-formatted email, just use the setBodyHtml() method instead of setBodyText():

$mail->sendBodyHTML("Thank <b>you</b> for registering!");

Where to From Here?

Sending email from your PHP-powered websites is easy once you've been provided with the necessary background, so hopefully this tutorial helped alleviate any initial confusion you had in this regard. For further information, check out the following resources for more information about sending email using PHP:

About the Author

Jason Gilmore is founder of a web development and consulting firm based out of Columbus, Ohio. Formerly Apress' open source editor, Jason fostered the development of more than 60 books, along the way helping to transform their open source line into one of the industry's most respected publishing programs. He's the author of several books, including the best-selling Beginning PHP and MySQL: From Novice to Professional (currently in its third edition), Beginning PHP and PostgreSQL: From Novice to Professional, and Beginning PHP and Oracle: From Novice to Professional.

Jason is cofounder of CodeMash, a nonprofit organization tasked with hosting an annual namesake developer's conference, and was a member of the 2008 MySQL Conference speaker selection board. Jason has over 100 articles to his credit within prominent publications such as Developer.com, Linux Magazine, and TechTarget.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel