Email (Imap) Addon

Everything related to Hesk - helpdesk software

Moderator: mkoch227

Do you think this addon is usefull ?

Yes!
21
84%
No!
1
4%
Maybe
3
12%
Don't know what the hell this is
0
No votes
 
Total votes: 25

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

Modified version

Post by josesanch »

Hi everyone.

I've solved the problems with multi-part email and also I've added the posibility that the costumer of replying e-mails to generate new replies for the ticket.

There is the code

Code: Select all

<?php
////////////////////////////////////////// Settings //////////////////////////////////////////
$server               = "mailserver"; //mail server.
$user                 = "username"; // Mail Username
$password             = "pass"; // 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         = 3; // 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);

	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();
	$trackingURL = $hesk_settings['hesk_url'].'/ticket.php?track='.$trackingID;
	$sql = "
		INSERT INTO `hesk_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);

	/* Get e-mail message for customer */
	$msg = hesk_getEmailMessage('new_ticket');
	$msg = str_replace('%%NAME%%',hesk_msgToPlain($name,1),$msg);
	$msg = str_replace('%%SUBJECT%%',hesk_msgToPlain($subject,1),$msg);
	$msg = str_replace('%%TRACK_ID%%',$trackingID,$msg);
	$msg = str_replace('%%TRACK_URL%%',$trackingURL,$msg);
	$msg = str_replace('%%SITE_TITLE%%',$hesk_settings['site_title'],$msg);
	$msg = str_replace('%%SITE_URL%%',$hesk_settings['site_url'],$msg);
	$msg = str_replace('%%MESSAGE%%',hesk_msgToPlain($message,1),$msg);

	/* 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)
	{
		$trackingURL_admin=$hesk_settings['hesk_url'].'/admin/admin_ticket.php?track='.$trackingID;

		$msg = hesk_getEmailMessage('new_ticket_staff');
		$msg = str_replace('%%NAME%%',hesk_msgToPlain($name,1),$msg);
		$msg = str_replace('%%SUBJECT%%',hesk_msgToPlain($subject,1),$msg);
		$msg = str_replace('%%TRACK_ID%%',$trackingID,$msg);
		$msg = str_replace('%%TRACK_URL%%',$trackingURL_admin,$msg);
		$msg = str_replace('%%SITE_TITLE%%',$hesk_settings['site_title'],$msg);
		$msg = str_replace('%%SITE_URL%%',$hesk_settings['site_url'],$msg);
		$msg = str_replace('%%MESSAGE%%',hesk_msgToPlain($message,1),$msg);

		/* 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);
	$trackingURL = $hesk_settings['hesk_url'].'/ticket.php?track='.$trackingID;
	$sql = "SELECT id FROM  `hesk_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;

		$msg = hesk_getEmailMessage('new_reply_by_customer');
		$msg = str_replace('%%NAME%%',$name,$msg);
		$msg = str_replace('%%SUBJECT%%',$subject,$msg);
		$msg = str_replace('%%TRACK_ID%%',$trackingID,$msg);
		$msg = str_replace('%%TRACK_URL%%',$trackingURL_admin,$msg);
		$msg = str_replace('%%SITE_TITLE%%',$hesk_settings['site_title'],$msg);
		$msg = str_replace('%%SITE_URL%%',$hesk_settings['site_url'],$msg);
		$msg = str_replace('%%MESSAGE%%',$message,$msg);

		/* 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>";
}
edsel
Posts: 1
Joined: Tue Feb 16, 2010 2:40 pm

Change of the charset so it allows æ ø å ä ö?

Post by edsel »

To allow foreign characters like the ones we use here in Sweden I added a small piece of code in the solution of Josesanch att about line 37:

Code: Select all

while ($msgNumber <= $number) { 
   list($subject, $name, $email, $date, $message, $trackingID) = getMessage($msgStream, $msgNumber); 

		/*** Additional code ***/
		$elements = imap_mime_header_decode($subject);
		$subject = $elements[0]->text;

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

		$message = imap_qprint($message);
		/*** End of additional code ***/

if (!$trackingID) { 
      $trackingID = createNewTicket($subject, $name, $email, $date, $message); 
   } else { 
      createNewReply($subject, $name, $email, $date, $message, $trackingID); 
   } 

This worked for me very well.

Good luck,
Edsel - Sweden
MonkeyButt
Posts: 9
Joined: Tue Mar 09, 2010 1:53 am

Got yours working yet errors with apostrophe

Post by MonkeyButt »

josesanch,

Out of all the other codes I copy/pasted from this thread, yours was the only one I got working and works good. I have 1 issue that is killing me. When I send messages to the mailbox and run your script, I don't know if there are other charactures but one that is getting me is if there is an apostrophe in the email. For example, someone types Who's your daddy... the ' generates the error below. If no ' then message posts good. Any ideas? I can't confirm yet if it would also happen on " or slashes, etc... Thanks!!

Code: Select all


Can't execute SQL: INSERT INTO `hesk_tickets` ( `trackid`,`name`,`email`,`category`,`priority`,`subject`,`message`,`dt`,`lastchange`,`ip`,`status`,`attachments`,`custom1`,`custom2`,`custom3`,`custom4`,`custom5` ) VALUES ( '2HXYESRMBB','Test User','testemail@email.com','8','3','This is a test','Getting an error when there is an apostrophe in the body of an email.
Sample line is Mark's car.

',NOW(),NOW(),'email','0','','','','','','' )

MySQL said:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's car.

',NOW(),NOW(),'email','0','','','','','',' at line 5

 
IgMuSh
Posts: 5
Joined: Fri Mar 12, 2010 12:55 pm

Re: Got yours working yet errors with apostrophe

Post by IgMuSh »

MonkeyButt wrote:josesanch,
When I send messages to the mailbox and run your script, I don't know if there are other charactures but one that is getting me is if there is an apostrophe in the email.
I also encountered the same problem.
I tried to solve the problem myself.
It seems to be solved.
You should find this line

Code: Select all

   $sql = "INSERT INTO `hesk_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','','','','','','')"; 
Then change it to this

Code: Select all

   $sql = "INSERT INTO `hesk_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','".mysql_real_escape_string($message)."',NOW(),NOW(),'email','0','','','','','','')";
[/code]
MonkeyButt
Posts: 9
Joined: Tue Mar 09, 2010 1:53 am

Well.. not for me..

Post by MonkeyButt »

1st, thanks a ton for replying.. I made that change and it didn't quite work for me. Here's my error after the code change you just sent. I'm not a code person but it looks like where it should say "What's the problem here..", it's added a \ which I'm assuming is causing the issue. Any ideas?

Code: Select all

Can't execute SQL: INSERT INTO `hesk_tickets` (`trackid`,`name`,`email`,`category`,`priority`,`subject`,`message`,`dt`,`lastchange`,`ip`,`status`,`attachments`,`custom1`,`custom2`,`custom3`,`custom4`,`custom5`) VALUES '7SNS2JE795','Test User','test@email.com','8','3','Hello World!','What\'s the problem here...
\r\n',NOW(),NOW(),'email','0','','','','','','')

MySQL said:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''7SNS2JE795','Nelson Bruner','nbruner@ehosted.com','8','3','Hello World!','What\' at line 1
IgMuSh
Posts: 5
Joined: Fri Mar 12, 2010 12:55 pm

Re: Well.. not for me..

Post by IgMuSh »

Try this before previous string:

Code: Select all

    if (get_magic_quotes_gpc()) {$message = stripslashes($message);}
MonkeyButt
Posts: 9
Joined: Tue Mar 09, 2010 1:53 am

Arr...

Post by MonkeyButt »

I must have something wrong!! ##$@#@ Again, I appreciate your help.. Hate to do this but let me post my entire imap.php code and glance and make sure I have it right.

Code: Select all


