October 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Sending Email from your PHP Applications

  • February 4, 2005
  • By W. Jason Gilmore
  • Send Email »
  • More Articles »

Sending to Multiple Recipients

In the last example, you may have noticed the send() method requires that the recipient address is presented as an array element. This is because send() allows for emails to be sent to multiple users by simply appending new elements to the array. The following example sends an email to two users, namely jason@example.com and rosemarie@example.com:

// Send the email to editor@example.com
$result = $mail->send(array('jason@example.com', 'rosemarie@example.com'));

You can also specify other recipients in the same fashion using the setCc() and setBc() methods.

Sending an Attachment

The question of how to send attachments using PHP's mail() function comes up quite frequently in newsgroups. I'm happy to forego the gory details of how it's accomplished in this tutorial, because the HTML Mime Mail class resolves the problem quite easily. In the following example I'll send an email with an attachment titled htmlmimemail.doc to rosemarie@example.com:

<?php

    require_once("htmlMimeMail5/htmlMimeMail5.php");

    // Instantiate a new HTML Mime Mail object
    $mail = new htmlMimeMail5();

    // Set the From and Reply-To headers
    $mail->setFrom("Jason <jason@example.com>");
    $mail->setReturnPath("jason@example.com");

    // Set the Subject
    $mail->setSubject("Test with attached email");

    // Set the body
    $mail->setText("Please find the new tutorial attached. Thank you!");

    // Retrieve a file for attachment
    $attachment = new fileAttachment("htmlmimemail.doc", "application/vnd.ms-word");

    // Attach the file, assigning it a name and a corresponding Mime-type.
    $mail->addAttachment($attachment);

    // Send the email to rosemarie@example.com
    $result = $mail->send(array('rosemarie@example.com'));
?>

Sending out HTML formatted email

Personally, I think receiving HTML-formatted email is akin to scratching one's nails on the blackboard. There's simply no reason for it. However there has been such an ongoing interest in learning how to do so programmatically that I don't think this tutorial would be complete without some discussion of the matter. Like attachments, sending HTML-formatted email with HTML Mime Mail is amazingly easy. The following example sends rosemarie@example.com an HTML-formatted email with a gray background and a GIF image named advertisement.gif. Note that the second (optional) parameter of the setHTML() method specifies the location of the images that should be embedded in the email. You also have the option of pointing the images to an online location, however keep in mind that some email clients (Squirrelmail for example) will not render these images for reasons of security.

<?php

    require_once("htmlMimeMail5/htmlMimeMail5.php");

    // Instantiate a new HTML Mime Mail object
    $mail = new htmlMimeMail5();

    // Set the From and Reply-To headers
    $mail->setFrom("Jason <jason@example.com>");
    $mail->setReturnPath("jason@example.com");

    // Set the Subject
    $mail->setSubject("HTML formatted email");

    $html = "
    <body bgcolor='#808080'>
    <img src='advertisement.gif' alt='Buy my book!'>
    </body>";

    // Create the HTML email
    $mail->setHTML($html, "/home/apache/newsletter/images//");

    // Send the email to rosemarie@example.com
    $result = $mail->send(array('rosemarie@example.com'));

?>

Sending out Bulk Email

Web-based PHP scripts aren't designed to run for long periods of time (the default execution time is 30 seconds), and this presents a problem if you need to send a weekly newsletter to a large number of recipients. Therefore you'll want to consider alternative solutions for such purposes. These solutions not only facilitate subscriber management, but also support a wide variety of additional useful features such as auto-responses, web-based subscription/unsubscription, and web-based newsletter administration. As you might imagine, several open source software packages are available. I'll highlight two of the most popular packages here:

While these open source solutions are indeed powerful, both serve primary roles as mailing list administrators, and accordingly are lacking in a few features one might expect of a newsletter-specific application. For instance, scheduled mailings, response tracking, and custom HTML templates are just a few features that could greatly enhance the utility of this service. That said, if you have a budget available for purchasing third-party services, I suggest taking some time to evaluate a few commercial offerings. I'll highlight some of the more promising services here:

Alternatively you could take advantage of PHP's command-line interface (CLI), enabled by default as of version 4.3.0. Using the CLI, you can create scripts that execute directly from the command-line, or on a scheduled basis using a daemon such as CRON. PHP CLI scripts don't lack any of the features available to you when building Web applications, and have the additional advantage of being able to execute for a long period of time.

Conclusion

Communicating via email is a wonderfully effective and cheap means for enhancing your website's capabilities and keeping users updated regarding your organization's latest offerings. Using PHP and the great HTML Mime Mail class, doing so programmatically has never been easier. If you found this tutorial useful, I welcome you to email me with your comments and questions at wj AT wjgilmore.com. Thank you!

About the Author

W. Jason Gilmore (http://www.wjgilmore.com/) is the Open Source Editor for Apress (http://www.apress.com/). He's the author of Beginning PHP 5 and MySQL: Novice to Professional (Apress, 2004. 748pp.). His work has been featured within many of the computing industry's leading publications, including Linux Magazine, O'Reillynet, Devshed, Zend.com, and Webreview.





Page 2 of 2



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel