Advertisement
Guest User

php Mailer class

a guest
Apr 12th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.53 KB | None | 0 0
  1. class EmailHandler
  2. {
  3.     private $recipients = array();
  4.     private $subject = '';
  5.     private $htmlmessage = '';
  6.     private $textmessage = '';
  7.     private $headers = array();
  8.     private $attachments = array();
  9.     private $tokens = array();
  10.     private $prep_data = array();
  11.    
  12.     function __construct($email)
  13.     {
  14.         //Set the "From" address
  15.         $this->headers['From'] = $email;
  16.     }
  17.    
  18.     /************\
  19.     * Recipients *
  20.     \************/
  21.     function addRecipient($email)
  22.     {
  23.         //Add a recipient
  24.         $this->recipients[] = $email;
  25.         return true;   
  26.     }
  27.    
  28.     function removeRecipient($email)
  29.     {
  30.         //Remove a recipient
  31.         foreach ($this->recipients as $index=>$val)
  32.         {
  33.             if ($val == $email)
  34.             {
  35.                     unset($this->recipients[$index]);
  36.                     return true;
  37.             }
  38.         }
  39.         return false;
  40.     }
  41.    
  42.     function getRecipients()
  43.     {
  44.         //Return the list of recipients
  45.         return $this->recipients;
  46.     }
  47.    
  48.    
  49.     /*********\
  50.     * Headers *
  51.     \*********/
  52.     function setHeader($header, $value)
  53.     {
  54.         //Set an Email Header
  55.         $this->headers[$header] = $value;
  56.         return true;   
  57.     }
  58.    
  59.     function removeHeader($header)
  60.     {
  61.         //Remove an Email Header
  62.         unset($this->headers[$header]);
  63.         return true;   
  64.     }
  65.    
  66.     function getHeaders()
  67.     {
  68.         //Return the list of email headers
  69.         return $this->headers; 
  70.     }
  71.    
  72.     /*********\
  73.     * Message *
  74.     \*********/
  75.     function setHTML($message)
  76.     {
  77.         //Set the HTML verison of the message
  78.         $this->htmlmessage = $message;
  79.         return true;   
  80.     }
  81.    
  82.     function setText($message)
  83.     {
  84.         //Set the plaintext version of the message
  85.         $this->textmessage = $message;
  86.         return true;
  87.     }
  88.    
  89.     function setSubject($subject)
  90.     {
  91.         //Set the message subject
  92.         $this->subject = $subject;
  93.         return true;
  94.     }
  95.    
  96.     function getHTML()
  97.     {
  98.         //Get the HTML version of the message
  99.         return $this->htmlmessage; 
  100.     }
  101.    
  102.     function getText()
  103.     {
  104.         //Get the plaintext version of the message
  105.         return $this->textmessage; 
  106.     }
  107.    
  108.     function getSubject()
  109.     {
  110.         //Get the message subject
  111.         return $this->subject;
  112.     }
  113.    
  114.     /********\
  115.     * Tokens *
  116.     \********/
  117.     function setToken($tag, $value)
  118.     {
  119.         //Set a token (TAG will replace {TAG} in the message bodies and subject)
  120.        
  121.         //TODO: Implement this feature
  122.         $this->tokens[$tag] = $value;
  123.         return true;
  124.     }
  125.    
  126.     function removeToken($tag)
  127.     {
  128.         //Removes a token
  129.         unset($this->tokens[$tag]);
  130.         return true;
  131.     }
  132.    
  133.     function getTokens()
  134.     {
  135.         //Returns the list of tokens
  136.         return $this->tokens;
  137.     }
  138.    
  139.     /*************\
  140.     * Attachments *
  141.     \*************/
  142.     function addAttachment($filepath, $filename)
  143.     {
  144.         //Attaches a file if it exists
  145.         if($contents = file_get_contents($filepath))
  146.         {
  147.             $this->attachments[$filename]['contents'] = $contents;
  148.             $this->attachments[$filename]['type'] = mime_content_type($filepath);
  149.             return true;
  150.         } else {
  151.             return false;
  152.         }
  153.     }
  154.    
  155.     function removeAttachment($filename)
  156.     {
  157.         //Removes an attached file
  158.         unset($this->attachments[$filename]);
  159.         return true;
  160.     }
  161.    
  162.     /************\
  163.     * Processing *
  164.     \************/
  165.     function prepData()
  166.     {
  167.         //Processes and prepares data for sending/queueing
  168.        
  169.         $random_hash = md5(date('r', time()));
  170.         $this->headers['Content-Type'] = 'multipart/mixed; boundary="PHP-mixed-'.$random_hash.'"';
  171.        
  172.         $message = '--PHP-mixed-'.$random_hash.''."\r\n".'Content-Type: multipart/alternative; boundary="PHP-alt-'.$random_hash.'"'."\r\n\r\n";
  173.         if($this->textmessage != '')
  174.         {
  175.             $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";
  176.         }
  177.        
  178.         if($this->htmlmessage != '')
  179.         {
  180.             $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";
  181.         }
  182.        
  183.         $message .= '--PHP-alt-'.$random_hash.'--'."\r\n\r\n";
  184.        
  185.         if(!empty($this->attachments))
  186.         {
  187.             foreach ($this->attachments as $fname=>$fcontents)
  188.             {
  189.                 $contents = chunk_split(base64_encode($fcontents['contents']));
  190.                 $filetype = $fcontents['type'];
  191.                
  192.                 $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";
  193.             }
  194.         }
  195.        
  196.         $message .= '--PHP-mixed-'.$random_hash.'--';
  197.        
  198.         $headerlist = '';
  199.         foreach($this->headers as $key=>$val)
  200.         {
  201.             $headerlist .= $key . ": " . $val . "\r\n";
  202.         }
  203.        
  204.         $recipientlist = implode(', ', $this->recipients);
  205.        
  206.         $this->prep_data['headers'] = $headerlist;
  207.         $this->prep_data['message'] = $message;
  208.         $this->prep_data['subject'] = $this->subject;
  209.         $this->prep_data['recipients'] = $recipientlist;
  210.         return true;
  211.     }
  212.    
  213.     function queue()
  214.     {
  215.         //Add this email to the queue
  216.         global $db;
  217.         $this->prepData();
  218.         $db->query("INSERT INTO mail.mail_queue
  219.             VALUES (
  220.                 NULL,
  221.                 '" . mysql_real_escape_string($this->prep_data['recipients']) . "',
  222.                 '" . mysql_real_escape_string($this->prep_data['subject']) . "',
  223.                 '" . mysql_real_escape_string($this->prep_data['message']) . "',
  224.                 '" . mysql_real_escape_string($this->prep_data['headers']) . "',
  225.                 0
  226.             )
  227.         ");
  228.         return true;  
  229.     }
  230.    
  231.     function send()
  232.     {
  233.         //Send email immediately
  234.         $this->prepData();
  235.         mail($this->prep_data['recipients'], $this->prep_data['subject'], $this->prep_data['message'], $this->prep_data['headers']);
  236.         return true;
  237.     }
  238.    
  239.     function debug()
  240.     {
  241.         //Spit out the prepped data for debugging
  242.         $this->prepData();
  243.         return $this->prep_data;
  244.     }
  245.    
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement