Home arrow PHP arrow Page 4 - A MIME Mailer Class

Sending it on its way - PHP

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.

TABLE OF CONTENTS:
  1. A MIME Mailer Class
  2. You've Got Mail
  3. Adding an Attachment
  4. Sending it on its way
By: Chris Root
Rating: starstarstarstarstar / 20
January 04, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The final two methods send the mail. If the proper headers and body information is set, the mail message is assembled from all the information previously gathered. The "assemble()" method is called to assemble the attachment data and put it together with the body of the message.

  function send()
  {
   if($this->header_set && $this->body_set)
   {
    $message = $this->assemble();
    $headers = $this->headers;
    $to_address = $this->to;
    $mail_subject = $this->subject;
    if(mail($to_address,$mail_subject,$message,$headers))
    {
     return true;
    }
    else
    {
     return false;
    }
   }
  }
  
  function assemble()
  {
   $str = $this->body;
   if(count($this->files) > 0)
   {
    $str = implode($this->files);
    $str .= "--MIME_BOUNDRY--\n";
   }
   return $str;
  }
}
?>

Formatting can be important to the success of the mailing. Attachments can fail to appear or be corrupted if the output is not correct. If you have any trouble while modifying or adding to this class, consult documentation about mime formatting to determine what might have gone wrong.

The Code in Use

Using the mime_mailer class is a simple matter. The first line you will need is to include the file in your application. You then initialize an instance of the class, add your attachments and send the mail. Use as many instances as you need. The to, from, subject and body information can come from any source in your application you need it to.

include("class/mime_mailer.php");
$to = "
recipeint@someserver.com";
$from = "
mesilly@thisserver.com";
$subject = "Season's Greetings";
$body = "Here's some xmas cards just for you!";
$mailer = new mime_mailer($to,$from,$subject,$body);
$mailer->add_attachment("cards/xmascard.pdf");
$mailer->add_attachment("cards/xmascard2.gif");
$mailer->send();

No Stamps to Lick

There are quite a few applications where this basic functionality would be indispensable. Wrapping it all up into just a few lines can save a lot of time and effort. Experiment with the code, use it in your applications, and if you have any modifications that you think will be useful to others, please share them, and please email responsibly.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: