Before I get my hands dirty by coding this extensible MIME mailer class with PHP, I'd like to explain the functionality that I plan to implement behind its API. The class will be comprised of a few straightforward methods that will be useful for sending MIME-compliant email messages. It will also incorporate the capacity to handle simple attachments. Once the class has been completed, you'll be able to incorporate additional methods into it, according to your personal requirements. Having explained how this MIME mailer class will work, I'll start by defining its principal structure and implementing its constructor. Here's the first incarnation of the class in question: class Mailer{ var $sender; var $recipient; var $subject; var $headers=array(); function Mailer($sender,$recipient,$subject,$message){ // validate incoming parameters if(!preg_match("/^.+@.+$/",$sender)){ trigger_error('Invalid value for email sender.'); } if(!preg_match("/^.+@.+$/",$recipient)){ trigger_error('Invalid value for email recipient.'); } if(!$subject||strlen($subject)>255){ trigger_error('Invalid length for email subject.'); } if(!$message){ trigger_error('Invalid value for email message.'); } $this->sender=$sender; $this->recipient=$recipient; $this->subject=$subject; $this->message=$message; // define some default MIME headers $this->headers['MIME-Version']='1.0'; $this->headers['Content-Type']='multipart/mixed;boundary="MIME_BOUNDRY"'; $this->headers['From']='<'.$this->sender.'>'; $this->headers['Return-Path']='<'.$this->sender.'>'; $this->headers['Reply-To']=$this->sender; $this->headers['X-Mailer']='PHP 4/5'; $this->headers['X-Sender']=$this->sender; $this->headers['X-Priority']='3'; } // create text part of the message function buildTextPart(){ // code to send text/plain messages goes here } // create message MIME headers function buildHeaders(){ // code to build MIME headers goes here } // add new MIME header function addHeader($name,$value){ // code to add MIME headers goes here } // send email function send(){ // code to send MIME email goes here } } As you can see, the structure of the above "Mailer" class is quite simple to grasp and its methods are extremely intuitive. In this first step, I only implemented the corresponding constructor, but in the upcoming sections, I'll be defining the others also. For now, pay close attention to the constructor. It accepts a few basic email-related parameters, such as the sender, the recipient, the message's subject, and finally, the text of the message itself. The method performs a basic validation on these input arguments, and if they're considered valid, they are designated class properties. The constructor finishes its execution by defining some default MIME headers, which will be added later on when actually sending the MIME-based email message. The rest of the methods defined by the "Mailer" class speak for themselves, since they've been named according to the task they will perform. However, they haven't been implemented concretely yet. In the upcoming section of this tutorial, I'll be doing just that. This will complete the definition of this MIME mailer class. To see how the remaining methods of the class will be implemented, please click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|