PHP: Decoding of base64 encoded IMAP body - php

PHP -IMAP
I have saved all imap mail to database and displaying them from database. My problem is that one mail is showing "Content-Transfer-Encoding: base64". If i base64 decode it then other mails also get affected. I haven't been able to change it to human readable format till yet. Any help will be highly appreciated.
I have tried many solution but The code currently I am using is,
mb_convert_encoding($body, "ISO-8859-1", "UTF-8");
The output of this mail is
------=_NextPart_001_0606_012497BF.12408D60
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: base64
OTA5MTU5Mzg3DQoNCuS7juaKgOacr+i1sOWQkeeuoeeQhueahOWbm+S4quaguOW/gw0KMjAxMi05
LTE1ICAxNjowMjoxNw==
------=_NextPart_001_0606_012497BF.12408D60
Content-Type: text/html;
charset="utf-8"
Content-Transfer-Encoding: base64
PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv
L0VOIj4NCjxIVE1MPjxIRUFEPg0KPE1FVEEgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PXV0
Zi04IiBodHRwLWVxdWl2PUNvbnRlbnQtVHlwZT4NCjxNRVRBIG5hbWU9R0VORVJBVE9SIGNvbnRl
bnQ9Ik1TSFRNTCA4LjAwLjYwMDEuMTg5OTkiPjwvSEVBRD4NCjxCT0RZPg0KPFA+PEZPTlQgc2l6
ZT0yPjkwOTE1OTM4NzxCUj48QlI+5LuO5oqA5pyv6LWw5ZCR566h55CG55qE5Zub5Liq5qC45b+D
PC9GT05UPjwvUD4NCjxQPjxGT05UIHNpemU9Mj4yMDEyLTktMTUmbmJzcDsgMTY6MDI6MTc8L0ZP
TlQ+PC9QPjwvQk9EWT48L0hUTUw+DQo=
------=_NextPart_001_0606_012497BF.12408D60--

It's base64, you have to use base64_decode on body part like this:
base64_decode('OTA5MTU5Mzg3DQoNCuS7juaKgOacr+i1sOWQkeeuoeeQhueahOWbm+S4quaguOW/gw0KMjAxMi05LTE1ICAxNjowMjoxNw==');

just to manually check there are several online services like
http://www.motobit.com/util/base64-decoder-encoder.asp
just copy and paste the base64 source and choose "decode". You will get a "human readyble format".

Related

PHP imap: how to decode and convert Windows-1252 charset emails?

My PHP app processes incoming emails. The processing code usually works fine, but the app crashed recently with the below exception:
Unexpected encoding - UTF-8 or ASCII was expected (View: /home/customer/www/gonativeguide.com/gng2-core/vendor/laravel/framework/src/Illuminate/Mail/resources/views/html/panel.blade.php) {"exception":"[object] (Facade\\Ignition\\Exceptions\\ViewException(code: 0): Unexpected encoding - UTF-8 or ASCII was expected (View: /home/customer/www/gonativeguide.com/gng2-core/vendor/laravel/framework/src/Illuminate/Mail/resources/views/html/panel.blade.php) at /home/customer/www/gonativeguide.com/gng2-core/vendor/league/commonmark/src/Input/MarkdownInput.php:30)
It seems that there was an incoming email whose text was not properly decoded and this made the app crash later on.
I realized that the email had a Windows-1252 encoding:
Content-Type: text/html; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
The email decoding code looks currently like this:
// DECODE DATA
$data = ($partno)?
imap_fetchbody($mbox,$mid,$partno): // multipart
imap_body($mbox,$mid); // simple
// Any part may be encoded, even plain text messages, so check everything.
if ($p->encoding==4)
$data = quoted_printable_decode($data);
elseif ($p->encoding==3)
$data = base64_decode($data);
I checked this page to understand what I need to change to decode emails with Windows-1252, but it not clear to me which value corresponds to Windows-1252 and how to decode and convert the data to UTF-8. I would highly appreciate any hints, preferably with suggested code on this.
Thanks,
W.
In your case, this line:
$data = quoted_printable_decode($data);
needs to be adapted like this:
$data = mb_convert_encoding(quoted_printable_decode($data), 'UTF-8', 'Windows-1252');
More generally, to cope with non-UTF-8 encodings, you may want to extract the charset of the body part:
from the body part structure, returned by imap_bodystruct(), or
from the body part MIME headers, returned by imap_fetchmime().

php imap not reading body message without attachment

I have written php code for reading email. I am using
imap_fetchbody($this->conn, $i, 1.2);
for displaying email message. When mail is with attachment it is display in well mannered but if email is without attachment it displays nothing. When I am using
$body = imap_fetchbody($this->conn, $i, 1);
message is displaying in text/plain format. like this
this is system generated mail -- Regards! Md Belal
When I am using
$body = imap_body($this->conn, $i);
it is displaying like this
--047d7b5d2f88ddb36c050069a175 Content-Type: text/plain; charset=UTF-8 this is system generated mail -- Regards! Md Belal 8287326270 --047d7b5d2f88ddb36c050069a175 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
this is system generated mail
--
Regards!
Md Belal
--047d7b5d2f88ddb36c050069a175--
I need to display it in well mannered. Like this.
this is system generated mail
--
Regards!
Md Belal
What to do please help. Thanks!
Try this
https://stackoverflow.com/a/14936048/2289510
btw i haven't tried but nl2br (http://hu1.php.net/nl2br) may can help you too, its adding line breaks when your result is in text/plain format and its contains \n \r characters.

PEAR SMTP Behaviour in Windows

I have a strange problem.
I am using PEAR SMTP on Ubuntu. When I send this message:
--==Alternative_Boundary_x{da5ccf044317613d9f8e371455f292a6}x
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
hello world - plain text test
--==Alternative_Boundary_x{da5ccf044317613d9f8e371455f292a6}x
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: 7bit
<i>hello world - html test</i>
--==Alternative_Boundary_x{da5ccf044317613d9f8e371455f292a6}x--
--==Mixed_Boundary_x{e46253d4aad6aff75d74ef5eb064a626}x
Content-Type: application/rtf; name="attachment.rtf"
Content-Disposition: attachment; filename="attachment.rtf"
Content-Transfer-Encoding: base64
e1xydGYxXGFuc2lcZGVmZjB7XGZvbnR0Ymx7XGYwXGZtb2Rlcm5cZnBycTFcZmNoYXJzZXQwIENv
dXJpZXIgTmV3O317XGYxXGZuaWxcZmNoYXJzZXQwIENvdXJpZXIgTmV3O319DQp7XGNvbG9ydGJs
IDtccmVkMFxncmVlbjBcYmx1ZTA7fQ0Ke1wqXGdlbmVyYXRvciBNc2Z0ZWRpdCA1LjQxLjE1LjE1
MTU7fVx2aWV3a2luZDRcdWMxXHBhcmRcY2YxXGxhbmcyMDU3XGYwXGZzMzIgSGVsbG8gd29ybGRc
Y2YwXGYxXGZzMjBccGFyDQpcY2YxXGlcZjBcZnMyNCBIZWxsbyB3b3JsZFxjZjBcaTBcZjFcZnMy
MFxwYXINClxiIEhlbGxvIHdvcmxkXHBhcg0KXGIwXHBhcg0KXHBhcg0KfQ0KAA==
--==Mixed_Boundary_x{e46253d4aad6aff75d74ef5eb064a626}x
--==Mixed_Boundary_x{e46253d4aad6aff75d74ef5eb064a626}x
Content-Type: text/xml; name="attachment.xml"
Content-Disposition: attachment; filename="attachment.xml"
Content-Transfer-Encoding: base64
PD94bWwgdmVyc2lvbj0iMS4wIj8+DQo8YXR0YWNoZW1udD5pbiAueG1sIGZpbGU8L2F0dGFjaGVt
bnQ+IA==
--==Mixed_Boundary_x{e46253d4aad6aff75d74ef5eb064a626}x--
I receive it without one of the --==Mixed_Boundary_x{e46253d4aad6aff75d74ef5eb064a626}x (look where there are two in a row) missing. Some how PEAR smtp send sends the message with that missing.
But if I do it from Windows, using phplib, it sends just fine and causes the attachments to work!
I know I could use the PEAR Mime mail to deal with this, but unfortunately, I am working with legacy code and that is not an option since the message has already had all the encodings.
My question is, why does PEAR remove it? And what does it break the attachments when not there?
Thanks
I can only assume from what you give, but the Ascii-Art in your boundary is really nice, but just probably a little over the top. It contains characters that are not allowed there per the specs, and that are the curly brackets. Maybe because of having an invalid email there, things get dropped?
Got it working! Turns out the extra line was not supposed to be there at all and was causing all sorts of problems.
Managed to change some code so that it was not there and it all started working!

PHP library to generate EML email files?

I'm trying to generate EML files from PHP. Is there any library that will allow me to easily create them? I could find some ActiveX component on the internet but would rather use something more portable.
I ended up building the MIME message myself using this kind of template, where each field is replaced by a TEMPLATE_<name> variable:
From: TEMPLATE_FROM_ADDRESS
MIME-Version: 1.0
To: TEMPLATE_TO_ADDRESS
Subject: TEMPLATE_SUBJECT
Content-Type: multipart/mixed; boundary="080107000800000609090108"
This is a message with multiple parts in MIME format.
--080107000800000609090108
Content-Type: text/plain
TEMPLATE_BODY
--080107000800000609090108
Content-Type: application/octet-stream;name="TEMPLATE_ATTACH_FILENAME"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;filename="TEMPLATE_ATTACH_FILENAME"
TEMPLATE_ATTACH_CONTENT
--080107000800000609090108
Then creating the final message is quite simple using str_replace:
$content = file_get_contents("Template.eml");
$content = str_replace("TEMPLATE_FROM_ADDRESS", $fromEmail, $content);
$content = str_replace("TEMPLATE_TO_ADDRESS", $toEmail, $content);
// etc. for each template parameter
// Also don't forget to base64_encode the attachment content;
$content = str_replace("TEMPLATE_ATTACH_CONTENT", base64_encode($attachContent), $content);
Additional info about file attachment in this post: Attachment name and file extension not working in email *.eml
Edit (2018): Since this answer was written it seems it's been copied and pasted a bit everywhere, the template in particular. To avoid any conflict with other MIME data, you should make sure that the boundary "080107000800000609090108" is unique - it's a string of random characters no longer than 70 characters.
I think you don't need a library. It's just plain text (e.g. http://bitdaddys.com/example1.eml)
Date: Sat, 12 Aug 2006 14:25:25 -0400
From: John Doe <jdoes#someserver.com>
Subject: BitDaddys Software
To: sales#bitdaddys.com
Dear BitDaddys Corp.,
We have added your software to our approved list.
Thank you for your efforts.
Sincerely,
John Doe
Some Server Company
You can just output text with headers and save it using fwrite. For attachments use base64_encode() as stated here
Use imap_savebody (part of the imap library http://us1.php.net/manual/en/function.imap-savebody.php) with a null $part_number. It creates a beautiful .eml file with one line of code with the entire message (null $part_number = all parts... not documented but works).
the other two solutions depend on the format of the email (only one attachment and no html section in the first solution, and only text email in the second).
imap_savebody creates a perfect .eml file no matter what the format of the incoming email is (as long as it's RFC-complaint of course).

Getting IMAP Headers in PHP

Hey Guys,
I am totally confused on PHP's imap functions, they are all over the place and I've dug through comments to get this far. I can do imap_fetchbody($connection, 5, null); and retrieve the entire message which gives me all that i need:
ie:
To: user#gmail.com Content-Type:
multipart/mixed;
boundary=001517570c8a681a5004a36a1fd6
--001517570c8a681a5004a36a1fd6 Content-Type: multipart/alternative;
boundary=001517570c8a681a4804a36a1fd4
--001517570c8a681a4804a36a1fd4 Content-Type: text/plain;
charset=ISO-8859-1
asdf
--001517570c8a681a4804a36a1fd4 Content-Type: text/html;
charset=ISO-8859-1
asdf
--001517570c8a681a4804a36a1fd4--
--001517570c8a681a5004a36a1fd6 Content-Type: image/gif;
name="ajax-loader.gif"
Content-Disposition: attachment;
filename="ajax-loader.gif"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_gnruant80
... bunch of code here omitted ...
But I really want this information in an Array or Object to use it. without having to think up some regular expression to get data out of the entire texts above.
Does anyone know a way to do that or which php imap command to use? Or is there a place with really good examples anyone knows of?
You can use a non null value for section parameter(3rd parameter). Please see second example of http://php.net/manual/en/function.imap-fetchbody.php. I think that is what you are searching.

Categories