View Issue Details
ID | Project | Category | View Status | Date Submitted | Last Update |
---|---|---|---|---|---|
0011195 | mantisbt | feature | public | 2009-11-19 16:17 | 2009-11-19 16:21 |
Reporter | jeremib | Assigned To | |||
Priority | none | Severity | feature | Reproducibility | have not tried |
Status | new | Resolution | open | ||
Summary | 0011195: IMAP to Mantis - Adding notes to mantis from imap account | ||||
Description | I had the need to monitor an imap account, look for the ticket number in the subject, and then add the contents of the email as a note on the ticket. I created the attached file, which is run via cron every 5 minutes. It looks for subjects that contain the format #<bug number>. So 0002939. It also handles attachments and adds those to the ticket as well. This script can be executed on a different server as Mantis is running as it connects to mantis via SOAP. | ||||
Tags | No tags attached. | ||||
Attached Files | imap_to_mantis.php (5,439 bytes)
<?php /** * Logs in and checks an imap account for subjects which match #<bug number * then adds the note to the bug via SOAP. * * @author Jeremi Bergman <jeremib@gmail.com> */ ## SETTINGS $mantis_soap_wsdl = "http://domain.tld/mantis/api/soap/mantisconnect.php?wsdl"; $mantis_username = ''; $mantis_password = ''; $mailbox_username = ""; $mailbox_password = ""; $mailbox_identifier = "{imap.gmail.com:993/imap/ssl}INBOX"; ## END SETTINGS $sc = new SoapClient($mantis_soap_wsdl); $mbox = imap_open($mailbox_identifier, $mailbox_username, $mailbox_password) or die("can't connect: " . imap_last_error()); $mc = imap_check($mbox); $result = imap_fetch_overview($mbox,"1:{$mc->Nmsgs}",0); foreach ($result as $overview) { //check to see if message has been viewed yet $seen_msg = $overview->seen; //retrieve message number, date, from, subject, uid, seen, //and message body from message if (!$seen_msg){ if ( preg_match_all("/#\d+/", $overview->subject, $matches) ) { foreach($matches as $match) { $bug_id = preg_replace("/[^0-9]/","", $match); $bug_id = $bug_id[0]; $files = extract_attachments($mbox, $overview->msgno); $filenames = array(); if (is_array($files)) { foreach($files as $file) { if ( $file['is_attachment'] ) { $tmp_filename = "/tmp/" .time(); file_put_contents($tmp_filename, $file['attachment']); $save_filename = time() . "_" . $file['filename']; $sc->mc_issue_attachment_add($mantis_username, $mantis_password, $bug_id, $save_filename, mime_content_type($tmp_filename), base64_encode($file['attachment'])); unlink($tmp_filename); $filenames[] = $save_filename; } } } $text = "Subject: {$overview->subject}\n"; $text .= "From: {$overview->from}\n"; $text .= "To: {$overview->to}\n"; $text .= "===============\n"; $text .= get_part($mbox, $overview->msgno, 'TEXT/PLAIN'); foreach($filenames as $filename) { $text .= "Attached File: {$filename}\n"; } $sc->mc_issue_note_add($mantis_username, $mantis_password, $bug_id, array('text' => $text)); } imap_setflag_full($mbox, $overview->msgno, "\\Seen \\Deleted"); } } // end if } // end foreach /** * http://www.php.net/manual/en/function.imap-fetchbody.php#4880 */ function get_mime_type(&$structure) { $primary_mime_type = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER"); if($structure->subtype) { return $primary_mime_type[(int) $structure->type] . '/' . $structure->subtype; } return "TEXT/PLAIN"; } /** * http://www.php.net/manual/en/function.imap-fetchbody.php#4880 */ function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) { if (!$structure) { $structure = imap_fetchstructure($stream, $msg_number); } if($structure) { if($mime_type == get_mime_type($structure)) { if(!$part_number) { $part_number = "1"; } $text = imap_fetchbody($stream, $msg_number, $part_number); if($structure->encoding == 3) { return imap_base64($text); } else if ($structure->encoding == 4) { return imap_qprint($text); } else { return $text; } } if ($structure->type == 1) { /* multipart */ while (list($index, $sub_structure) = each($structure->parts)) { if ($part_number) { $prefix = $part_number . '.'; } $data = get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1)); if ($data) { return $data; } } } } return false; } /** * http://www.electrictoolbox.com/function-extract-email-attachments-php-imap/ */ function extract_attachments($connection, $message_number) { $attachments = array(); $structure = imap_fetchstructure($connection, $message_number); if(isset($structure->parts) && count($structure->parts)) { for($i = 0; $i < count($structure->parts); $i++) { $attachments[$i] = array( 'is_attachment' => false, 'filename' => '', 'name' => '', 'attachment' => '' ); if($structure->parts[$i]->ifdparameters) { foreach($structure->parts[$i]->dparameters as $object) { if(strtolower($object->attribute) == 'filename') { $attachments[$i]['is_attachment'] = true; $attachments[$i]['filename'] = $object->value; } } } if($structure->parts[$i]->ifparameters) { foreach($structure->parts[$i]->parameters as $object) { if(strtolower($object->attribute) == 'name') { $attachments[$i]['is_attachment'] = true; $attachments[$i]['name'] = $object->value; } } } if($attachments[$i]['is_attachment']) { $attachments[$i]['attachment'] = imap_fetchbody($connection, $message_number, $i+1); if($structure->parts[$i]->encoding == 3) { // 3 = BASE64 $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); } elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']); } } } } return $attachments; } ?> | ||||