email piping

Everything related to Hesk - helpdesk software

Moderator: mkoch227

Post Reply
ruffmeister
Posts: 48
Joined: Wed Jun 16, 2010 2:52 pm

email piping

Post by ruffmeister »

Hi there,

I know its not in a working version but wondered if anyone has ever got email piping working on version 2.1 / 2.2

I am willing to pay a coder to get this function working / if anyone would code share then that would be great

Help!
josesanch
Posts: 2
Joined: Sun Feb 07, 2010 10:38 am

Re: email piping

Post by josesanch »

This works for me.

Some things has changes.

I hope It would work for you.

Best Regards.

<?php
////////////////////////////////////////// Settings //////////////////////////////////////////
$server = "mailserver"; //mail server.
$user = "username"; // Mail Username
$password = "password" ; // Mail Password
$delete = "Y"; // if delete is Y then processed mail will be deleted
$debug = 0; // Display Notices about in/outgoing emails 1 = on | 0 = off
$category = 1; // Hesk Category ID to trow all emails in
$priority = 2; // 3=Low 2=Medium 1=High
/////////////////////////////////////////////////////////////////////////////////////////////

define('IN_SCRIPT',1);
define('HESK_PATH','');

if (!function_exists('imap_open')) die("IMAP functions are not available.");
if ($debug==1) error_reporting(E_ALL); else error_reporting(0);

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

// Connect to database
require(HESK_PATH . 'inc/database.inc.php');

hesk_dbConnect() or hesk_error("$hesklang[cant_connect_db] $hesklang[contact_webmsater] $hesk_settings[webmaster_mail]!");

$host = "{".$server.":143/imap/novalidate-cert}"."INBOX";

$msgStream = imap_open($host, $user, $password) or die("<br><br><b>Error: Can't connect to mailbox</b>");
$check = imap_mailboxmsginfo($msgStream);
$number = $check->Nmsgs;
$msgNumber = "1";
if (!$number) echo "<h1>No hay nuevos mensajes</h1>";

while ($msgNumber <= $number) {
list($subject, $name, $email, $date, $message, $trackingID) = getMessage($msgStream, $msgNumber);
/*** Additional code ***/
$elements = imap_mime_header_decode($subject);
$subject = mysql_real_escape_string($elements[0]->text);
if (!$subject) $subject = "Sin Asunto";

$elements = imap_mime_header_decode($name);
$name = mysql_real_escape_string($elements[0]->text);

$message = mysql_real_escape_string(imap_qprint($message));
/*** End of additional code ***/
if (!$trackingID) {
$trackingID = createNewTicket($subject, $name, $email, $date, $message);
} else {
createNewReply($subject, $name, $email, $date, $message, $trackingID);
}

//if ($delete == "Y") imap_delete($msgStream, $msgNumber); // mark the current message for deletion
$msgNumber++;
}

//if ($delete == "Y") imap_expunge($msgStream); // delete all messages marked for deletion
imap_close($msgStream);

// Functions
function getHeader($msgStream, $msgNumber) {
// strips the mail header and places everything in an array .. like from name, from email, date and subject
$mailheader = imap_headerinfo($msgStream, $msgNumber);
$headerArray = array();
$headerArray[0] = $mailheader->subject;
$from = $mailheader->from;
foreach ($from as $id => $object) {
$headerArray[1] = $object->personal; // from personal
$headerArray[2] = $object->mailbox . "@" . $object->host; // from address
}
$headerArray[3] = $mailheader->Date;

return $headerArray;
}

function extractPlain($msgStream, $msgNumber, $st = null, $num = "")
{
// pull the plain text for message $n
if (!$st) $st = imap_fetchstructure($msgStream, $msgNumber);
if (!empty($st->parts)) {
for ($i = 0, $j = count($st->parts); $i < $j; $i++) {
$part = $st->parts[$i];
if ($part->subtype == 'PLAIN') {
return imap_fetchbody($msgStream, $msgNumber, $num.($i+1));
}

if (!empty($part->parts)) {
$body = extractPlain($msgStream, $msgNumber, $part, ($i+1).".");
if ($body) return $body;
}
}
} else {
return imap_body($msgStream, $msgNumber);
}
}

function getMessage($msgStream, $msgNumber)
{
// pull the plain text for message $n
$message = nl2br(extractPlain($msgStream, $msgNumber));
$headerArray = getHeader($msgStream, $msgNumber);
$subject = htmlentities(addslashes($headerArray[0]));
$name = htmlentities(addslashes($headerArray[1]));
$email = htmlentities($headerArray[2]);
$date = htmlentities($headerArray[3]);
$txt = "";

if (preg_match("/- TrackingID: (.*)/", $subject, $matches)) {
$trackingID = $matches[1];
}
foreach (imap_mime_header_decode($name) as $item) $txt .= $item->text;
$name = $txt;
return array($subject, $name, $email, $date, $message, $trackingID);

}


function generateTrackingID()
{
// Generate tracking ID
$useChars='AEUYBDGHJLMNPQRSTVWXZ123456789';
$trackingID = $useChars{mt_rand(0,29)};
for($i=1;$i<10;$i++) $trackingID .= $useChars{mt_rand(0,29)};
return $trackingID;
}


function createNewTicket($subject, $name, $email, $date, $message)
{
global $category, $priority, $hesk_settings, $hesklang;
$trackingID = generateTrackingID();
$sql = "
INSERT INTO `".hesk_dbEscape($hesk_settings['db_pfix'])."tickets` (
`trackid`,`name`,`email`,`category`,`priority`,`subject`,`message`,`dt`,`lastchange`,`ip`,`status`,`attachments`,`custom1`,`custom2`,`custom3`,`custom4`,`custom5`
)
VALUES (
'$trackingID','$name','$email','$category','$priority','$subject','$message',NOW(),NOW(),'email','0','','','','','',''
)";

$result = hesk_dbQuery($sql);
$result = hesk_dbQuery("select * from `".hesk_dbEscape($hesk_settings['db_pfix'])."tickets` where trackid='$trackingID'");
$ticket = hesk_dbFetchAssoc($result);


/* Get e-mail message for customer */
$msg = hesk_getEmailMessage('new_ticket', $ticket);

/* Send e-mail */
$headers = "From: $hesk_settings[noreply_mail]\n";
$headers.= "Reply-to: $hesk_settings[noreply_mail]\n";
$headers.= "Return-Path: $hesk_settings[webmaster_mail]\n";
$headers.= "Content-type: text/plain; charset=".$hesklang['ENCODING'];
@mail($email,$hesklang['ticket_received']." - TrackingID: $trackingID",$msg,$headers);

/* Need to notify any admins? */
$admins=array();
$sql = "SELECT `email`,`isadmin`,`categories` FROM `".hesk_dbEscape($hesk_settings['db_pfix'])."users`"; //" WHERE `notify`='1'";
$result = hesk_dbQuery($sql);
while ($myuser=hesk_dbFetchAssoc($result))
{
/* Is this an administrator? */
if ($myuser['isadmin']) {$admins[]=$myuser['email']; continue;}
/* Not admin, is he allowed this category? */
$cat=substr($myuser['categories'], 0, -1);
$myuser['categories']=explode(',',$cat);
if (in_array($category,$myuser['categories']))
{
$admins[]=$myuser['email']; continue;
}
}
if (count($admins)>0) {
$msg = hesk_getEmailMessage('new_ticket_staff', $ticket, 1);

/* Send e-mail to staff */
$email=implode(',',$admins);
$headers = "From: $hesk_settings[noreply_mail]\n";
$headers.= "Reply-to: $hesk_settings[noreply_mail]\n";
$headers.= "Return-Path: $hesk_settings[webmaster_mail]\n";
$headers.= "Content-type: text/plain; charset=".$hesklang['ENCODING'];
@mail($email,$hesklang['new_ticket_submitted'],$msg,$headers);
}
echo "<h3>Creando nuevo ticked: $trackingID</h3>";

}

function createNewReply($subject, $name, $email, $date, $message, $trackingID)
{
global $hesk_settings, $hesklang;
$trackingID = trim($trackingID);

$sql = "SELECT id FROM `".hesk_dbEscape($hesk_settings['db_pfix'])."tickets` WHERE trackid='$trackingID'";
$result = hesk_dbQuery($sql) or hesk_error("$hesklang[cant_sql]: $sql</p><p>$hesklang[mysql_said]:<br>".mysql_error()."</p><p>$hesklang[contact_webmsater] $hesk_settings[webmaster_mail]");
$data = hesk_dbFetchAssoc($result);
$replyto = $data["id"];

/* Make sure the ticket is open */
$sql = "UPDATE `".hesk_dbEscape($hesk_settings['db_pfix'])."tickets` SET `status`='1',`lastreplier`='0',`lastchange`=NOW() WHERE `id`=".hesk_dbEscape($replyto)." LIMIT 1";
$result = hesk_dbQuery($sql);

/* Add reply */
$sql = "
INSERT INTO `".hesk_dbEscape($hesk_settings['db_pfix'])."replies` (
`replyto`,`name`,`message`,`dt`
)
VALUES (
'".hesk_dbEscape($replyto)."',
'".hesk_dbEscape($name)."',
'".hesk_dbEscape($message)."',
NOW()
)
";
$result = hesk_dbQuery($sql);
/* Need to notify any admins? */
$admins=array();
$sql = "SELECT `email`,`isadmin`,`categories` FROM `".hesk_dbEscape($hesk_settings['db_pfix'])."users`"; //" WHERE `notify`='1'";
$result = hesk_dbQuery($sql);
while ($myuser=hesk_dbFetchAssoc($result))
{
/* Is this an administrator? */
if ($myuser['isadmin']) {$admins[]=$myuser['email']; continue;}
/* Not admin, is he allowed this category? */
$cat=substr($myuser['categories'], 0, -1);
$myuser['categories']=explode(",",$cat);
if (in_array($category,$myuser['categories']))
{
$admins[]=$myuser['email']; continue;
}
}

if (count($admins)>0) {
/* Prepare ticket message for the e-mail */
$message = hesk_msgToPlain($message,1);

$trackingURL_admin = $hesk_settings['hesk_url'].'/admin/admin_ticket.php?track='.$trackingID;
$result = hesk_dbQuery("select * from `".hesk_dbEscape($hesk_settings['db_pfix'])."tickets` where trackid='$trackingID'");
$ticket = hesk_dbFetchAssoc($result);

$msg = hesk_getEmailMessage('new_reply_by_customer', $ticket, 1);

/* Send e-mail to staff */
$email=implode(',',$admins);
$headers = "From: $hesk_settings[noreply_mail]\n";
$headers.= "Reply-to: $hesk_settings[noreply_mail]\n";
$headers.= "Return-Path: $hesk_settings[webmaster_mail]\n";
$headers.= "Content-type: text/plain; charset=".$hesklang['ENCODING'];
@mail($email,$hesklang['new_reply_ticket'],$msg,$headers);
}
echo "<h3>Creando respuesta para: $trackingID</h3>";
}
hollandsedrop
Posts: 90
Joined: Tue Dec 30, 2008 11:29 am

Re: email piping

Post by hollandsedrop »

First of all thank you for your work.

Problems:
1 - Messages will be notified by mail to all staffs. Whereas the only relevant categories should be sent to staffs.
2 - There are problems in the charset.
Sampler
Posts: 9
Joined: Mon Mar 10, 2008 1:08 pm

Re: email piping

Post by Sampler »

Hi there, I've tried implementing the above code.

When the mailbox was empty we got the no new messages prompt, however when I retested after sending a message I get an error saying "can't execute SQL" - I've tried giving the SQL account the board uses all permissions rather than the few it had but this hasn't resolved the issue.

If I intentionally change the username/pass to an incorrect one it comes up with the "unable to logon" error so I'm fairly sure it's logging into the mailbox - hence the "no new mails" originally but it seems to struggle to handle a message in there.

Any help? I turned debugging on but get the same error.

P.S. just like to say thanks for a great mod, the IT guys will really appreciate it if we can get it running :D

Cheers
This message has been transmitted on 100% recycled electrons.
brek
Posts: 14
Joined: Tue Mar 23, 2010 10:12 am

Re: email piping

Post by brek »

same here. getting error:

Error:

Can't execute SQL
Please notify webmaster at support@domain.com

Anybody?
franku
Posts: 12
Joined: Thu Jul 22, 2010 3:57 pm

Re: email piping

Post by franku »

Hi,

We would love to see a resolution to this - we have so many customers who just wont co-operate and use the help desk - email piping would be great for us and a feature worth paying for.
Frank
Klemen
Site Admin
Posts: 10136
Joined: Fri Feb 11, 2005 4:04 pm

Re: email piping

Post by Klemen »

Klemen, creator of HESK and PHPJunkyardWas this helpful? You can buy me a drink here Image

Image You should follow me on Twitter here

Help desk software | Cloud help desk | Guestbook | Link manager | Click counter | more PHP Scripts ...

Also browse for php hosting companies, read php books, find php resources and use webmaster tools
Post Reply