Hi, I'm sure, here are some folks who have used Kayako support suite for web hosting purpose. You have noticed a feature there. Whenever a ticket is submitted and the client receive a reply from the help desk to his inbox, the client can simply reply the email to post further on the same ticket. I mean, the client replies to the email and the email message is appended to the ticket page. This is the basic work of an email parser. The parser, parses each email, received to a certain email id of the web host and saves the data to the database. I need a PHP code snippet to do the same. I want to parse preconfigured emails received in my inbox and want to do some operations (not important here) with the parsed texts. Folks, please help me this time to get the code. Anyone here knows how to do that?
I used this for a helpdesk script I developed a while back. It's a little rough, but should parse the email as you are needing. You will need to put the data into a database or do whatever else you need with it. This script does not use a pipe like many do, but checks an inbox via PHP's IMAP function. $imap = imap_open("{MYSITE.COM:143/notls}INBOX", "USERNAME", "PASSWORD")): $message_count = imap_num_msg($imap); for ($i = 1; $i <= $message_count; ++$i): $header = imap_header($imap, $i); //DEBUGGING ONLY //print_r($header); $body = imap_fetchbody($imap, $i, "1.1"); if ($body == ""): $body = imap_fetchbody($imap, $i, "1"); endif; $body = trim(substr(quoted_printable_decode($body), 0, 100)); //GET THE VARIABLES WE NEED $email[$i]['from'] = $header->from[0]->mailbox.'@'.$header->from[0]->host; $email[$i]['fromaddress'] = $header->from[0]->personal; $email[$i]['to'] = $header->to[0]->mailbox; $email[$i]['subject'] = $header->subject; $email[$i]['reply_to'] = $header->reply_to[0]->mailbox.'@'.$header->reply_to[0]->host; $email[$i]['message_id'] = $header->message_id; $email[$i]['date'] = $header->udate; $email[$i]['body'] = $body; $from = $email[$i]['fromaddress']; $from_email = $email[$i]['from']; //DO WHATEVER YOU NEED TO PROCESS THE MESSAGE HERE //ADVUST THIS FOR YOUR OWN USAGE --$message->save() probably wont mean anything to you if($message->save()): imap_delete($imap, $i); endif; endfor; imap_expunge($imap); imap_close($imap); PHP:
Thank you jestep a lot for this contribution. I was in search of the script for last 1 year. Please check the first line of the script. Is it correct? It's ending with a colon ), I've not checked yet. I'm really unaware of imap functions of PHP. So I'll ask help once more. I've three queries. 1. What should be my code for the quoted portion, if I'm using cPanel as control panel (LAMP environment) and my mail server is mail.mysite.com? What should be the same if I'm using GMail? 2. Is there any way to autorun this script, whenever a mail is received? I mean, can I set the script to run as a cron, when a new email enters my mail server? 3. What's a Pipe in this case. What's the need of using a pipe for email parsing?
I can see number of lines here in the code, which ends with a colon, instead of a semicolon. Please clarify. Is the code correct or a typo?
Those should just be on the for and if statements. You can use for(): endfor; or for() { } Matter of coding preference.
If you are trying to get a helpdesk like email parsing code I will suggest you to look at the cerberus helpdesk's email parsing code. Its free and not encrypted. They must also be having the same email parsing setup Thanks!!
Hi, I tested the code number of times, on different environment. In all of the cases PHP5 was installed. But it didn't work. Please give me a solutions.
I need to use this on GoDaddy shared hosting but the function imap_open is deactivated. I need to read gmail and pipe bounced e-mails to a scrip.
Hello, I'm looking to do the same for a comment/reply enviroment, but I need to be able to use stmp or pop, any advice? I'm worried that for my purpouse imap will not be supported. I am not sure why though. adbox
SMTP is for sending email. For email you receive, use imap or pop. "Why" is always that your email host doesn't support it. (Or it supports it in a non-standard way - gmail uses port 993.)