<?
/*
* class: mimemailer.php
* description: class for sending mails with attachments
custom: cc, bcc, charset, from adress and name
multiple attachments, plaintext and/or htmlmessage possible
* created: 04.04.2005
* last change: 05.04.2005
* author: david kargl <hide@address.com>
* copyright: david kargl, everyone free to use and change code given
* usage : $mailer = new mimemailer();
$mailer->addTo($to);
$mailer->setFrom($fromname, $fromadress);
$mailer->addBcc($bcc);
$mailer->setSubject($subject);
$mailer->addAttachment($filepath, $filetype);
$mailer->setPlaintext($plaintext);
$mailer->setHtmltext($htmltext);
// reset methods
$mailer->resetTo();
$mailer->resetCc();
$mailer->resetBcc();
$mailer->resetAttachments();
$mailer->deliver();
*/
define ("_NL_", chr(13).chr(10));
class mimemailer{
// declarate variables
var $boundary = "";
var $charset = "";
var $fromAdress = "";
var $fromName = "";
var $subject = "";
var $to = array();
var $cc = array();
var $bcc = array();
var $attachments = array();
var $plainText = "";
var $htmlText = "";
var $fullHeader = "";
var $built = false;
// construktor
function mimemailer($cs="ISO-8859-1") {
$this->boundary = uniqid(mt_rand());
$this->charset = $cs;
$built = false;
}
// funktion to set charset
function setCharSet($char) {
$this->charset = $char;
return true;
}
// funktion to check if e-mailadresse is valid
function validate_email($mailadresse) {
if(!preg_match("/[a-z0-9_-]+(\.[a-z0-9_-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})/i",$mailadresse)) return false ;
return true;
}
// set sender name and adress
function setFrom($name, $email) {
$this->fromAdress = $email;
$this->fromName = $name;
$built = false;
return true;
}
// set recipient
function addTo($to) {
if (!$this->Validate_email($to)) return false;
$this->to[] = $to;
$built = false;
return true;
}
// add cc recipient
function addCc($cc) {
if (!$this->Validate_email($cc)) return false;
$this->cc[] = $cc;
$built = false;
return true;
}
// add bcc recipient
function addBcc($bcc) {
if (!$this->Validate_email($bcc)) return false;
$this->bcc[] = $bcc;
$built = false;
return true;
}
// reset To
function resetTo() {
unset($this->to);
return true;
}
// reset Cc
function resetCc() {
unset($this->cc);
return true;
}
// reset Bcc
function resetBcc() {
unset($this->bcc);
return true;
}
// reset Attachments
function resetAttachments() {
unset($this->attachments);
return true;
}
// set subject
function setSubject($subject) {
$this->subject = $subject;
$built = false;
return true;
}
// set htmltext
function setHtmltext($html) {
$this->htmlText = $html;
$built = false;
return true;
}
// set plaintext
function setPlaintext($text) {
$this->plainText = strip_tags($text);
$built = false;
return true;
}
// function attach files to the mail
// returns false if error occurs
// (error: file does not exist, mimetype not specified)
function addAttachment($file, $type, $name="") {
// check if file exists
if(!is_file($file)) return false;
if(!$type) return false;
if(!$name) $name = basename($file);
$encoding = "base64";
$content = fread(fopen($file,"rb"), filesize($file));
$content = base64_encode($content);
$content = chunk_split($content, 72);
$attachment = new myAttachment($name, $type, $encoding, $content);
$this->attachments[] = $attachment;
return true;
}
// create a string from to-array
function _getToString() {
return trim(implode(", ", $this->to));
}
// create a string from cc-array
function _getCcString() {
return trim(implode(", ", $this->cc));
}
// create a string from to-array
function _getBccString() {
return trim(implode(", ", $this->bcc));
}
// true if there is at least one attachment
function hasAttachment() {
return (sizeof($this->attachments)>0);
}
// true if there is plaintext to send
function hasPlaintext() {
return ($this->plainText!="");
}
// true if there is htmltext to send
function hasHtmltext() {
return ($this->htmlText!="");
}
// compose the message
// returns false if error occurs
// (error: nothing to send)
function _build() {
if ((sizeof($this->attachments)==0) && ($this->plainText=="") && ($this->htmlText=="")) return false;
// default part
$defaultHeader = $this->_getDefaultHeader();
$headerEnd = $this->_getHeaderEnd();
// plaintext part
if ($this->hasPlaintext()) {
$plaintextHeader = $this->_getPlaintextHeader();
}
// htmltext part
if ($this->hasHtmltext()) {
$htmltextHeader = $this->_getHtmltextHeader();
}
// attachment part
if ($this->hasAttachment()) {
$attachmentHeader = $this->_getAttachmentHeader();
}
$this->fullHeader = $defaultHeader.$plaintextHeader.$htmltextHeader.$attachmentHeader.$headerEnd;
$built = true;
return true;
}
// returns the default header for the message
function _getDefaultHeader() {
$ret = 'To: '.$this->_getToString()._NL_;
$ret .= 'From: '.$this->fromName.' <'.$this->fromAdress.'>'._NL_;
if($this->cc) $ret .= 'Cc: '.$this->_getCcString()._NL_;
if($this->bcc) $ret .= 'Bcc: '.$this->_getBccString()._NL_;
$ret .= 'MIME-Version: 1.0'._NL_;
$ret .= 'Content-Type: multipart/mixed; boundary="'.$this->boundary.'"'._NL_._NL_;
return $ret;
}
// returns the end of the header
function _getHeaderEnd() {
$ret = '--'.$this->boundary.'--'._NL_;
return $ret;
}
// returns the header for the attachments
function _getAttachmentHeader() {
$ret = '';
foreach ($this->attachments as $value) {
$ret .= '--'.$this->boundary._NL_;
$ret .= $value->getData()._NL_._NL_;
}
return $ret;
}
// returns the header for html text part
function _getHtmltextHeader() {
$ret = '--'.$this->boundary._NL_;
$ret .= 'Content-type: text/html; charset="'.$this->charset.'";'._NL_;
$ret .= 'Content-Transfer-Encoding: 8bit;'._NL_._NL_;
$ret .= $this->htmlText._NL_._NL_;
return $ret;
}
// returns the header for plain text part
function _getPlaintextHeader() {
$ret = '--'.$this->boundary._NL_;
$ret .= 'Content-type: text/plain; charset="'.$this->charset.'";'._NL_;
$ret .= 'Content-Transfer-Encoding: 8bit;'._NL_._NL_;
$ret .= $this->plainText._NL_._NL_;
return $ret;
}
// send the message
// returns false if error occurs
// (error: "from" name and adress not specified, no subject specified, no "to" specified,
// build failed, and at last mail() failed)
function deliver() {
if ($this->fromName=="") return false;
if ($this->fromAdress=="") return false;
if ($this->subject=="") return false;
if ($this->to=="") return false;
if (!$built && !$this->_build()) return false;
if ($this->fullHeader=="") return false;
return mail("", $this->subject, "", $this->fullHeader);
}
}
class myAttachment {
// declarate variables
var $filename = "";
var $type = "";
var $encoding = "";
var $content = "";
// construktor
function myAttachment($fn ='', $t='application/octet-stream', $e='base64', $c) {
$this->filename = $fn;
$this->type = $t;
$this->encoding = $e;
$this->content = $c;
}
// constructs a header for the attachment
function getData() {
$ret = 'Content-type: '.$this->type.'; name="'.$this->filename.'"'._NL_;
$ret .= 'Content-Transfer-Encoding: '.$this->encoding._NL_;
$ret .= 'Content-Disposition: attachment; filename="'.$this->filename.'"'._NL_._NL_;
$ret .= $this->content._NL_;
return $ret;
}
}
?>