PHPBuilder - Sending MIME Email With PHP Page 3

RSS Twitter

Sending MIME Email With PHP - Page 3

by: Kartic Krishnamurthy
August 7, 2000

The MIME Class

With these basics in mind, let us build and implement a MIME mail class in PHP. In our PHP arsenal, we already have the necessary tools to do the encoding!
The MIME class must have the ability to:
  1. Add attachment(s).
  2. Perform encoding on the attached data, as per the individual request.
  3. Build the MIME parts/headers.
  4. Generate a complete email with the MIME parts/headers included.
  5. Return the completed mail as string.
  6. Send it using the local mailer (or optionally call an SMTP mailer).
The class is called MIME_mail. We will discuss the class methods to bridge the gap between the theory and practice. (Reading suggestion: Luis Argerich's OO Programming in PHP). Most of the comments have been stripped off for readability. Some method and class member variables are for internal access only and have been indicated so in the commentary below (and in the original class file).


class MIME_mail {
var $to;
$headers "";

$base64_func'';    // if !specified use PHP's base64  
var $qp_func '';        // None at this time

var $mailer ""
// Set this to the name of a valid mail object

These are public access variables (i.e., you can directly manipulate this in your script). Most of these variables are self-explanatory. $headers contains the optional headers you might want to send to the mailer (more on this later). $errstr is a variable that contains a readable error string that can be used in the calling script.
$base64_func and $qp_func are "function handlers" that the users can customize. By default, they are set to null strings. For $base64_func, a null string means that we will be using the PHP built-in base64_encode() function...(yeah! Neat isn't it!). Quoted Printable is accessible by the variable$qp_func. There is no native quoted-printable encode function in PHP (however, imap enabled installations can use imap_qprint()). We will not be discussing the quoted-printable encoding method in this article.


var $mimeparts = array(); 

$mimeparts is an internal array that contains the individual MIME-compliant parts of the email message. Please desist from manipulating this and other private methods/variables outside this (or other inherited) class.


// Constructor.
function MIME_mail($from=""$to=""$subject=""$body=""$headers "") {
$this->to $to;
$this->from $from;
$this->subject $subject;
$this->body $body;
    if (
is_array($headers)) {
        if (
    if (
$from) {
$headers preg_replace("!(from:\ ?.+?[\r\n]?\b)!i"''$headers);
$this->headers chop($headers);
$this->mimeparts[] = "" ;    //Bump up location 0;

We have the constructor for our object and this takes as arguments the "from" and "to" email addresses, subject and body of the email and headers. For the body part, you can give the regular email you would probably type in. The last argument is optional (user-defined) headers. E.g., X-Mailer: MYMailer_1.0. Please note that $headers can either be an array of different headers you may want to send to the mailer or just a single container for one specific header. You can not send a From: header in the $headers argument; if it is found, that part is automatically stripped. You can use the multiple headers as follows: array("X-Mailer: MYMailer_1.0", "X-Organization:").
$mimeparts is created with an empty slot 1 (index 0), the rationale for which we will see later.

« Previous Page
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Kartic Krishnamurthy



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