PHP: imap_savebody() breaks long lines - php

I attempt to parse an email message whose body is in csv format.
The email supposedly has this content:
,111111111111111,Abcde Abcd,,abcde#abcdefghi.com.au,61352722827,13/04/2012,14/04/2011,Abcd Abcdefghi,
,111111111111111,Abcde Abcd,,abcde#abcdefghi.com.au,61352722827,13/04/2012,14/04/2011,Abcd Abcdefghi,
But imap_savebody() saves it like this:
,111111111111111,Abcde
Abcd,,abcde#abcdefghi.com.au,61352722827,13/04/2012,14/04/2011,Abcd
Abcdefghi,
,111111111111111,Abcde
Abcd,,abcde#abcdefghi.com.au,61352722827,13/04/2012,14/04/2011,Abcd
Abcdefghi,
The encoding is 7BIT as outputted by imap_fetchstructure. Anyone knows why this happens?

Related

PHP Invalid quoted-printable sequence, malformed q encoding from Yahoo

I came across the following error in PHP generated by an email forwarded from a Yahoo account:
Notice: Unknown: Invalid quoted-printable sequence: =?UTF-8?Q?ck-off with Weekly Sale up to 90% off (errflg=3) in Unknown on line 0
I've spent hours researching this issue and decided to send myself the exact same output string in an email without having Yahoo involved. The original q-encoded text that decodes correctly:
=?UTF-8?Q?GOG_Forward=3A_Fw=3A_=F0=9F=98=89_A_great_Monday_kick-?= =?UTF-8?Q?off_with_Weekly_Sale_up_to_90=25_off?=
The malformed q-encoded text from Yahoo:
=?UTF-8?Q?GOG_Forward =?UTF-8?Q?ck-off_with_Weekly_Sale_up_to_90%_off?=
The correct string when decoded:
GOG Forward: Fw: 😉 A great Monday kick-off with Weekly Sale up to 90% off
Roundcube manages to decode both the normal and the malformed text though I'm not sure how and 25 megabytes is a bit much to dig through and I haven't been able to determine even where they're decoding subject headers.
How do I fix Yahoo's malformed version of q-encoding?
<?php
//These fail:
echo imap_mime_header_decode($mail_message_headers['Subject']);
echo quoted_printable_decode($mail_message_headers['Subject']);
?>
For clarification the imap_fetchstructure page clarifies the value 4 for encoding is Quoted-Printable / ENCQUOTEDPRINTABLE.
New Development
It turns out that for some reason Yahoo sends the subject twice for the same header, one malformed and the other is not. Here is the Subject header from the raw email:
Subject: =?UTF-8?Q?GOG_Forward:_Fw:_=F0=9F=98=89_A_great_Monday_ki?=
=?UTF-8?Q?ck-off_with_Weekly_Sale_up_to_90%_off?=
MIME-Version: 1.0
I created a solution that uses Roundcube's source code to decode the message.
I posted the code and demo:
You can see it here
Click the big play button to preview the extraction
Go to code tab to see the extracted Roundcube code that you could use for your project
Since you mentioned to not use classes in the example I extracted Roundcube's decode_mime_string() function from rube_mime, and a couple of things from rcube_charset such as $aliases, parse_charset(), and convert().
As far as decoding the malformed text from Yahoo:
=?UTF-8?Q?GOG_Forward =?UTF-8?Q?ck-off_with_Weekly_Sale_up_to_90%_off?=
Into this:
GOG Forward: Fw: 😉 A great Monday kick-off with Weekly Sale up to 90% off
It's impossible. There's not enough data in there. For example it's missing the "😉 A great Monday ki". Do you have the full source of the email address?

Extracting Body of email

