As I mentioned earlier, it’d be pretty convenient to slightly modify the existing signature of the “Mailer” class in order to save all of the MIME types allowed by it in a simple array. In doing so, not only will its private “getMimeType()” method be much easier to code, but the incorporation of other supported MIME types will merely be a matter of adding more elements to the mentioned array. Of course, all of this theory must be translated into concrete PHP code. Therefore, have a look at the improved definition of the “Mailer” class, which includes all of the changes explained earlier: class Mailer{ var $sender; var $recipient; var $subject; var $headers=array(); var $mimeTypes=array(); var $attachments=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']='PHP5'; $this->headers['X-Sender']=$this->sender; $this->headers['X-Priority']='3'; // define some default MIME types $this->mimeTypes['image/jpeg']='jpg'; $this->mimeTypes['image/jpg']='jpg'; $this->mimeTypes['image/gif']='gif'; $this->mimeTypes['text/plain']='txt'; $this->mimeTypes['text/html']='htm'; $this->mimeTypes['text/xml']='xml'; $this->mimeTypes['application/pdf']='pdf'; } // create text part of the message function buildTextPart(){ return "--MIME_BOUNDRY\nContent-Type: text/plain; charset=iso-8859-1\nContent-Transfer-Encoding: quoted-printable\n\n\n".$this->message."\n\n"; } // create attachments part of the message function buildAttachmentPart(){ if(count($this->attachments)>0){ $attachmentPart=''; foreach($this->attachments as $attachment){ $fileStr=file_get_contents($attachment); $fileStr=chunk_split(base64_encode($fileStr)); $attachmentPart.="--MIME_BOUNDRY\nContent-Type: ".$this->getMimeType($attachment)."; name="".basename($attachment).""\nContent-disposition: attachment\nContent-Transfer-Encoding: base64\n\n".$fileStr."\n\n"; } return $attachmentPart; } } // create message MIME headers function buildHeaders(){ foreach($this->headers as $name=>$value){ $headers[]=$name.': '.$value; } return implode("\n",$headers)."\nThis is a multi-part message in MIME format.\n"; } // add new MIME header function addHeader($name,$value){ $this->headers[$name]=$value; } // add new attachment function addAttachment($attachment){ if(!file_exists($attachment)){ trigger_error('Invalid attachment.',E_USER_ERROR); } $this->attachments[]=$attachment; } // get MIME Type of attachment function getMimeType($attachment){ $attachment=explode('.',basename($attachment)); if(!$mimeType=array_search(strtolower($attachment[count($attachment)-1]),$this->mimeTypes)){ trigger_error('MIME Type not found.',E_USER_ERROR); } return $mimeType; } // send email function send(){ $to=$this->recipient; $subject=$this->subject; $headers=$this->buildHeaders(); $message=$this->buildTextPart().$this->buildAttachmentPart()."--MIME_BOUNDRY--\n"; if(!mail($to,$subject,$message,$headers)){ trigger_error('Error sendind email.',E_USER_ERROR); } } } Well, at this point you’ll have to agree that the business logic has been dramatically improved. And the reason is that now the class stores all the allowed MIME types as an array of properties, called “$mimeTypes.” The immediate advantages are: if a new MIME type needs to be supported by the class, the process is reduced to adding a new element to the corresponding “$mimeTypes” array. And the implementation of the “getMimeType()” private method is much simpler and tighter. So far, so good. At this point, I've showed you how to improve the initial signature of this MIME mailer class by way of a simple property. Though, I’m sure that after introducing this minor modification you'll want to see how it functions. In the final section of this tutorial, I’m going to setup an illustrative example for you that shows the class in action. Of course, to see how the example in question will be created, you’ll have to click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|