PHP
  Home arrow PHP arrow A MIME Mailer Class
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

A MIME Mailer Class
By: Chris Root
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 20
    2006-01-04


    Table of Contents:
  • A MIME Mailer Class
  • You've Got Mail
  • Adding an Attachment
  • Sending it on its way

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    A MIME Mailer Class
    ( Page 1 of 4 )

    Setting up a web application to send plain text email is easy. What if you need the email to handle other content, such as images or special document formats? This article explains how to design a PHP class for sending email with attachments.

    Email has become an important part of life these days. More and more web applications are expected to be capable of using email to communicate. Online stores in particular need a way of confirming orders, sending gifts certificates and distributing information about specials among other things. You may want to send an HTML formatted greeting to the registered visitors of your blog, publish a newsletter in an automated fashion or even code a custom email application for your own use on a personal web server. You may even want to provide basic web email services to users of a small web community.

    Sending plain text email is pretty easy in most web application environments. Within a PHP application you can use the "mail()" function. Where things get more interesting is when your requirements extend to other content such as images, HTML, PDF, RTF, CSV or other document formats

    This article outlines the design of a PHP class for sending email with attachments. Everything you need to do this is supplied by the core PHP system, so nothing additional (such as non-bundled extensions) should be needed save for the required set up and connection between PHP and a SMTP mail package such as Sendmail or Postfix. For details on setting up Sendmail and PHP, read through the appropriate section of the PHP manual. If you are already using the mail() function in PHP, you are ready to go right now.

    The Benefits of the Design

    Of late I have had my fill of code written with a single application and purpose in mind. The kind of code I mean is long procedural code, hardcoded with names and string values specific to that project, and almost always spread out over multiple documents. Want to modify it? Good luck! Want to use it somewhere else? Yeah right! Many times it might seem quicker to write something in this way, but when it comes time to alter it in any way you've got a problem. If you didn't write it in the first place, it makes it that much worse.

    This class was designed based on the functionality of some code written in that very way. Rather than labor for hours trying to modify this code I decided to encapsulate the operation in a class. What I ended up with is a piece of code that can be used in any project and never has to be written again. For those that haven't used classes, coding in this manner allows you to assemble larger applications out of smaller widgets. How simple or complex in function you make your classes are up to you, but you may find that keeping it simple is the best approach. The PHP language may not have all of the features of a completely object oriented language such as Java or C# but what it does have can save you hours of frustration if you let it.

    The mime_mailer class has three methods (including the constructor) that can be considered "public" and is all you need to reliably send email with attachments from a PHP application. You could easily extend this class with other classes that produce files of various types, provide shopping, checkout and payment services, process input from forms, work with the local file system and a long list of other useful functions.



     
     
    >>> More PHP Articles          >>> More By Chris Root
     

       

    PHP ARTICLES

    - Implementing the Data Mapper Design Pattern ...
    - Defining an Abstract Class with Restrictive ...
    - The Reflection API: Working with Reflected M...
    - Using Restrictive Constructors in PHP 5
    - Getting Information on a Reflected Class wit...
    - Introducing the Reflection API in PHP 5
    - Swift Mailer's Batchsend Method and Other Fe...
    - Embedding Attachments into Email Messages wi...
    - Dynamically Attaching Files with Swift Mailer
    - Using Different Paths for Attachments with S...
    - Handling Attachments and Sending HTML Email ...
    - Sending Blind Carbon Copies with Swift Mailer
    - Using Swift Mailer's Cc MIME Header
    - Using Sendmail and Mail() with Swift Mailer
    - Using Different SMTP Transports with Swift M...


    Code Analysis Tools
    Enterprise code analysis tools that deliver quality and reliable code



    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 11 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek