Advertisement
Draven

EmailHandler Class

Aug 25th, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.14 KB | None | 0 0
  1.     public function caughtEmail($full_email)
  2.     {
  3.         global $database;
  4.         global $member;
  5.         global $EmailClass;
  6.  
  7.         # Include email parser  
  8.         require_once('rfc822_addresses.class.php');
  9.         require_once('mime_parser.class.php');
  10.  
  11.         # Create the email parser class
  12.         $mime = new mime_parser_class;
  13.        
  14.         $mime->ignore_syntax_errors = 1;
  15.         $parameters=array(
  16.             'Data'=>$full_email,
  17.         );
  18.  
  19.         $mime->Decode($parameters, $decoded);
  20.  
  21.         # Get the name and email of the sender
  22.         $fromName = $decoded[0]['ExtractedAddresses']['from:'][0]['name'];
  23.         $fromEmail = $decoded[0]['ExtractedAddresses']['from:'][0]['address'];
  24.  
  25.         # If If there is no fromName then fromName becomes fromEmail.
  26.         if(!isset($fromName) || empty($fromName))
  27.         {
  28.             $fromName1 = explode('@', $fromEmail);
  29.             $fromName = $fromName1[0];
  30.         }
  31.  
  32.         # Get the name and email of the recipient
  33.         $toEmail = $decoded[0]['ExtractedAddresses']['to:'][0]['address'];
  34.         $toName = $decoded[0]['ExtractedAddresses']['to:'][0]['name'];
  35.         if(!isset($toName)) $toName = "";
  36.  
  37.         # Get the subject
  38.         $subject = $decoded[0]['Headers']['subject:'];
  39.        
  40.         # Stop script if it's spam
  41.         if(strpos($subject, '***SPAM***') !== FALSE)
  42.         {
  43.             exit;
  44.         }
  45.  
  46.         # If no subject then make it (No Subject)
  47.         if($subject=="") $subject = "(No Subject)";
  48.  
  49.         # Retrieve Ticket ID
  50.         preg_match('/\[#(\d+)\]/U', $subject, $subject_ticket_id);
  51.  
  52.         $removeChars = array('<','>');
  53.  
  54.         # Get the message id
  55.         $messageID = str_replace($removeChars,'',$decoded[0]['Headers']['message-id:']);
  56.  
  57.         # Get the reply id
  58.         $replyToID = str_replace($removeChars,'',$decoded[0]['Headers']['in-reply-to:']);
  59. nline
  60.         # Get the message body
  61.         if(substr($decoded[0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Body']))
  62.         {
  63.             $body = $decoded[0]['Body'];
  64.  
  65.         }
  66.         elseif(substr($decoded[0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Parts'][0]['Body']))
  67.         {
  68.             $body = $decoded[0]['Parts'][0]['Body'];
  69.         }
  70.         elseif(substr($decoded[0]['Parts'][0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Parts'][0]['Parts'][0]['Body']))
  71.         {
  72.             $body = $decoded[0]['Parts'][0]['Parts'][0]['Body'];
  73.         }
  74.         preg_match('/Email ID: (?P<body_ticket_id>\w+)/', $body, $body_ticket_id);
  75.  
  76.         $new_body = $body;
  77.  
  78.         # Send email to users with notification
  79.         $database->query('SELECT id, full_name, email, email_notify FROM users_table WHERE email_notify = 1', array());
  80.         $users_notify = $database->statement->fetchAll(PDO::FETCH_OBJ);
  81.         if($database->count() > 0)
  82.         {
  83.             foreach($users_notify as $row)
  84.             {
  85.                 $notify_subject = $EmailClass->site_name." - New Email";
  86.                 $notify_body_content = $row->full_name.",\r\n\r\nThere is a new email on the Hub";
  87.                 $EmailClass->sendEmail($row->email, $notify_subject, $notify_body_content, $row->full_name);
  88.             }
  89.         }
  90.  
  91.         # If there is no ticket ID in the subject line or body then create new ticket.
  92.         if((!isset($subject_ticket_id)) && (!isset($body_ticket_id)))
  93.         {
  94.             $new_email = true;
  95.         }
  96.         else {
  97.             if((isset($subject_ticket_id[1]) && (isset($body_ticket_id['body_ticket_id']))) && ($subject_ticket_id[1] == $body_ticket_id['body_ticket_id']))
  98.             {
  99.                 $ticket_id = $subject_ticket_id[1];
  100.             }
  101.             elseif(isset($subject_ticket_id[1]))
  102.             {
  103.                 $ticket_id = $subject_ticket_id[1];
  104.             }
  105.             elseif(isset($body_ticket_id['body_ticket_id']))
  106.             {
  107.                 $ticket_id = $body_ticket_id['body_ticket_id'];
  108.             }
  109.             if(isset($ticket_id))
  110.             {
  111.                 $email_id = $this->getEmailID($ticket_id);
  112.                 if($this->compareTicket($email_id, $ticket_id, $fromEmail) == true)
  113.                 {
  114.                     $new_email = false;
  115.  
  116.                     $check_status = $this->getEmailStatus($email_id);
  117.  
  118.                     $last_email_id = $email_id;
  119.  
  120.                     if($check_status=="Closed")
  121.                     {
  122.                         $database->query('UPDATE emails_table SET status = :status WHERE id = :id', array(':status' => 2, ':id' => $email_id));
  123.                     }
  124.                     $database->query('UPDATE emails_table SET last_replier = :lastreplier, updated = NOW() WHERE id = :id', array(':lastreplier' => $fromName, ':id' => $email_id));
  125.                     $database->query('INSERT INTO email_message (email_id, ticket_id, message, created) VALUES (?, ?, ?, NOW())', array($email_id, $ticket_id, $new_body));
  126.  
  127.                     $last_email_message_id = $database->lastInsertId();
  128.                 }
  129.                 else {
  130.                     # The ticket ID doesn't match the sender email so create new ticket.
  131.                     $new_email = true;
  132.                 }
  133.             }
  134.             else {
  135.                 # $ticket_id is not set so create new ticket.
  136.                 $new_email = true;
  137.             }
  138.         }
  139.  
  140.         # If $new_email is true then create new ticket.
  141.         if($new_email == true)
  142.         {
  143.             $ticket_id = $this->genRandID(6);
  144.             $category1 = explode('@',$toEmail);
  145.             $category = ucwords(str_replace('_', ' ', $category1[0]));
  146.  
  147.             $database->query('INSERT INTO emails_table (ticket_id, category, recipient_name, recipient_email, sender_name, sender_email, subject, message, last_replier, created, updated) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())', array($ticket_id, $category, $toName, $toEmail, $fromName, $fromEmail, $subject, $new_body, $fromName));
  148.  
  149.             # Get last email insert ID
  150.             $last_email_id = $database->lastInsertId();
  151.  
  152.             $subject = "[#".$ticket_id."] ".$subject;
  153.             $body_content = $fromName.",\r\n\r\nThank you for contacting us. This is an automated response confirming your email. We will get back to you shortly.\r\nWhen replying, please make sure that the email ID is kept in the subject line to ensure that your replies are tracked appropriately.\r\n\r\nEmail ID: ".$ticket_id."\r\nSubject: ".$subject."\r\nDepartment: ".$category."\r\nStatus: Open";
  154.             $EmailClass->sendEmail($fromEmail, $subject, $body_content, $fromName, $toEmail);
  155.         }
  156.  
  157.         # Loop through email parts
  158.         foreach($decoded[0]['Parts'] as $part)
  159.         {
  160.             # Check for attachments
  161.             if($part['FileDisposition'] == 'attachment')
  162.             {
  163.                 # Format file name (change spaces to underscore then remove anything that isn't a letter, number or underscore)
  164.                 $filename = preg_replace('/[^0-9,a-z,\.,_]*/i', '', str_replace(' ', '_', $part['FileName']));
  165.  
  166.                 if($new_email == true)
  167.                 {
  168.                     $database->query('INSERT INTO email_attachments (email_id, file_name, created) VALUES (?, ?, NOW())', array($last_email_id, $filename));
  169.                 }
  170.                 else {
  171.                     $database->query('INSERT INTO email_attachments (email_id, reply_id, file_name, created) VALUES (?, ?, ?, NOW())', array($email_id, $last_email_message_id, $filename));
  172.                 }
  173.  
  174.                 $last_attachment_id = $database->lastInsertId();
  175.  
  176.                 # Write the data to the file
  177.                 if(!is_dir(ATTACHMENTS.$fromEmail.DS.$last_email_id.DS.$last_attachment_id))
  178.                 {
  179.                     mkdir(ATTACHMENTS.$fromEmail.DS.$last_email_id.DS.$last_attachment_id.DS, 0755, true);
  180.                 }
  181.                 $fp = fopen(ATTACHMENTS.$fromEmail.DS.$last_email_id.DS.$last_attachment_id.DS.$filename, 'w');
  182.                 $written = fwrite($fp, $part['Body']);
  183.                 fclose($fp);
  184.  
  185.                 $database->query('UPDATE email_attachments_table SET file_size = :filesize WHERE id = :lastattachmentid', array(':filesize' => filesize(ATTACHMENTS.$fromEmail.DS.$last_email_id.DS.$last_attachment_id.DS.$filename), ':lastattachmentid' => $last_attachment_id));
  186.             }
  187.         }
  188.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement