Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

encoding - PHP imap problems

I need to be able to use my gmail from a php script. But whatever I try, the message body comes out all crappy with characters like =3D and random equals signs. Sometimes it comes out as base64 or nothing at alls. How can I fetch an email and dispay it in HTMLPurifier clean html or plain text in a pre if no html is available.

$overview = imap_fetch_overview($inbox,$email_number,0);

$body_pre = imap_fetchbody($inbox, $email_number, 2.1); 

$message = imap_fetchbody($inbox, $email_number, 2.2); 

$message = base64_decode($message);

if (empty($message))
 {
  $message = $body_pre;
  $message = preg_replace('@(https?://([-w.]+)+(:d+)?((/[w/_.%-+~]*)?(?S+)?)?)@', '<a href="$1">$1</a>',$message);
$message = '<pre>'.htmlentities($message).'</pre>';  

}else{

 $cleanconfig = HTMLPurifier_Config::createDefault();
 $cleanconfig->set('Core.Encoding', 'UTF-8');
 $cleanconfig->set('HTML.Doctype', 'HTML 4.01 Transitional');
 $purifier = new HTMLPurifier($cleanconfig);

  $message = $purifier->purify($message);

 }

this code, $message just comes blank.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're assuming your mail message is always at position 2.2? There's absolutely no guarantees... depends on what message is sent you plan text (just one body), html/text (two), with attachment (three plus) in reply to another email (then the other email will be a body with it's own sub bodies).

The =3D (quoted printable) and base64 data are because of message body encodings. Have a look at imap_fetchstructure which explains how many parts their are on a message (you can search through it to find text), and review the ->type component of each body to learn about it's encoding (type=4 is quoted printable, type=3 is base64)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...