I am pulling down an email which has english, chinese and japanese in the email.
I was using PHP/EZComponents to do this, but a certain japanese char was just not coming through so I am switching to php imap_* funcs to see if they will work.
This is what I have below, and the output I am getting. I need to decode this somehow... I know this has been well (read:overly/chaotically) documented all over the web, but I dont have time to earn a PHD in this right now. Any help is greatly appreciated.
$hn='{imap.gmail.com:993/imap/ssl}INBOX';
$inbox = imap_open($hn,$username,$password,CL_EXPUNGE);
foreach($emails as $email_number) {
$ov = imap_fetch_overview($inbox,$email_number,0);
$msg = imap_fetchbody($inbox,$email_number,2);
var_dump($msg);
// doesnt work... .. but right idea?
// var_dump( utf8_decode($msg) );
}
PARTIAL OUTPUT:
<font face=3D"Arial"><span lang=3D"EN-US" style=3D"font-size:10.5pt"><br></=
span></font><font color=3D"navy" face=3D"MS Gothic"><span lang=3D"JA" style=
=3D"font-size:10.5pt">=CC=EC=9A=DD=A4=AC=A4=A4=A4=A4=A4=AB=A4=E9=A1=A2</spa=
n></font></p><p style=3D"margin-right:0pt;margin-bottom:12pt;margin-left:0p=
t">
<font color=3D"navy" face=3D"MS Gothic"><span lang=3D"JA" style=3D"font-siz=
e:10.5pt"><br></span></font></p><p style=3D"margin-right:0pt;margin-bottom:=
12pt;margin-left:0pt"><font color=3D"navy" face=3D"MS Gothic"><span lang=3D=
"JA" style=3D"font-size:10.5pt">xxend</span></font></p>
I also met this problem employed imap_fetchbody function to get the mail body.
I found that the string get from imap_fetchbody was converted to quoted-printable string automatically.
I resolved this issue by using imap_qprint function to convert the fetched string body into correct one.
Related
I've written a WordPress plugin which sends out new post notifications. There is a setting to convert subject lines from html entites to quoted-printable so they'll display in UTF-8 on any email client. A few weeks ago I started getting reports that the quoted-printable subject line was being kept as-is instead of being decoded.
Sample Subject header:
Subject: =?UTF-8?Q?[Pranamanasyoga]=20Foro=20Pranamanasyoga=20:=20estr?= =?UTF-8?Q?=C3=A9s=20y=20resilencia?=
I cannot replicate it locally and have not been able to find any common denominators between reporters.
The code that generates the quoted-printable line is this:
<?php
$enc = iconv_get_encoding( 'internal_encoding' ); // this is UTF-8
$preferences = ['input-charset' => $enc, 'output-charset' => "UTF-8", 'scheme' => 'Q' ];
$filtered_subject = '[Pranamanasyoga] Foro Pranamanasyoga : estrés y resilencia';
$encoded = iconv_mime_encode( 'Subject', html_entity_decode( $filtered_subject ), $preferences );
$encoded = substr( $encoded, strlen( 'Subject: ' ) );
If I try decoding it, it works fine:
$decoded = iconv_mime_decode($encoded, 0, "UTF-8");
var_dump(['encoded' => $encoded, 'decoded' => $decoded])."\n";
Result:
array(2) {
["encoded"]=>
string(102) "=?UTF-8?Q?[Pranamanasyoga]=20Foro=20Pranamanasyoga=20:=20estr?=
=?UTF-8?Q?=C3=A9s=20y=20resilencia?="
["decoded"]=>
string(59) "[Pranamanasyoga] Foro Pranamanasyoga : estrés y resilencia"
}
One thing I noticed, but think is not related is that my code actually adds a newline before the second =?UTF-8?Q? piece and the email subject header does not have it. Decoding the strings with- and without the newline works the same.
Does anyone have ideas/suggestions on what may be causing the email clients (Gmail included) to display the string as-is, instead of decoding it to UTF-8?
P.S. While writing this I saw a suggestion to use mb_encode_mimeheader() in a different thread. It seems to work well with iconv_mime_decode() in my test code, but the output string is indeed different from the original one:
[Pranamanasyoga] Foro Pranamanasyoga : =?UTF-8?Q?estr=C3=A9s=20y=20resile?=
=?UTF-8?Q?ncia?=
Could it be that email clients would prefer this format over the original one?
I'm trying to parse an email that is partially written in English and partially written in Japanese using PHP. Here's the code I have so far:
if ($mbox = imap_open($mailbox, $username, $password)) {
$inbox = imap_check($mbox);
$total = (int) $inbox->Nmsgs;
$min = max(1, $total - 10);
// fetch 10 most recent emails
if ($total > 0 && ($emails = imap_fetch_overview($mbox, "{$min}:{$total}"))) {
foreach ($emails as $email) {
$body = imap_body($mbox, $email->uid, FT_UID);
// for testing purposes
echo $body;
// parsing logic here
}
}
}
So as you can see I'm just echoing out the plaintext body of the email, just to test things, and here's part of the echoed response I get when I run this script:
Your Japanese word of the day is: ç”·ã®å
However, the following is what I should see in place of that, based on what's actually in the email:
Your Japanese word of the day is: 男の子
So clearly something's being lost in translation (pun intended). I'm using some simple string manipulation to try and parse the Japanese characters, and then insert them into a MySQL database. However, it's currently inserting those gobbledygook characters instead. What am I missing?
Edit: I'm using PHP version 5.4.45
So I'm not really sure how to get this working in PHP 5. I decided to try upgrading my server to PHP 7 and suddenly all of the code just started working.
I'm using a simple MQTT client in PHP - https://github.com/sskaje/mqtt - and want to retrieve exactly one message (which is retained - always there) from broker, and then display it on a page. Everything works good, but I cannot get it to display the whole page. It displays "Test Text 1", then debugging code, then my message, and stops there, not showing "Test Text 2" nor "Test Text 3". If anyone would help me, I would be incredibly grateful, as I have no idea at all, what does not work, and have spent a lot of time working on it. Thanks!
Test Text 1
<?php
require('spMQTT.class.php');
$mqtt = new spMQTT('tcp://127.0.0.1:1883/');
spMQTTDebug::Enable();
$mqtt->setKeepalive(5);
$connected = $mqtt->connect();
if (!$connected) {
die("Not connected\n");
}
$topics['#'] = 0;
$mqtt->subscribe($topics);
$mqtt->loop('default_subscribe_callback');
$mqtt->unsubscribe(array_keys($topics));
printf("Test Text 2");
/**
* #param spMQTT $mqtt
* #param string $topic
* #param string $message
*/
function default_subscribe_callback($mqtt, $topic, $message) {
printf("Message received: Topic=%s, Message=%s\n", $topic, $message);
break;
}
?>
Test Text 3
I'm not familiar with that mqtt library, but it looks very much like the call to loop() is blocking, so you will have to approach the problem differently.
I have a code to search though emails & if particular string is found, stuff will be done. Below is my code:
$date = date( "d-M-Y", strToTime( "-1 days" ));
$email=imap_search($inbox, 'SUBJECT "Undelivered Mail Returned to Sender" SINCE
"'.$date.'"', SE_UID);
if($email){
foreach($email as $email_number) {
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,1); /* This is Line 18 */
This code gives me following error:
Warning: imap_fetchbody() [function.imap-fetchbody]: Bad message number in
/home/public_html/example.com/check_invalid_email.php on line 18
Now on the above code if I change the subject only like this:
$email=imap_search($inbox, 'SUBJECT "Mail delivery failed" SINCE
"'.$date.'"', SE_UID);
It works like a charm with this. For your information there are emails with both the subjects.
I've not included my whole code, please consider everything is alright as changing one line gives me desired results.
Any help with this? I'm not able to find out the cause of it.
i think you have to use it like this as you are getting the key,value array
foreach($email as $key=>$email_number) {
I am trying to fetch a particular section of an email body using:
$message = imap_fetchbody ($imap_stream,$n,1);
This works fine for emails sent using Outlook, Hotmail, Yahoo etc. But when I try and fetch the body of the an email sent from a Blackberry or Andriod device, the message is encoded and scrambled such as:
xmb250IHN0eWxlPSdjb2xvcjojRjk3MWIxMDNiMTEyYjExOGI0N2I1MWI1
Although the body is encoded, the header is fine. How do I decode the message body of en email sent from an Android or Blackberry device?
Thank you,
Chris.
You should be able to run imap_fetchstructure to get the encoded value. If that value equals 3 you need to decode via imap_base64.
I've used the following before (Can't remember if it was ever tested with sent mobile email before though):
$structure = imap_fetchstructure($stream, $msgno);
$text = imap_fetchbody($stream, $msgno, $partnum);
if($structure->encoding == 3) {
$text = imap_base64($text);
} else if($structure->encoding == 4) {
$text = imap_qprint($text);
}