PHP Classes
elePHPant
Icontem

Retrieve email from gmail with imap: Retrieve mail and decode correctly

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  All requests RSS feed  >  Retrieve email from gmail with imap  >  Request new recommendation  >  A request is featured when there is no good recommended package on the site when it is posted. Featured requests  >  No recommendations No recommendations  

Retrieve email from gmail with imap

Edit

Picture of Syd Chino Barrett by Syd Chino Barrett - 3 months ago (2015-11-09)

Retrieve mail and decode correctly

This request is clear and relevant.
This request is not clear or is not relevant.

+3

I do the connection, I can grab the emails. but they are illegible. Maybe I have problem with imap_fetch_body. Any ideas?

  • 2 Clarification requests
  • 2. Picture of Manuel Lemos by Manuel Lemos - 3 months ago (2015-11-09) Reply

    DO you want to decode the retrieved messages so they can be rendered in a way the user understands?

    • 3. Picture of Dave Smith by Dave Smith - 3 months ago (2015-11-09) in reply to comment 2 by Manuel Lemos Comment

      There is a known issue with imap, and this is probably what he is running into. According to the request, everything is in place, he is just is getting illegible content back, so instead of answering a technical question in a package recommendation section, it makes more sense to me to answer the question in the section where it would do most users the most good. But, that may just be me.

    • 3. Picture of Dave Smith by Dave Smith - 3 months ago (2015-11-09) in reply to comment 2 by Manuel Lemos Comment

      There is a known issue with imap, and this is probably what he is running into. According to the request, everything is in place, he is just is getting illegible content back, so instead of answering a technical question in a package recommendation section, it makes more sense to me to answer the question in the section where it would do most users the most good. But, that may just be me.

    • 4. Picture of Manuel Lemos by Manuel Lemos - 3 months ago (2015-11-10) in reply to comment 3 by Dave Smith Comment

      I am not aware of that issue. What is it?

      What I know is that imap extension was considered to be removed from PHP core because the base C library is not maintained.

      When he says illegible, it crosses my mind that the message parts may be base64 encoded even for text or HTML parts. That is why I asked if he wanted to decode the messages.

    • 5. Picture of Syd Chino Barrett by Syd Chino Barrett - 3 months ago (2015-11-10) in reply to comment 4 by Manuel Lemos Comment

      Exactly. There is a problem with encode/decode. Problems with special char. The worse thing is the image, converted in text. BUT, at the end I used the options of imap_fetchbody

      0 - Message header 1 - MULTIPART/ALTERNATIVE 1.1 - TEXT/PLAIN 1.2 - TEXT/HTML 2 - MESSAGE/RFC822 (entire attached message) 2.0 - Attached message header 2.1 - TEXT/PLAIN 2.2 - TEXT/HTML 2.3 - file.ext

      with the 1 option, I have plain text and for now is good. My problem was the insertion in a SQL Table with all strange charachters.

      I did a simple page.

      $oConn=new SQL();

      if($emails) {

      $output = '';
      

      rsort($emails);
      

      foreach($emails as $email_number) 
      {
          $header=imap_headerinfo($inbox,$email_number);
      

          $from = $header->from[0]->mailbox . "@" . $header->from[0]->host;
          $toaddress=$header->toaddress;
          $replyto=$header->reply_to[0]->mailbox."@".$header->reply_to[0]->host;
          $datetime=date("Y-m-d H:i:s",$header->udate);
          $subject=$header->subject;
      

          //remove the " from the $toaddress
          $toaddress = str_replace('"','',$toaddress);
      

          // echo '<strong>A:</strong> '.$toaddress.'<br>';
          // echo '<strong>Da:</strong> '.$from.'<br>';
          // echo '<strong>Oggetto:</strong> '.$subject.'<br>';
          // echo '<strong>Data:</strong> '.$datetime.'<br>';
      

          $message = (imap_fetchbody($inbox,$email_number,1));
      	
      	/*0 - Message header
      	1 - MULTIPART/ALTERNATIVE
      	1.1 - TEXT/PLAIN
      	1.2 - TEXT/HTML
      	2 - MESSAGE/RFC822 (entire attached message)
      	2.0 - Attached message header
      	2.1 - TEXT/PLAIN
      	2.2 - TEXT/HTML
      	2.3 - file.ext*/
      	
      	
          if($message == '')
          {
              $message = (imap_fetchbody($inbox,$email_number,1));
          }
      	
      	$ett = between('ETT :', 'Telefonico :', $message);
      	$telefonico = between('Telefonico :', 'Nome cliente :', $message );
      	$cliente  = between('Nome cliente :', 'Recapito :', $message );
      	$recapito  = between('Recapito :', 'indirizzo :', $message );
      	$indirizzo  = between('indirizzo :', 'Note :', $message );
      	$note  = after('Note :', $message );
      	
      	//echo "$ett<br>";
      	//echo "$telefonico<br>";
      	//echo "$cliente<br>";
      	//echo "$recapito<br>";		
      	//echo "$indirizzo<br>";
      	//echo "$note<br>";
      	
      	
      	$query  = " INSERT INTO Onfield.dbo.Imap ( EmailTo, EmailFrom, Subject, message, ETT, Telefono, Cliente, Recapito, Indirizzo, Note) VALUES ( '".$toaddress."', '".$from."', '".$subject."', '".$message."', '".$ett."',	'".$telefonico."',	'".$cliente."','".$recapito."','".$indirizzo."', '".$note."' ) " ;
      	
      	$oConn->Query($query);
      	
      	//echo $message.'<br>' ;
      	//echo $query;	
       
      

      } }

      between and after are functions.

  • 1. Picture of Dave Smith by Dave Smith - 3 months ago (2015-11-09) Reply

    I would post your question in the php specialists forum, you are more likely to get an answer there.

    Ask clarification

    1 Recommendation

    MIME E-mail message parser: Decode MIME e-mail messages

    This recommendation solves the problem.
    This recommendation does not solve the problem.

    +4

    Picture of Manuel Lemos by Manuel Lemos package author package author Reputation 11100 - 3 months ago (2015-11-10) Comment

    I think imap_fetch_body does not decode the messages. It may be encoded with quoted-printable or base64.

    Use this class and pass the whole message with headers and all parts, so it decodes each part for you.

    The class is tolerant to malformed messages. So if the sender sent a message with encoding mistakes, the class still parses the message as much as possible.

    • 1 Comment
    • 1. Picture of Syd Chino Barrett by Syd Chino Barrett - 3 months ago (2015-11-11) Reply

      Thank you. I will try as soon as I have little time. I was testing the Imap function with "classic" mail, instead fortunately in my mailbox I will receive preformatted mail with Subject, and six information (Ett, telefonico, cliente, etc.), so I need just plain text for retrieving this information. Thank for your support, surely I will study your package.

      Have a nice day.


    Recommend package
    : 
    :