Email (Imap) Addon
Posted: Wed Jan 07, 2009 8:06 pm
/*************************************
Title: Mail ADDON
Version: 1.0
Author: Joep Persoon djjoepwork-<[at]>-yahoo-<[dot]>-com
Demo: -
Download: -
Website: -
Description:
Addon to connect true imap a mailbox, extract the emails and create tickets for each one of them. The title will be the email subject, Conformation will be send to the admins & customer etc just like adding a ticket true the website
Script has to run with help of cronjob or something else you wanna use to run it every x minutes
*************************************/
What do you need to use this addon ?:
- an email account with imap support
- imap enabled in your webserver settings (script will tell you when its disabled)
- Cronjob or somethimg simular to run the script automatic every x minutes
How to use it:
- copy paste the code into a file and name it imap.php
- change the settings in imap.php
- upload it in your hesk root folder
- setup a cronjob to run the file http://YOURHESKURL/imap.php every x minutes ( I have setup every 10 minutes but its what you prefer)
Filename : imap.php (place it in your hesk root folder)
[Edit] Changed debug info displayed when debug=1. With tnx to ictconsulting! [/Edit]
Title: Mail ADDON
Version: 1.0
Author: Joep Persoon djjoepwork-<[at]>-yahoo-<[dot]>-com
Demo: -
Download: -
Website: -
Description:
Addon to connect true imap a mailbox, extract the emails and create tickets for each one of them. The title will be the email subject, Conformation will be send to the admins & customer etc just like adding a ticket true the website
Script has to run with help of cronjob or something else you wanna use to run it every x minutes
*************************************/
What do you need to use this addon ?:
- an email account with imap support
- imap enabled in your webserver settings (script will tell you when its disabled)
- Cronjob or somethimg simular to run the script automatic every x minutes
How to use it:
- copy paste the code into a file and name it imap.php
- change the settings in imap.php
- upload it in your hesk root folder
- setup a cronjob to run the file http://YOURHESKURL/imap.php every x minutes ( I have setup every 10 minutes but its what you prefer)
Filename : imap.php (place it in your hesk root folder)
Code: Select all
<?php
// Settings /////////////////////////////////////////////////////////////////////////////////
$server = "pop.yourdomain.com"; //mail server.
$user = "yourusername"; // Mail Username
$password = "yourpassword"; // 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);
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_once('hesk_settings.inc.php');
require_once('language/'.$hesk_settings['language'].'.inc.php');
require_once('inc/common.inc.php');
// Connect to database
require_once('inc/database.inc.php');
hesk_dbConnect() or hesk_error("$hesklang[cant_connect_db] $hesklang[contact_webmsater] $hesk_settings[webmaster_mail]!");
// 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 getBody($msgStream, $msgNumber){
// gets email content
$body=imap_body($msgStream,$msgNumber);
return $body;
}
$host = "{".$server.":143"."}"."INBOX";
//connect to inbox and check for new msgs
$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;
if ($debug==1) {
echo "<b>Notices (".date("H:i:s")."):</b><br>";
if ($number>0) {
echo "<b> - ".$number." email(s)</b><br>";
} else {
echo "<b> - No Email in Inbox</b><br>";
}
$mbox = imap_open("{".$server.":143}", $user, $password);
echo "<h1>Mailboxes</h1>\n";
$folders = imap_listmailbox($mbox, "{".$server."}", "*");
if ($folders == false) {
echo "imap_listmailbox failed<br />\n";
} else {
foreach ($folders as $val) {
echo $val . "<br />\n";
}
}
echo "<h1>Headers in INBOX</h1>\n";
$headers = imap_headers($mbox);
if ($headers == false) {
echo "imap_headers failed<br />\n";
} else {
foreach ($headers as $val) {
echo $val . "<br />\n";
}
}
imap_close($mbox);
}
$msgNumber = "1";
while ($msgNumber <= $number) {
$headerArray = getHeader($msgStream, $msgNumber);
$message = htmlentities(getBody($msgStream, $msgNumber));
$subject = htmlentities($headerArray[0]);
$name = htmlentities($headerArray[1]);
$email = htmlentities($headerArray[2]);
$date = htmlentities($headerArray[3]);
// Generate tracking ID
$useChars='AEUYBDGHJLMNPQRSTVWXZ123456789';
$trackingID = $useChars{mt_rand(0,29)};
for($i=1;$i<10;$i++) $trackingID .= $useChars{mt_rand(0,29)};
$trackingURL=$hesk_settings['hesk_url'].'/ticket.php?track='.$trackingID;
// End Generate tracking ID
$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) or hesk_error("$hesklang[cant_sql]: $sql</p><p>$hesklang[mysql_said]:<br>".mysql_error()."</p><p>$hesklang[contact_webmsater] $hesk_settings[webmaster_mail]");
// Get e-mail message for customer
$fp=fopen('emails/new_ticket.txt','r');
$message=fread($fp,filesize('emails/new_ticket.txt'));
fclose($fp);
$message=str_replace('%%NAME%%',$name,$message);
$message=str_replace('%%SUBJECT%%',$subject,$message);
$message=str_replace('%%TRACK_ID%%',$trackingID,$message);
$message=str_replace('%%TRACK_URL%%',$trackingURL,$message);
$message=str_replace('%%SITE_TITLE%%',$hesk_settings['site_title'] ,$message);
$message=str_replace('%%SITE_URL%%',$hesk_settings['site_url'] ,$message);
// Send e-mail
$headers="From: $hesk_settings[noreply_mail]\n";
$headers.="Reply-to: $hesk_settings[noreply_mail]\n";
@mail($email,$hesklang['ticket_received'],$message,$headers);
// Need to notify any admins?
$admins=array();
$sql = "SELECT `email`,`isadmin`,`categories` FROM `hesk_users` WHERE `notify`='1'";
$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]");
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_ticket.php?track='.$trackingID;
// Get e-mail message for customer
$fp=fopen('emails/new_ticket_staff.txt','r');
$message=fread($fp,filesize('emails/new_ticket_staff.txt'));
fclose($fp);
$message=str_replace('%%NAME%%',$name,$message);
$message=str_replace('%%SUBJECT%%',$subject,$message);
$message=str_replace('%%TRACK_ID%%',$trackingID,$message);
$message=str_replace('%%TRACK_URL%%',$trackingURL_admin,$message);
$message=str_replace('%%SITE_TITLE%%',$hesk_settings['site_title'] ,$message);
$message=str_replace('%%SITE_URL%%',$hesk_settings['site_url'] ,$message);
// Send e-mail to staff
$email=implode(',',$admins);
$headers="From: $hesk_settings[noreply_mail]\n";
$headers.="Reply-to: $hesk_settings[noreply_mail]\n";
@mail($email,$hesklang['new_ticket_submitted'],$message,$headers);
} // End if
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);
?>