我目前正在使用imap
开发一个脚本来获取电子邮件并将其插入到mysql中。基本上是一个票务系统..脚本工作得很好,并将数据插入MySQL,但我有一些问题需要更新当前的票证。我想从电子邮件主题中获取票号并更新该票证。以下是我到目前为止的情况。有什么建议吗?
主题如下:RE:Ticket#12345 - Testing Ticket creation
。我需要做的是获取#
和-
之间的数字,将其与数据库匹配并更新。目前,它只是在MySQL中插入新项目。
/* try to connect to the mailbox*/
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect: ' . imap_last_error());
}elseif($eml_service_control =="0"){
echo 'Email to Tickets Service has been disabled by the system Administrator';
}
/* If all goes well then let us grab the emails */
$emails = imap_search($inbox,'UNSEEN');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
foreach($emails as $email_number){
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
$seen=$overview[0]->seen;
$date=$overview[0]->date;
$date2=$overview[0]->udate;
$from_email=imap_rfc822_parse_adrlist($overview[0]
->from, 'localhost');$from_email=$from_email[0];
$from_email=clean($from_email->mailbox."@".$from_email->host);
$subject=clean($overview[0]->subject);
$subject1=$overview[0]->subject;
$preg=preg_match("/Ticket#ID - ([0-9]+\s)/b", $subject1, $res);
$wo_id=extract_numbers($overview[0]->subject);print_r($res);
$wo_id=$wo_id[0];
$wo_id=$res[1];
if(!$wo_id){ $wo_id="";}
$message=strip_tags($message,"<br><div><p>");
$message=clean("Received <b>$date</b><br/><hr/>$message");
$q=mysql_query("SELECT * FROM `table_customer` WHERE
CUSTOMER_EMAIL='$from_email'");
if(mysql_num_rows($q)==1){ $customer_id=mysql_fetch_array($q);
$customer_id=$customer_id['CUSTOMER_ID']; } else { $customer_id=""; }
print_r($overview);
$q=mysql_query("SELECT * FROM tickets WHERE TICKET_ID='$ticket_id'") or die(mysql_error());
$n=mysql_num_rows($q);
$date2=date("H:i:s", $date2);
$priority="1";
if($n==0){
mysql_query("INSERT INTO `tickets`
(SCOPE, DESCRIPTION, PRIORITY_ID, CREATED_TIME, OPEN_DATE, STATUS, CUSTOMER_ID)
VALUES
('$subject', '$message','$priority', '$date2', '$date', '0', '$customer_id')");
} elseif($n==1) {
mysql_query("UPDATE tickets SET DESCRIPTION = CONCAT
('Received <b>$date</b><br/><hr/>$message <hr/><hr/>', DESCRIPTION) WHERE
TICKET_ID='$ticket_id'") or die(mysql_error());
}
$status = imap_setflag_full($inbox, $email_number, "\\Seen \\Flagged", ST_UID);
imap_delete($inbox, $email_number);
}
echo $output;
} else {
echo "<!-- debug -->";
}
/* close the connection */
imap_close($inbox);
答案 0 :(得分:1)
preg_match("/Ticket#([0-9]+)/", $subject, $output);
$output[1];
将从包含Ticket#{{number}}
的任何字符串返回数字。
答案 1 :(得分:1)
我会尝试这样的事情:
// Grab the ticket_id from the subject line ($str)
preg_match('/Ticket#([0-9]+)\s+?/', $str, $matches);
$ticket_id = $matches[1];
// Check if ticket id exists
$result=mysql_query("select count(*) as total from tickets where ticket_id ='$ticket_id'");
$data=mysql_fetch_assoc($result);
if ( $data['total'] > 0 ) {
// Do mysql update
}
else {
// Do mysql insert
}