I am trying to extract the message body of an encoded email. Everything worked fine for many a year but now there are some extra headings being included which have put a spanner in the works
To follow is the end of the email headers and the start of the message body itself.
When I view the email source it shows a blank line between the two sections, but when I try and split it by this, it does not find it
If I split by the first 5 characters of the message body, it actually ignores the blank line and THE line prior to that ( YamCpMIyU+au/dWzSGjp0w9hpHu/m/vs8HM=)
I am utterly confused by this and am reaching out for any advice you can give.
Content-Transfer-Encoding: base64
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io;
q=dns/txt; s=20140610; t=1551842112;
bh=QhnqlnG4IESh6eMyHbR+KrM4N9LZk0XPpXuFqpHah2U=;
h=Content-Type:Date:From:Reply-To:Subject:To;
b=DRGclGLkyYq+uYoipKgl7d7CTB3Z8MQ/SVEiJe5KwiW91BuPTXRwnTaAb9AjTa+xbxC
1QYGocd8r8ZD2v9JRdlqLWTb9m9M91nRhO8tsbBbVK7VofmOmzYzHpVEfdQMJBo/jbth8
YamCpMIyU+au/dWzSGjp0w9hpHu/m/vs8HM=
UGxhbmVQbG90dGVyIExvZyBmcm9tIE1hY2Fww6EsIEFtYXDDoSAtIEJSIGZvciAwNS8wMy8yMDE5
IFRpbWVzIGFyZSBVVEMNCkxvY2F0aW9uOiBNYWNhcMOhLCBBbWFww6EgLSBCUiwgQXV0aG9yOiBG
YWJpYW5vIEZlcnJlaXJhLCBSZWNlaXZlcjogIFNCTVENCg0KQ3JlYXRlZCB3aXRoIE5pYyBTdG9y
ZXlzIFBsYW5lUGxvdHRlciBSZXBvcnQgVmlld2VyIFZlciAyLjMgDQpEb3dubG9hZCBGcm9tOiBo
Many thanks
Alexis

PHP: how to target base64 encoded messages?

I have raw encoded base64 emails that I would like to decode. However on the raw email data, there is 3 sections to it.
The first section is HEADERS.
The second section is HTML content / tags.
The third section is the encoded base64.
I can manually add the string to my program and decode it just fine. However, I want to TARGET or PULL the encoded message into my program, so when I run it, its automatic.
But how do I target the encryption data when the header and html content / tags are in the way? I can read files through PHP, but would I do something like
if (strlength IS REALLY LONG == encoded data)
{decode that data}
Based on the following link, your solution would be something like this:
How to check whether the string is base64 encoded or not
$regex = '^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$';
if(preg_match($regex,$string) != false){
//some operation
$decoded = base64_decode($string);
}

Line Breaks in OpenPGP (Using PEAR with drupal)

I am encrypting form submissions in drupal with Pear. The encryption//decryption is working fine, but line breaks are not working.
What I mean by that is, whether I use Enigma in thunderbird, or I use gpg4win, the message gets decrypted, but it looks like this:
Online Form = 1\r\nFirst Name: John\r\nLast Name: Smith\r\n
I have tried \n\n \r \r\n none of these seem to work.
So the question is how do I get line breaks to output properly after being decrypted.
(I'm using the drupal mail function sending plain text email with UTF-8 encoding, although I don't think this matters since the message is being decrypted)

PHP _EOL not working when input to $message of mail() function

I'm using PHP _EOL when building the message body of my email but the line feeds are not getting through and the entire message body ends up one long line in the resultant email. This happens regardless of Multi-part or html only messages. Sending as text only it works fine, but of course I want to send Multi-part messages.... Any ideas?
Uhm. If there are no line breaks in your HTML email, it's probably because neither a \n nor a \r\n is a newline in HTML; a <br /> tag is.
I've never even used PHP_EOL before, but I wonder if it is set to the type of your server, not of the recipient. I don't see how a constant could be correct for all recipients, that doesn't make sense.
Usually '\n' is all that is needed... in some cases you may need '\r\n' depending on the protocol involved. What are you using to send the email? What are you using to view the email?

Categories