Hi everyone,
I am trying to install a mail2db script that is ued with a support ticket system released for OSCommerce. Normally, the script would be run by the mail server via the alias file, but my web host will not allow me to have access to this, so I thought I'd try to get it to work another way.
The other way is basically running the script once every few minutes, and having it login to the POP3 server, downloading the messages and parsing them -- but I've run into a bit of a problem. The original mail2db script is writtent o interpret what a mail server would throw at it, but I don't know what that looks like. The mail2db script looks for a $contents variable, which it parses and extracts the header, parts (or structure) and body of the e-mail from that string.
Does that make sense? I hope so. Anyways, so here what I have so far (please excuse the length):
<?php
$MAILHOST = "server";
$USERNAME = "blah";
$PASSWORD = "blah";
// open mailbox
$inbox = @imap_open ("{". $MAILHOST . "/pop3:110}",
$USERNAME, $PASSWORD) or exit();
// get number of messages
$total = imap_num_msg($inbox);
if ($total > 0)
{
for($x=$total; $x>0; $x--)
{
// get individual message contents
$contents = imap_unknown_function($inbox, $x);
function osc_create_random_string() {
$ascii_from = 50; // 2
$ascii_to = 90; // Z
//
// exclude some characters
$exclude = array(58, 59, 60, 61, 62, 63, 64, 73, 79);
mt_srand((double)microtime() * 1000000);
$string = '';
$i = 0;
while ($i < 7) {
// random ASCII decimal within limits
$randnum = mt_rand($ascii_from, $ascii_to);
if (!in_array($randnum, $exclude)) {
$string .= chr($randnum);
$i++;
}
}
return $string;
}
function osc_parse_mime_decode_output(&$obj, &$parts){
if (!empty($obj->parts)) {
for ($i=0; $i<count($obj->parts); $i++) {
osc_parse_mime_decode_output($obj->parts[$i], $parts);
}
} else {
$ctype = $obj->ctype_primary.'/'.$obj->ctype_secondary;
switch ($ctype) {
case 'text/plain':
if (!empty($obj->disposition) AND $obj->disposition == 'attachment') {
$parts['attachments'][] = $obj->body;
} else {
$parts['text'][] = $obj->body;
}
break;
case 'text/html':
if (!empty($obj->disposition) AND $obj->disposition == 'attachment') {
$parts['attachments'][] = $obj->body;
} else {
$parts['html'][] = $obj->body;
}
break;
default:
$parts['attachments'][] = $obj->body;
}
}
}
// $config_file = true;
// Normally "$config_file", this is for database and mime functions
include('configure.php');
include(DIR_FS_ADMIN . 'includes/classes/mime_decode.php');
$link = mysql_connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD) or die('Could not connect to server.');
mysql_select_db(DB_DATABASE) or die('Count not select database.');
$store_query = mysql_query("select configuration_key, configuration_value from configuration where configuration_key in
('STORE_OWNER', 'STORE_OWNER_EMAIL_ADDRESS',
'DEFAULT_HELPDESK_STATUS_ID', 'DEFAULT_HELPDESK_PRIORITY_ID',
'DEFAULT_HELPDESK_DEPARTMENT_ID')", $link);
while ($store = mysql_fetch_array($store_query)) {
define($store['configuration_key'], $store['configuration_value']);
}
if (phpversion() > 4) {
$contents = file('php://stdin');
} else {
$contents = file('/dev/stdin');
}
if (sizeof($contents) < 1) {
$error = "\n" .
'An error has occured with osc_mail2db.' . "\n\n" .
'The command used was:' . "\n\n" .
implode(' ', $argv) . "\n\n";
mail(STORE_OWNER_EMAIL_ADDRESS, "[osC Mail2Database] Error", $error, 'From: ' . STORE_OWNER_EMAIL_ADDRESS);
exit;
}
reset($contents);
$contents = implode ('', $contents);
$params = array('input' => $contents,
'crlf' => "\r\n",
'include_bodies' => TRUE,
'decode_headers' => TRUE,
'decode_bodies' => TRUE);
$output = Mail_mimeDecode::decode($params);
$parts = array();
osc_parse_mime_decode_output($output, $parts);
$field_body = '';
if (isset($parts['text'][0])) {
$field_body = trim($parts['text'][0]);
} elseif (isset($parts['html'][0])) {
$field_body = trim(strip_tags($parts['html'][0]));
}
$output->headers['date'] = trim($output->headers['date']);
if (empty($output->headers['date'])) {
$field_date = date("Y-m-d H:i:s");
} else {
$field_date = date("Y-m-d H:i:s", strtotime($output->headers['date'], time()));
}
$output->headers['received'] = trim($output->headers['received']);
if (ereg('([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)', $output->headers['received'], $regs)) {
$field_ip = $regs[1];
} else {
$field_ip = '';
}
$field_host = @gethostbyaddr($field_ip);
$output->headers['from'] = trim($output->headers['from']);
if (empty($output->headers['from'])) {
$field_from = '';
$field_from_email_address = '';
} else {
if (ereg('"([^"]+)" <([^>]+)>', $output->headers['from'], $regs)) {
$field_from = trim($regs[1]);
$field_from_email_address = trim($regs[2]);
} elseif (ereg('([^<]+)<([^>]+)>', $output->headers['from'], $regs)) {
$field_from = trim($regs[1]);
$field_from_email_address = trim($regs[2]);
} elseif (substr($output->headers['from'], 0, 1) == '<') {
$field_from = substr($output->headers['from'], 1, -1);
$field_from_email_address = $field_from;
} else {
$field_from = $output->headers['from'];
$field_from_email_address = $field_from;
}
}
$output->headers['to'] = trim($output->headers['to']);
if (empty($output->headers['to'])) {
$field_to = '';
$field_to_email_address = '';
} else {
if (ereg('"([^"]+)" <([^>]+)>', $output->headers['to'], $regs)) {
$field_to = trim($regs[1]);
$field_to_email_address = trim($regs[2]);
} elseif (ereg('([^<]+)<([^>]+)>', $output->headers['to'], $regs)) {
$field_to = trim($regs[1]);
$field_to_email_address = trim($regs[2]);
} elseif (substr($output->headers['to'], 0, 1) == '<') {
$field_to = substr($output->headers['to'], 1, -1);
$field_to_email_address = $field_to;
} else {
$field_to = $output->headers['to'];
$field_to_email_address = $field_to;
}
}
$field_message_id = trim($output->headers['message-id']);
$ticket = false;
$parent_id = '0';
$status_id = DEFAULT_HELPDESK_STATUS_ID;
$priority_id = DEFAULT_HELPDESK_PRIORITY_ID;
$department_id = DEFAULT_HELPDESK_DEPARTMENT_ID;
$departments_query = mysql_query("select department_id from
helpdesk_departments where email_address = '" . addslashes
($field_to_email_address) . "'", $link);
if (mysql_num_rows($departments_query)) {
$departments = mysql_fetch_array($departments_query);
$department_id = $departments['department_id'];
}
$field_subject = trim($output->headers['subject']);
if (ereg('^.*\[osC:([A-Z0-9]{7})\].*$', $field_subject, $regs)) {
$ticket = $regs[1];
$ticket_info_query = mysql_query("select he.parent_id,
ht.department_id, ht.status_id, ht.priority_id from
helpdesk_entries he left join helpdesk_tickets ht on (he.ticket =
ht.ticket) where he.ticket = '" . $ticket . "' order by he.parent_id
desc limit 1", $link);
if (mysql_num_rows($ticket_info_query)) {
$ticket_info = mysql_fetch_array($ticket_info_query);
$parent_id = $ticket_info['parent_id'];
$status_id = $ticket_info['status_id'];
$priority_id = $ticket_info['priority_id'];
$department_id = $ticket_info['department_id'];
}
} else {
while (true) {
$ticket = osc_create_random_string();
$check_query = mysql_query("select count(*) as count from helpdesk_tickets where ticket = '" . $ticket . "'", $link);
$check = mysql_fetch_array($check_query);
if ($check['count'] < 1) break;
}
}
$check_query = mysql_query("select count(*) as count from
helpdesk_tickets where ticket = '" . $ticket . "'", $link);
$check = mysql_fetch_array($check_query);
if ($check['count'] < 1) {
mysql_query("insert into helpdesk_tickets (ticket,
department_id, priority_id, status_id, datestamp_last_entry)
values ('" . $ticket . "', '" . $department_id . "', '" .
DEFAULT_HELPDESK_PRIORITY_ID . "', '" .
DEFAULT_HELPDESK_STATUS_ID . "', now())", $link);
}
mysql_query("insert into helpdesk_entries (helpdesk_entries_id, ticket, parent_id, message_id,
ip_address, host, datestamp_local, datestamp,
receiver, receiver_email_address, sender,
email_address, subject, body, entry_read)
values ('', '" . $ticket . "', '" . $parent_id . "', '" .
addslashes($field_message_id) . "', '" . addslashe
($field_ip) . "', '" . addslashes($field_host) . "', now(), '" .
addslashes($field_date) . "', '" . addslashes($field_to) . "', '" .
addslashes($field_to_email_address) . "', '" . addslashe
($field_from) . "', '" . addslashe
($field_from_email_address) . "', '" . addslashes($field_subject) . "', '" . addslashes($field_body) . "', '0')", $link);
mysql_close($link);
// Mark the message for deletion when done with it
imap_delete($inbox, $x);
}
imap_close($inbox);
?>
I have the unknown function listed as imap_unknown_function. What I think I need is the function that just outputs an entire message data, and then let the rest of the script figure it out. Can someone look this over and enlighten me as to how I would do this? Or perhaps suggest or provide a better or more efficient way to do the above?
TIA