IMAP connector based on POP3-Connector

Everything related to Hesk - helpdesk software

Moderator: mkoch227

Post Reply
somjue
Posts: 5
Joined: Wed Jul 18, 2012 9:11 pm

IMAP connector based on POP3-Connector

Post by somjue »

Was posted here: viewtopic.php?f=13&t=4114&p=17868#p17868

This add-on is created and tested with 2.4 RC and based on the standard e-mail parsing and ticket adding from HESK. Please note, that the Plugin creates a temporary file "imap.eml", maybe you would place it on a temp-directory.

Greetings,
Juerg
dr_patso
Posts: 192
Joined: Tue May 15, 2012 3:23 am

Re: IMAP connector based on POP3-Connector

Post by dr_patso »

Here is the code in that thread for easier reference

Code: Select all

#!/usr/bin/php -q
<?php

define('IN_SCRIPT',1);
define('HESK_PATH', dirname(dirname(dirname(__FILE__))) . '/');

// Get required files and functions
require(HESK_PATH . 'hesk_settings.inc.php');
require(HESK_PATH . 'inc/common.inc.php');

// should be included from hesk_settings.inc.php
/*
$hesk_settings['pop3']=1;
$hesk_settings['pop3_host_name']='imap.myserver.ch';
$hesk_settings['pop3_host_port']=143;
$hesk_settings['pop3_tls']=0;
$hesk_settings['pop3_user']='myusername';
$hesk_settings['pop3_password']='mypassword';
*/

// Is this feature enabled?
if (empty($hesk_settings['pop3']))
{
   die($hesklang['pfd']);
}

// Email piping is enabled, get other required includes
require(HESK_PATH . 'inc/pipe_functions.inc.php');

// Connecting zu Server
if ($hesk_settings['pop3_host_port'] == 993) {
   $host = "{".$hesk_settings['pop3_host_name'].":993/imap/ssl/novalidate-cert}"."INBOX";
} else {
   $host = "{".$hesk_settings['pop3_host_name'].":".$hesk_settings['pop3_host_port']."}INBOX";
}


// Connect to IMAP
if ($conn = imap_open($host, $hesk_settings['pop3_user'], $hesk_settings['pop3_password']   )) {
   echo $hesk_settings['debug_mode'] ? "<pre>Connected to the IMAP server "" . $host . "".</pre>\n" : '';
   echo $hesk_settings['debug_mode'] ? "<pre>User "" . $hesk_settings['pop3_user'] . "" logged in.</pre>\n" : '';

   // Get number of messages
   //$msgnos = imap_search($conn, 'UNSEEN');
   $msgnos = imap_search($conn, 'ALL');

   echo $hesk_settings['debug_mode'] ? "<pre>There are ".count($msgnos)." messages in the mail box.</pre>\n" : '';

   // If we have any messages, process them
   if(count($msgnos)>0) {
      // Connect to the database
      hesk_dbConnect();

      for ($msgno = 0; $msgno < count($msgnos); $msgno++) {
         echo $hesk_settings['debug_mode'] ? "<pre>Parsing message ".intval($msgno+1)." of ".count($msgnos).".</pre>\n" : '';

         // get full Message
         $message_raw = imap_fetchheader($conn, $msgnos[$msgno]).imap_body($conn, $msgnos[$msgno]);
         $fh=fopen("imap.eml","w");
         fwrite($fh, $message_raw);
         fclose($fh);

         // Parse the incoming email
         $results = parser("imap.eml");

         unlink("imap.eml");

         // Convert email into a ticket (or new reply)
         if ( $id = hesk_email2ticket($results, 1) ) {
            echo $hesk_settings['debug_mode'] ? "<pre>Ticket $id created/updated.</pre>\n" : '';
         } else {
            echo $hesk_settings['debug_mode'] ? "<pre>Ticket NOT inserted - may be duplicate, blocked or an error.</pre>\n" : '';
         }

         echo $hesk_settings['debug_mode'] ? "<br /><br />\n\n" : '';

         imap_delete($conn, $msgnos[$msgno]);  // mark the current message for deletion
      }

      imap_expunge($conn);  // delete all messages marked for deletion


   }

   imap_close($conn);
   echo $hesk_settings['debug_mode'] ? "<pre>Disconnected from the IMAP server "" . $host. "".</pre>\n" : '';
} else {
   $error=imap_errors()[0];
}

// Any error messages?
if($error != '')
{
   echo "<h2>Error: " . htmlspecialchars($error) . "</h2>";
}
return NULL;
This is freaking Great! I had to modify this line since, hesk settings will automatically disable pop3 fetching if you set an imap port that breaks pop3.

Just edit if pop3 host port = whatever port works with pop3. Not sure how this would work if pop3 was disabled on the mail server, but that's not typical.

Code: Select all

// Connecting zu Server
if ($hesk_settings['pop3_host_port'] == 110) {
   $host = "{".$hesk_settings['pop3_host_name'].":143/imap/notls}"."INBOX";
} else {
   $host = "{".$hesk_settings['pop3_host_name'].":".$hesk_settings['pop3_host_port']."}INBOX";
}
I really want to modify this code to take advantage of IMAP features. Let's say, check separate folders on the imap server. Let's say I have a folder on the imap server for each user, it could check for mail in each folder and assign tickets based on folder.. Check the dr_patso folder and any mail in dr_patso folder get's assigned to dr_patso in hesk. You could do it with categories too or something of that nature. Not sure how well php supports subfolders but you could have a folder for the user, and the categories under that.
Post Reply