A MIME Mailer Class - Sending it on its way
(Page 4 of 4 )
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.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |