Extracting Body of email - php

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

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?

PHP send an appointment to Lotus Notes, dolar char in headers

I'm trying to send an appointment to Lotus Notes, I am able to send something but not all headers are consider in my mail. At first I found that it is not allowed to use '' in header name so I found that if I will use '-' instead of '' the server will send the email with the '_' at the end. This I had to use in case when I want to set header '_ViewIcon'.
Now I have another problem. Some Lotus headers start with dolar, like '$Abstract' ... But when I set this header using PHPMailer I will get on the Lotus side instead of $Abstract the $24Abstract.
So I can to do if these chars are not allowed for RFC 822 ? How can I send a correct appointment to Lotus Notes.
With PHP Mailer I'm using these headers:
$mail->AddCustomHeader("\$PublicAccess: 1");
$mail->AddCustomHeader("-ViewIcon: 148");
But in the Lotus when I open detail of email I can see:
$24PublicAccess.
Solution:
I find out the solution which is helpful for me. The special characters like $,_,-,... are not allowed only for header name. So I send from Lotus Notes appointment to my GMail and then I open the source of mail from GMail. I found many Lotus headers with this format:
X-Lotus-Item: 1; name=$PublicAccess; type=300
// and type=300 means number I think
So when I rewrite all my headers into this format, it's working now!
I can setup all headers which contains $ or any other special character.
try with simple quote and without a backslash
$mail->AddCustomHeader('$PublicAccess: 1');

PHP: imap_savebody() breaks long lines

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?

separate email content

I am trying to separate contents within an incoming email.
Whenever the user receives and email with a marker line (### Reply above this line to append to the ticket ###) and if he clicks reply to this email the reply area starts with
On Apr 22, 2010, at 11:22 AM, Test wrote:
Reply above this line to append to the ticket ###
Now when PHP script find the string (### Reply above this line to append to the ticket ###) and cut the mail into two parts and uses the first one which has in this example On Apr 22, 2010, at 11:22 AM, Test wrote: along with unclosed < blockquote > and < div > tags.
How can i remove the content in such a manner where i can use the content above the "On Apr 22, 2010, at 11:22 AM, Test wrote:" line.
Any help, anyone who has tried this earlier.
thank you.
Your question is not very clear.
Do you mean that the initial 3 hashes are stripped when the user clicks on reply? This implies that the users MUA is broken - when I click on 'Reply' I expect it to quote the original message unmolested. Regardless the solution is just to split the message on 'Reply above this line to append to the ticket ###' instead of '### Reply above this line to append to the ticket ###'
Or is this just an issue with SO's formatting of your message?
Do you mean that when you cut off the first part, the HTML is badly formed? If so, just use a regex to strip out any HTML tags (the built in PHP strip_tags function might work too).
C.

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