Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class EmailHandler
- {
- private $recipients = array();
- private $subject = '';
- private $htmlmessage = '';
- private $textmessage = '';
- private $headers = array();
- private $attachments = array();
- private $tokens = array();
- private $prep_data = array();
- function __construct($email)
- {
- //Set the "From" address
- $this->headers['From'] = $email;
- }
- /************\
- * Recipients *
- \************/
- function addRecipient($email)
- {
- //Add a recipient
- $this->recipients[] = $email;
- return true;
- }
- function removeRecipient($email)
- {
- //Remove a recipient
- foreach ($this->recipients as $index=>$val)
- {
- if ($val == $email)
- {
- unset($this->recipients[$index]);
- return true;
- }
- }
- return false;
- }
- function getRecipients()
- {
- //Return the list of recipients
- return $this->recipients;
- }
- /*********\
- * Headers *
- \*********/
- function setHeader($header, $value)
- {
- //Set an Email Header
- $this->headers[$header] = $value;
- return true;
- }
- function removeHeader($header)
- {
- //Remove an Email Header
- unset($this->headers[$header]);
- return true;
- }
- function getHeaders()
- {
- //Return the list of email headers
- return $this->headers;
- }
- /*********\
- * Message *
- \*********/
- function setHTML($message)
- {
- //Set the HTML verison of the message
- $this->htmlmessage = $message;
- return true;
- }
- function setText($message)
- {
- //Set the plaintext version of the message
- $this->textmessage = $message;
- return true;
- }
- function setSubject($subject)
- {
- //Set the message subject
- $this->subject = $subject;
- return true;
- }
- function getHTML()
- {
- //Get the HTML version of the message
- return $this->htmlmessage;
- }
- function getText()
- {
- //Get the plaintext version of the message
- return $this->textmessage;
- }
- function getSubject()
- {
- //Get the message subject
- return $this->subject;
- }
- /********\
- * Tokens *
- \********/
- function setToken($tag, $value)
- {
- //Set a token (TAG will replace {TAG} in the message bodies and subject)
- //TODO: Implement this feature
- $this->tokens[$tag] = $value;
- return true;
- }
- function removeToken($tag)
- {
- //Removes a token
- unset($this->tokens[$tag]);
- return true;
- }
- function getTokens()
- {
- //Returns the list of tokens
- return $this->tokens;
- }
- /*************\
- * Attachments *
- \*************/
- function addAttachment($filepath, $filename)
- {
- //Attaches a file if it exists
- if($contents = file_get_contents($filepath))
- {
- $this->attachments[$filename]['contents'] = $contents;
- $this->attachments[$filename]['type'] = mime_content_type($filepath);
- return true;
- } else {
- return false;
- }
- }
- function removeAttachment($filename)
- {
- //Removes an attached file
- unset($this->attachments[$filename]);
- return true;
- }
- /************\
- * Processing *
- \************/
- function prepData()
- {
- //Processes and prepares data for sending/queueing
- $random_hash = md5(date('r', time()));
- $this->headers['Content-Type'] = 'multipart/mixed; boundary="PHP-mixed-'.$random_hash.'"';
- $message = '--PHP-mixed-'.$random_hash.''."\r\n".'Content-Type: multipart/alternative; boundary="PHP-alt-'.$random_hash.'"'."\r\n\r\n";
- if($this->textmessage != '')
- {
- $message .= '--PHP-alt-'.$random_hash.''."\r\n".'Content-Type: text/plain; charset="iso-8859-1"'."\r\n".'Content-Transfer-Encoding: 7bit'."\r\n\r\n" . $this->textmessage . "\r\n\r\n";
- }
- if($this->htmlmessage != '')
- {
- $message .= '--PHP-alt-'.$random_hash.''."\r\n".'Content-Type: text/html; charset="iso-8859-1"'."\r\n".'Content-Transfer-Encoding: 7bit'."\r\n\r\n" . $this->htmlmessage . "\r\n\r\n";
- }
- $message .= '--PHP-alt-'.$random_hash.'--'."\r\n\r\n";
- if(!empty($this->attachments))
- {
- foreach ($this->attachments as $fname=>$fcontents)
- {
- $contents = chunk_split(base64_encode($fcontents['contents']));
- $filetype = $fcontents['type'];
- $message .= '--PHP-mixed-'.$random_hash.''."\r\n".'Content-Type: '.$filetype.'; name="'.$fname.'"'."\r\n".'Content-Transfer-Encoding: base64'."\r\n".'Content-Disposition: attachment'."\r\n".''."\r\n".''.$contents ."\r\n\r\n";
- }
- }
- $message .= '--PHP-mixed-'.$random_hash.'--';
- $headerlist = '';
- foreach($this->headers as $key=>$val)
- {
- $headerlist .= $key . ": " . $val . "\r\n";
- }
- $recipientlist = implode(', ', $this->recipients);
- $this->prep_data['headers'] = $headerlist;
- $this->prep_data['message'] = $message;
- $this->prep_data['subject'] = $this->subject;
- $this->prep_data['recipients'] = $recipientlist;
- return true;
- }
- function queue()
- {
- //Add this email to the queue
- global $db;
- $this->prepData();
- $db->query("INSERT INTO mail.mail_queue
- VALUES (
- NULL,
- '" . mysql_real_escape_string($this->prep_data['recipients']) . "',
- '" . mysql_real_escape_string($this->prep_data['subject']) . "',
- '" . mysql_real_escape_string($this->prep_data['message']) . "',
- '" . mysql_real_escape_string($this->prep_data['headers']) . "',
- 0
- )
- ");
- return true;
- }
- function send()
- {
- //Send email immediately
- $this->prepData();
- mail($this->prep_data['recipients'], $this->prep_data['subject'], $this->prep_data['message'], $this->prep_data['headers']);
- return true;
- }
- function debug()
- {
- //Spit out the prepped data for debugging
- $this->prepData();
- return $this->prep_data;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement