A
A
Alexander2015-06-04 14:34:12
PHP
Alexander, 2015-06-04 14:34:12

How to parse a letter and get its text in php?

Hey! My service sends notifications to clients by mail. In the body of the letter there is a large "RESPOND" button, which sends the client to the site, to the personal account, where clients must write the answer. But, some clients got into the habit of clicking "reply" directly in the mail client - and write a letter with the answer. I tried to write in the body of the letter "this letter was sent automatically, use the button above to reply", but it does not help.
I spied on the experience of AirBnB - they process such scripts, they accept such letters on their script, parse them, get the client's response and automatically write it to themselves in the database. Very cool, I wanted to do the same.
On Habré I read an article on how to do this: habrahabr.ru/post/126448, made. Now I receive the text of the letter in base64 in my script, I decode it. And then a problem arose: how to understand where the text of the client's response ends and the quoting of the letter begins.
Example:

these numbers are busy
Wednesday 3 June 2015 21:20 UTC from SiteName <[email protected]>:
>-------------------------- --------------------------------------------------
>Hello Victoria! We remind you that you have received a new application for accommodation.
>Confirm or cancel it. Do not delay, please, with the answer - the client is waiting for your reaction. Ignoring applications and questions will lead to a decrease in your rating on the site.

And there may also be a signature in the client's letter (--- with uv. Petya) - it does not need to be taken into account.
Maybe there are some ready-made libraries for parsing?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Tetyuev, 2015-06-04
@librown

require_once('rfc822_addresses.php');
require_once('mime_parser.php');
require_once('simple_html_dom.php');
  
$mime = new mime_parser_class;

/* get information specific to this email */
    $overview = imap_fetch_overview($inbox,$email_number,0);
    $message = imap_fetchbody($inbox,$email_number,1);
    
    /* output the email header information */
    $output.= '<div class="toggler '.quoted_printable_decode(($overview[0]->seen ? 'read' : 'unread')).'">';
    if ($mime->Decode(Array('Data' => 'Subject: '.$overview[0]->subject), $decoded)) {
        if ($mime->Analyze($decoded[0], $results)) {
          $msg_subject = $results['Subject'];
        }
      }
    $sub		=	mb_convert_encoding($msg_subject, 'UTF-8', 'KOI8-R');
    $from		=	quoted_printable_decode($overview[0]->from);
    $date		=	quoted_printable_decode($overview[0]->date);
    $Sourse_des	=	$sub;
 		/* output the email body */
    
    $message	=	quoted_printable_decode($message);
    $html		=	str_get_html($message);

To health.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question