<?php
////////////////////////////////////////// Settings //////////////////////////////////////////
$server               = "imap.server.com"; //mail server.
$user                 = "username"; // Mail Username
$password             = "password"; // Mail Password
$delete               = "Y";  // if delete is Y then processed mail will be deleted
$debug                = 1; // Display Notices about in/outgoing emails 1 = on | 0 = off
$category         = 8; // Hesk Category ID to trow all emails in
$priority         = 3; // 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);

   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();
   $trackingURL = $hesk_settings['hesk_url'].'/ticket.php?track='.$trackingID;
   
   if (get_magic_quotes_gpc()) {$message = stripslashes($message);} 
   $sql = "INSERT INTO `hesk_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','".mysql_real_escape_string($message)."',NOW(),NOW(),'email','0','','','','','','')"; 

   $result = hesk_dbQuery($sql);

   /* Get e-mail message for customer */
   $msg = hesk_getEmailMessage('new_ticket');
   $msg = str_replace('%%NAME%%',hesk_msgToPlain($name,1),$msg);
   $msg = str_replace('%%SUBJECT%%',hesk_msgToPlain($subject,1),$msg);
   $msg = str_replace('%%TRACK_ID%%',$trackingID,$msg);
   $msg = str_replace('%%TRACK_URL%%',$trackingURL,$msg);
   $msg = str_replace('%%SITE_TITLE%%',$hesk_settings['site_title'],$msg);
   $msg = str_replace('%%SITE_URL%%',$hesk_settings['site_url'],$msg);
   $msg = str_replace('%%MESSAGE%%',hesk_msgToPlain($message,1),$msg);

   /* 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)
   {
      $trackingURL_admin=$hesk_settings['hesk_url'].'/admin/admin_ticket.php?track='.$trackingID;

      $msg = hesk_getEmailMessage('new_ticket_staff');
      $msg = str_replace('%%NAME%%',hesk_msgToPlain($name,1),$msg);
      $msg = str_replace('%%SUBJECT%%',hesk_msgToPlain($subject,1),$msg);
      $msg = str_replace('%%TRACK_ID%%',$trackingID,$msg);
      $msg = str_replace('%%TRACK_URL%%',$trackingURL_admin,$msg);
      $msg = str_replace('%%SITE_TITLE%%',$hesk_settings['site_title'],$msg);
      $msg = str_replace('%%SITE_URL%%',$hesk_settings['site_url'],$msg);
      $msg = str_replace('%%MESSAGE%%',hesk_msgToPlain($message,1),$msg);

      /* 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);
   $trackingURL = $hesk_settings['hesk_url'].'/ticket.php?track='.$trackingID;
   $sql = "SELECT id FROM  `hesk_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;

      $msg = hesk_getEmailMessage('new_reply_by_customer');
      $msg = str_replace('%%NAME%%',$name,$msg);
      $msg = str_replace('%%SUBJECT%%',$subject,$msg);
      $msg = str_replace('%%TRACK_ID%%',$trackingID,$msg);
      $msg = str_replace('%%TRACK_URL%%',$trackingURL_admin,$msg);
      $msg = str_replace('%%SITE_TITLE%%',$hesk_settings['site_title'],$msg);
      $msg = str_replace('%%SITE_URL%%',$hesk_settings['site_url'],$msg);
      $msg = str_replace('%%MESSAGE%%',$message,$msg);

      /* 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>";
} 


IgMuSh
Posts: 5
Joined: Fri Mar 12, 2010 12:55 pm

Re: Arr...

Post by IgMuSh »

I forgot to put a semicolon after

Code: Select all

if (get_magic_quotes_gpc()) {$message = stripslashes($message);}; 
MonkeyButt
Posts: 9
Joined: Tue Mar 09, 2010 1:53 am

Closer?

Post by MonkeyButt »

Well, no dice.. this is what I get... i did this one in plain text to make sure it stripped out junk text..

Can't execute SQL: INSERT INTO `hesk_tickets` (`trackid`,`name`,`email`,`category`,`priority`,`subject`,`message`,`dt`,`lastchange`,`ip`,`status`,`attachments`,`custom1`,`custom2`,`custom3`,`custom4`,`custom5`) VALUES 'MS9VWN2R3G','Test User','test@email.com','8','3','Hello World 2','
\r\n
\r\n
\r\nThank You,
\r\n
\r\nTest User
\r\n
\r\n
\r\n',NOW(),NOW(),'email','0','','','','','','')

MySQL said:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''MS9VWN2R3G','Test User','test@email.com','8','3','Hello World 2','
MonkeyButt
Posts: 9
Joined: Tue Mar 09, 2010 1:53 am

Trying..

Post by MonkeyButt »

Hey.. I'm trying to hack away at this a little more, the command you gave me to strip the slashes, if I wanted to tell it to strip away all the commas, how would I modify this? I tried = stripscommas but it gives me the same error messages so i don't know if that's even correct or not. And, I assume I can have both "if" statements, one for stripping slashes and one for commas?

You 1st code: if (get_magic_quotes_gpc()) {$message = stripslashes($message);};
IgMuSh
Posts: 5
Joined: Fri Mar 12, 2010 12:55 pm

Re: Trying..

Post by IgMuSh »

I am very sorry but I missed left parenthesis after "VALUES".
It must be:

Code: Select all

$sql = "INSERT INTO `hesk_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','".mysql_real_escape_string($message)."',NOW(),NOW(),'email','0','','','','','','')"; 
MonkeyButt
Posts: 9
Joined: Tue Mar 09, 2010 1:53 am

You are the bomb!!

Post by MonkeyButt »

That last code you sent worked like a champ!!! I was starting to doubt my copy/paste expertise! ;-) I've sent a bunch of email tests, with commas, quotes, attachments (which just ignores the attachments which is fine), and boom, not had an issue yet.

Thanks you SO much for taking the time to help. As with everyone in this forum, it's people like you that make all us new coders want to learn more so we can contribute back someday. I can't say thank you enough.

Now, off that.. Let me start this one by saying I'm not advertising for these people ** I know there are a lot of people that don't have cron access on their virtual servers (or can process more than once a day like me) but I found these people a while back and they appear to be stable and I've yet to have an issue, but anyone reading this, do your own research. It's free for up to 5 cron jobs. http://www.cronless.com/ That's it.. maybe that can help someone. ;-)
brek
Posts: 14
Joined: Tue Mar 23, 2010 10:12 am

Post by brek »

is it working with domain emails linked in Google Apps? Because I getting error then i typing in server (imap.gmail.com) : IMAP functions are not available :
MonkeyButt
Posts: 9
Joined: Tue Mar 09, 2010 1:53 am

gmail

Post by MonkeyButt »

Hey Brek, I'm not a good coder so I can't give you a good answer. But, I can tell you that in your code, gmail uses port 993 and "almost" certain require SSL (opposed to the normal port 143).

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

I see in the code that no SSL is required so I can't tell you exactly how to turn that on. Hope that helps.
brek
Posts: 14
Joined: Tue Mar 23, 2010 10:12 am

Post by brek »

yep, i also bad coder at this point :) anyone can help here how to enable SSL with google imap servers?
Post Reply