#native_company# #native_desc#

Sending MIME Email With PHP Page 4

By Kartic Krishnamurthy
on August 7, 2000

Heart of it All: The Methods

We modularize the creation of the MIME message headers, MIME part headers and the creation of the final email message into separate methods. The implementation of the methods follows directly from the MIME basics we encountered earlier.


function attach($data$description ""$contenttype OCTET$encoding BASE64$disp '') {

    if (empty(


    if (
trim($contenttype) == ''

$contenttype OCTET ;

    if (
trim($encoding) == ''

$encoding BASE64;

    if (
$encoding == BIT7

$emsg $data;

    elseif (
$encoding == QP

$emsg = $$this->qp_func($data);

    elseif (
$encoding == BASE64) {

        if (!
$this->base64_func)     # Check if there is user-defined function

$emsg base64_encode($data);


$emsg = $$this->base64_func($data);


$emsg chunk_split($emsg);

//Check if content-type is text/plain and if charset is not specified append default CHARSET

if (preg_match("!^".TEXT."!i"$contenttype) && !preg_match("!;charset=!i"$contenttype)) 

$contenttype .= ";rntcharset=".CHARSET ;

$msg sprintf("Content-Type: %sContent-Transfer-Encoding: %s%s%s%s",



$description) && (BODY != $description))?"Content-Description: $description".CRLF:""),

$disp?"Content-Disposition: $disp".CRLF:""),


BODY==$description$this->mimeparts[0] = $msg$this->mimeparts[] = $msg ;




Let us go over this method closely (as we will most of the other methods):
  1. The method takes as arguments, the:
  • actual data to attach ($data).
  • description the data corresponding to the Content-Description header ($description).
  • content-type of the data as will be used in the Content-Type header ($contenttype).
  • encoding to be used and the value of Content-Transfer-Encoding ($encoding).
  • disposition value that can be used in the Content-Disposition header $disp can be INLINE or ATTACH, both of which are constants.
  • Values such as BASE64, TEXT, etc., are defined as constants in the accompanying .def file.
  • We use the $encoding value to decide what kind of encoding we need to do on the data. The valid values are BIT7 (for 7bit), QP or BASE64. This method also checks to see if the user wants to use his/her own BASE64 or QP functions. As of this writing, only BIT7 and BASE64 are implemented in our class, however, you can always pass your own quoted-printable function to use, via the $qp_func class variable discussed earlier.
  • After the encoding process, you will notice the use of chunk_split() on the encoded information. This function splits the string into smaller chunks for an optional length. Since we do not specify the length, the default length of 76 is used. This is very much in compliance with emailing conventions.
  • Next, if $contenttype argument contains text/plain, a “charset=” parameter value must be given. The value is defined in the constant CHARSET and is us-ascii. Note that when headers are passed with parameter values, there must be a delimiting semi-colon (;) between the header and parameter. E.g., Content-Type: text/plain; charset=us-ascii.
  • The other MIME part headers are created if their respective values are passed to this method. We do not want to have a Content-Description header without a description, after all. After creating the headers, we append the encoded data the part information. (Inspect the sprintf() statement in the method).
  • Also, note that we use a special description field called BODY (again a constant). This is something we are using in our class implementation. If the description field is same as BODY, we use the first slot (index 0) of $mimeheaders array to attach. Read on for more on this.
  • attach() returns the current size of the $mimeparts array for the calling script’s reference. That way one knows in which slot an attachment “X” is present (actually return value less one is the actual index in the array)
  • Note that all headers must be terminated with a CRLF (rn) sequence.
  • Next, we look at the method fattach(), fattach() is similar to attach(), but it takes a filename as its first argument (instead if $data of attach()). This method is just a wrapper so that the caller can invoke fattach with the filename. fattach() will then read in the file and call attach() to actually attach the data. This method returns a 0 on failure, an explanation for which can be found in the $errstr variable or on success, returns the slot number (less one) of the file attachment in the $mimeparts array.
    We now have developed the capability to attach data, encode them and stow away the individual MIME parts in a private array. The jobs remaining to be done are:
    • to complete the MIME parts
    • to create the message headers with the MIME message headers included, the message original headers (as in To:, From: etc.) and include any user-defined headers.
    • to append the completed MIME parts to the headers so that a complete email packet is generated.