Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function caughtEmail($full_email)
- {
- global $database;
- global $member;
- global $EmailClass;
- # Include email parser
- require_once('rfc822_addresses.class.php');
- require_once('mime_parser.class.php');
- # Create the email parser class
- $mime = new mime_parser_class;
- $mime->ignore_syntax_errors = 1;
- $parameters=array(
- 'Data'=>$full_email,
- );
- $mime->Decode($parameters, $decoded);
- # Get the name and email of the sender
- $fromName = $decoded[0]['ExtractedAddresses']['from:'][0]['name'];
- $fromEmail = $decoded[0]['ExtractedAddresses']['from:'][0]['address'];
- # If If there is no fromName then fromName becomes fromEmail.
- if(!isset($fromName) || empty($fromName))
- {
- $fromName1 = explode('@', $fromEmail);
- $fromName = $fromName1[0];
- }
- # Get the name and email of the recipient
- $toEmail = $decoded[0]['ExtractedAddresses']['to:'][0]['address'];
- $toName = $decoded[0]['ExtractedAddresses']['to:'][0]['name'];
- if(!isset($toName)) $toName = "";
- # Get the subject
- $subject = $decoded[0]['Headers']['subject:'];
- # Stop script if it's spam
- if(strpos($subject, '***SPAM***') !== FALSE)
- {
- exit;
- }
- # If no subject then make it (No Subject)
- if($subject=="") $subject = "(No Subject)";
- # Retrieve Ticket ID
- preg_match('/\[#(\d+)\]/U', $subject, $subject_ticket_id);
- $removeChars = array('<','>');
- # Get the message id
- $messageID = str_replace($removeChars,'',$decoded[0]['Headers']['message-id:']);
- # Get the reply id
- $replyToID = str_replace($removeChars,'',$decoded[0]['Headers']['in-reply-to:']);
- nline
- # Get the message body
- if(substr($decoded[0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Body']))
- {
- $body = $decoded[0]['Body'];
- }
- elseif(substr($decoded[0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Parts'][0]['Body']))
- {
- $body = $decoded[0]['Parts'][0]['Body'];
- }
- 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']))
- {
- $body = $decoded[0]['Parts'][0]['Parts'][0]['Body'];
- }
- preg_match('/Email ID: (?P<body_ticket_id>\w+)/', $body, $body_ticket_id);
- $new_body = $body;
- # Send email to users with notification
- $database->query('SELECT id, full_name, email, email_notify FROM users_table WHERE email_notify = 1', array());
- $users_notify = $database->statement->fetchAll(PDO::FETCH_OBJ);
- if($database->count() > 0)
- {
- foreach($users_notify as $row)
- {
- $notify_subject = $EmailClass->site_name." - New Email";
- $notify_body_content = $row->full_name.",\r\n\r\nThere is a new email on the Hub";
- $EmailClass->sendEmail($row->email, $notify_subject, $notify_body_content, $row->full_name);
- }
- }
- # If there is no ticket ID in the subject line or body then create new ticket.
- if((!isset($subject_ticket_id)) && (!isset($body_ticket_id)))
- {
- $new_email = true;
- }
- else {
- if((isset($subject_ticket_id[1]) && (isset($body_ticket_id['body_ticket_id']))) && ($subject_ticket_id[1] == $body_ticket_id['body_ticket_id']))
- {
- $ticket_id = $subject_ticket_id[1];
- }
- elseif(isset($subject_ticket_id[1]))
- {
- $ticket_id = $subject_ticket_id[1];
- }
- elseif(isset($body_ticket_id['body_ticket_id']))
- {
- $ticket_id = $body_ticket_id['body_ticket_id'];
- }
- if(isset($ticket_id))
- {
- $email_id = $this->getEmailID($ticket_id);
- if($this->compareTicket($email_id, $ticket_id, $fromEmail) == true)
- {
- $new_email = false;
- $check_status = $this->getEmailStatus($email_id);
- $last_email_id = $email_id;
- if($check_status=="Closed")
- {
- $database->query('UPDATE emails_table SET status = :status WHERE id = :id', array(':status' => 2, ':id' => $email_id));
- }
- $database->query('UPDATE emails_table SET last_replier = :lastreplier, updated = NOW() WHERE id = :id', array(':lastreplier' => $fromName, ':id' => $email_id));
- $database->query('INSERT INTO email_message (email_id, ticket_id, message, created) VALUES (?, ?, ?, NOW())', array($email_id, $ticket_id, $new_body));
- $last_email_message_id = $database->lastInsertId();
- }
- else {
- # The ticket ID doesn't match the sender email so create new ticket.
- $new_email = true;
- }
- }
- else {
- # $ticket_id is not set so create new ticket.
- $new_email = true;
- }
- }
- # If $new_email is true then create new ticket.
- if($new_email == true)
- {
- $ticket_id = $this->genRandID(6);
- $category1 = explode('@',$toEmail);
- $category = ucwords(str_replace('_', ' ', $category1[0]));
- $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));
- # Get last email insert ID
- $last_email_id = $database->lastInsertId();
- $subject = "[#".$ticket_id."] ".$subject;
- $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";
- $EmailClass->sendEmail($fromEmail, $subject, $body_content, $fromName, $toEmail);
- }
- # Loop through email parts
- foreach($decoded[0]['Parts'] as $part)
- {
- # Check for attachments
- if($part['FileDisposition'] == 'attachment')
- {
- # Format file name (change spaces to underscore then remove anything that isn't a letter, number or underscore)
- $filename = preg_replace('/[^0-9,a-z,\.,_]*/i', '', str_replace(' ', '_', $part['FileName']));
- if($new_email == true)
- {
- $database->query('INSERT INTO email_attachments (email_id, file_name, created) VALUES (?, ?, NOW())', array($last_email_id, $filename));
- }
- else {
- $database->query('INSERT INTO email_attachments (email_id, reply_id, file_name, created) VALUES (?, ?, ?, NOW())', array($email_id, $last_email_message_id, $filename));
- }
- $last_attachment_id = $database->lastInsertId();
- # Write the data to the file
- if(!is_dir(ATTACHMENTS.$fromEmail.DS.$last_email_id.DS.$last_attachment_id))
- {
- mkdir(ATTACHMENTS.$fromEmail.DS.$last_email_id.DS.$last_attachment_id.DS, 0755, true);
- }
- $fp = fopen(ATTACHMENTS.$fromEmail.DS.$last_email_id.DS.$last_attachment_id.DS.$filename, 'w');
- $written = fwrite($fp, $part['Body']);
- fclose($fp);
- $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));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement