简体   繁体   中英

How to remove extra non-english characters from email message?

I am using the following script to read emails.

<?php
   //Fetch users from table 
   $sql_users=mysql_query("select  userName, realpass,userID  from users ");

   while($row = mysql_fetch_assoc($sql_users)){
       $username=$row['userName'].'@example.com'; // Email address is username@domain.com 
       $password=$row['realpass'];
       $hostname = '{example.com:995/pop3/ssl/novalidate-cert}'; 
       $username = $username; $password = $password; 
       $imap = imap_open($hostname,$username,$password) or die('Cannot connect: ' . imap_last_error());

       for ($i = 1; $i <= $message_count; ++$i){
           $header = imap_header($imap, $i);
           // fetch message body and mark it as read
       $body = imap_fetchbody($imap, $i,2,FT_PEEK);
           $prettydate = date("jS F Y", $header->udate);

       if (isset($header->from[0]->personal)) {
               $personal = $header->from[0]->personal;
           } else {
               $personal = $header->from[0]->mailbox;
           }
           $subject=$header->Subject;
           //display email content   
           $email = "$personal <{$header->from[0]->mailbox}@{$header->from[0]->host}>";
           echo "On $prettydate, $email said \"$body\".\n";
           echo '<br><br>';
   }
       print_r(imap_errors());
       imap_close($imap);
}

The problem is the email message returns from extra characters with it which need to be removed. Also I need to mark the emails as read. Here is a sample message:

" On 20th March 2013, Joe said "email prayer content.

This =A0is a test email for example.com. It should be converted into an= ew prayer request.

Thanks, Joe ". "

In the PHP reference, there is a comment with a similar issue to yours. In that comment he reads the content this way:

 $text = trim( utf8_encode( quoted_printable_decode( 
                    imap_fetchbody( $this->inbox, $emailNo, $section ) ) ) );

Try adjusting that example to your code and give it a try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM