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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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? 
PHP

A MIME Mailer Class
By: Chris Root
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · Firstly, thanks for this quick tutorial.I can't see the "body" part of the...
       · Try putting the body of the message at the beginning of the body string. This may...
       · I think i've solved the problem. In function assamble(), I put a "." before "=" in ...
       · Hello,This is a very good article you have done!A question about the mail...
       · As I understand it, The string does have to be unique, It cannot match anything that...
       · I am still using PHP 4.2.1. Will this class still work, or is only for PHP 5?
       · I developed it in PHP 4.x. It should work just fine in either.
       · I've got this working the first time up, but the email I get is encoded and no...
       · I am using MS Outlook...does that matter. my attachments are encoded so it all...
       · Thank you so much for this tutorial, it really helped me alot...i just need a...
     

       

    PHP ARTICLES

    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP
    - PHP Networking
    - Validating Web Forms with the Code Igniter P...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT