Facing the issue that when I try to get the body of an email through imap in php, the content of the variable $body is
Jääätzt Kööönmän umläute mit ÄnhÄÄÄng
instead of
Jääätzt Kööönmän umläute mit ÄnhÄÄÄng.
I do not understand where the problem is, all other characters like ?,!,$ are working fine, just german characters like ä,ö are making those problems.
The code-block in php I am using is
$body = imap_fetchbody($mbox, $overview->msgno, 1.2);
if (!strlen($body) > 0) {
$body = imap_fetchbody($mbox, $overview->msgno, 1);
}
Do you have any ideas? I am really desperate.
The Mail you try to show is not in the same encoding as the headers of your website. That's why your browser can't show the special chars properly, like the Umlaut in this case.
You can for example convert them to utf-8 with imap_utf8
$body = imap_fetchbody($mbox, $overview->msgno, 1.2);
if (!strlen($body) > 0) {
$body = imap_fetchbody($mbox, $overview->msgno, 1);
}
$body = imap_utf8($body);
http://php.net/manual/de/function.imap-utf8.php
Related
Im trying to get the actual text message part of the email with:
$body = imap_fetchbody($inbox, $email_number, 1.2);
Problem is I get this:
WXuQFOW1/53v6+6Z2Zmdnd1Z9gW7vHYREHZZtzaA4IOAMQKJmBARKfNWkwjRingtklgatEwuPlG4
RI2liSZKAAvwFRKuIlwVUYw8RJ4iKwu7yz7nuTPd/Z37R8/Mzq6zat2qm1RtV1dRS3d/33d+53fO
+Z0zxMxIXwwGExEYzKwIUEAoEv3w+Nljp86FIjFDlyXBgknVw0dXBKWuESCIAHIWISIM6Uvr9xc7
9jIY4Wji9Xc+
Do I need to do any kind of encoding here?
I had the same problem and I solved by placing this code:
$message = quoted_printable_decode(imap_fetchbody($inbox,$email_number,2));
I'm using imap with php to get the body of my emails.
With what've coded so far, I am not getting the special characters and weird formatting at the end. For instance, if I send myself an email (with a regular email client such as Apple Mail) that says:
Test with characters é à and some ! that rock.
What I get with php is:
Test with characters =C3=A9 =C3=A0 and some ! that rock.=
I've tried re-sending the body through the mail function of php with those headers, but I'm still getting the same problem.
$headers="From: address#email.com" . "\r\n" . "MIME-Version: 1.0" . "\r\n" . "Content-type:text/html;charset=UTF-8";
mail($sendTo, $message, $noBody, $headers);
I've tried addslashes(), which didn't help either.
Sample of my code here:
$imapLink=imap_open("{".$mailbox."}INBOX",$username,$password);
$mailBoxInfos = imap_check($imapLink);
$mailList = imap_fetch_overview($imapLink,"1:".$mailBoxInfos->Nmsgs);
if(isset($mailList))
{
$numMess = imap_num_msg($imapLink);
while($numMess>0) {
$message = imap_body($imapLink, $numMess);
$numMess --;
}
}
$imapClose = imap_close($imapLink);
Hope you can help me with that!
Thanks in advance!
Arthur
The =XX you see in your text is called "Quoted-Printable Content". It's used to display character which can't be displayed in 7-bits.
You can use quoted_printable_decode() to convert it back to a readable string.
So this should work:
$message = imap_body($imapLink, $numMess);
$message = quoted_printable_decode($message); // <-- add this line
$numMess --;
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP imap problems
I need to be able to use my mail (gmail) from a php script. But wherever I try, the email body comes out all rubbbish with characters like =3D and random equals signs. Sometimes it comes in as base64 or nothing at all. How can I get an email and display it in HTML Purifier clean html or plain text in a pre tag 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+)?)?)#', '$1',$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.
The message with "characters like =3D and random equals signs" is quoted-printable encoded. You can decode using php with quoted_printable_decode.
There are different ways how mail messages can be encoded, like quited-printable or base64. The sender decides on the used encoding.
I'm using some PHP imap_functions to automatically process emails.
However the emails have to stay in HTML format and the function imap_fetchbody seems to add '=' as the end of some lines meaning my html parser cannot parse it correctly.
I think it is because FT_INTERNAL needs setting but whatever I try it still does the same.
How do I add a bitmask set of parameters or stop the function from doing this.
$content .= imap_fetchbody($inbox, $email_number, "1", FT_UID & FT_PEEK & FT_INTERNAL);
$content .= imap_fetchbody($inbox, $email_number, "1", 124);
Not a great solution, but it works fine for me:
$body = preg_replace("/=(\r?)\n/", '', imap_fetchbody($mailbox, $message, $part));
Hope it helps in some way
I want to convert the following raw mail subject to normal UTF-8 text:
=?UTF-8?B?UmU6ICMyLUZpbmFsIEFjY2VwdGFuY2UgdGVzdCB3aXRoIG5ldyB0ZXh0IHdpdGggU2xvdg==?=
=?UTF-8?B?YWsgaW50ZXJwdW5jdGlvbnMgIivEvsWhxI3FpcW+w73DocOtw6khxYgi?=
The real text for that is (yes, there are some crazy diacritics in it):
Re: #2-Final Acceptance test with new text with Slovak interpunctions "+ľščťžýáíé!ň"
The way I handle mail subjects:
function subject_imapUtf8($str){
$conv = '';
$subParts = preg_split('/[\r\n]+/',$str);
for($i=0;$i<count($subParts);$i++){
$conv .= imap_utf8(trim($subParts[$i]));
}
return $conv;
}
For the example this gives me:
=?UTF-8?B?UmU6ICMyLUZpbmFsIEFjY2VwdGFuY2UgdGVzdCB3aXRoIG5ldyB0ZXh0IHdpdGggU2xvdg==?=ak interpunctions "+ľščťžýáíé!ň"
So as you can see the second part/line of the subject is converted correclty.
What do I need to change to convert the first part correctly?
mb_internal_encoding("UTF-8");
echo mb_decode_mimeheader($mime);
Demo http://codepad.viper-7.com/a9l4IA