How to decode utf-8 from imap gmail - php

I get this text from imap
=?UTF-8?Q?Ho=C3=A0ng_V=C5=A9_H=C3=A0?=
When I tried with imap_urf8($string) and I got
Hoà€ng Vùƒ Hà€
When I tried with utf8_decode(imap_urf8($string)) and I got
Hoa?ng Vu? Ha?
I want to get
Hoàng Vũ Hà
How can I do it?

Try to use the search function. This is RFC 2047 encoding, and there's plenty of questions on how to decode that already here on SO.

Related

Nanopb - decode with PHP

I have a message encoded with Nanopb implementation of Google Protocol Buffers.
I have to decode it to display the decoded result on a php page.
Is it possible to do it with PHP ?
If not, what is the best solution to do it in order to get an exploitable result in php ?
Thanks for your help.
Use php implementation of google protocol buffers like for instance:
google/protobuf/php
protobuf-php

PHP: Converting \xc3\xa4 to ä

I'm trying to correct an encoding error.
For example, a string which should read "Morgan Pålsson - världsreporter" has been encoded as "Morgan P\xc3\xa5lsson - v\xc3\xa4rldsreporter".
How do I convert "\xc3\xa5" back to "å" and "\xc3\xa4" back to "ä"?
I've tried combinations of various encode/decode functions and iconv, but no luck.
This seems like it should be straightforward. Any ideas?
We had this problem when decoding strings coming from Salesforce via the SOAP interface. Our solution to this "\xc3\xa4" problem looks really weird, but it works. Note that this is a Python solution, but maybe you can apply this to PHP as well! :)
decoded_string=encoded_string.encode('raw_unicode_escape').decode('unicode_escape').encode('latin1').decode('utf-8')

Is there a way to change the encoding of the headers in SwiftMailer?

I'm using SwiftMailer to send emails but I have some codification problems with UTF-8 subjects. Swiftmailer uses QPHeaderEncoder as default to encode email headers and the safeMap looks like it has some problems with some UTF-8 French characters. One subject I use contains the word trouvé (found in French) and when the subject gets to the user it shows trouv.
I'd like to use something similar to the NativeQPContentEncoder that's available as content encoders but for headers there's only Base64 and Quoted Printable encoders.
Is there a way to fix this, maybe I'm doing something wrong so I paste the code I'm using here
$message = Swift_Message::newInstance()
// set encoding in 8 bit
->setEncoder(Swift_Encoding::get8BitEncoding())
// Give the message a subject
->setSubject($subject)
// Set the From address with an associative array
->setFrom(array($from => $niceFrom))
// Set the To addresses with an associative array
->setTo(array($to)) ;
Check if in your PHP configuration mbstring.func_overload option has any value other than 0. If yes, change it to 0, reload your webserver and try to send message again.
mbstring.func_overload overrides some string PHP functions and may lead to tricky bugs with UTF-8.
Personally I solved exactly this problem by disabling mbstring.func_overload.
First, make sure you know how is your subject string encoded. If it is not UTF-8 then utf8_encode() it.
Also, make sure you setCharset('utf-8') your message.

how to parse email header in php?

I will be passing string containing raw email in which i want to parse headers. Might be Subject, text/html or attachments etc. Please help me on this.
There's other libraries too, but this was my first hit on google.
http://code.google.com/p/php-mime-mail-parser/
You will either needs to find the input in $_SERVER['argv'] or use fopen('php://input', 'r')
this can all be done with the php imap functions
Just use php's regex functions.

ASCII-characters instead of Swedish chars?

I have tested PHP's IMAP lib. to fetch emails from a GMAIL account, but I've just can't get my head around trying to make the characters to display correctly.
At first, I was close to pull my hair off when I realized that I accidentally fetched the attachments instead of the message body - not good, but now when that is solved, I still have problems viewing the actual messages with appropriate Swedish characters, like åÅ äÄ öÖ which instead appear as their ASCII-cousins; =E4, =E5 - and so on.
What is the appropriate way to solve this? I've tested all encoding functions that I can think of by myself - and it won't work...
Thanks!
Not 100% sure, but it seems to me that the content of the message is quoted-printable encoded. Try quoted_printable_decode - http://www.php.net/manual/en/function.quoted-printable-decode.php
If you are already using the IMAP extension, you can also try imap_qprint - http://www.php.net/manual/en/function.imap-qprint.php
Try this
function fixEncoding($in_str) {
$cur_encoding = mb_detect_encoding($in_str) ;
if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))
return $in_str;
else
return utf8_encode($in_str);
}

Categories