I need an Email Parser Script To Parse Emails using PHP

Discussion in 'PHP' started by techbongo, Jul 24, 2009.

  1. #1
    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?
     
    Last edited: Jul 24, 2009
    techbongo, Jul 24, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    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:
     
    jestep, Jul 24, 2009 IP
  3. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #3
    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?
     
    techbongo, Jul 25, 2009 IP
  4. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #4
    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?
     
    techbongo, Jul 25, 2009 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    Those should just be on the for and if statements.

    You can use

    for(): endfor;

    or

    for()
    {

    }

    Matter of coding preference.
     
    jestep, Jul 25, 2009 IP
  6. HivelocityDD

    HivelocityDD Peon

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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!!
     
    HivelocityDD, Jul 25, 2009 IP
  7. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #7
    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.
     
    techbongo, Jul 31, 2009 IP
  8. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #8
    Can somebody please help? I really need this help.
     
    techbongo, Jul 28, 2010 IP
  9. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #9
    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.
     
    Fracisc, Aug 15, 2010 IP
  10. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #10
    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
     
    adbox, Nov 27, 2012 IP
  11. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #11
    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.)
     
    Rukbat, Nov 27, 2012 IP
  12. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #12
    Thank you!
     
    adbox, Nov 27, 2012 IP