separate email content - php

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.

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

RegEx for string replacement in MySQL Database

I have recently upgrade an IPB version of my forum but the quotes were not upgraded and IPS is not giving the support I need.
I need to build a regular expression do find and replace
For example this is the old forum format:
<div class="quotetop">QUOTE(Cleber__v # Apr 14 2015, 12:25 PM)
<a href="index.php?act=findpost&pid=2778161"><{POST_SNAPBACK}>
</a></div><div class="quotemain"><!--quotec-->
TEXT TO BE KEPT
<!--QuoteEnd-->
</div><!--QuoteEEnd-->
And this is the new format it should be on:
`<div>
<blockquote class="ipsQuote"
data-cite="Em 14/04/2015, (Cleber__v disse:" data-ipsquote=""
data-ipsquote-timestamp="1428004301" data-ipsquote-userid="2350"
data-ipsquote-username="Cleber__v" data-ipsquote-contapp="forums"
data-ipsquote-contenttype="forums" data-ipsquote-contentclass="forums_Topic"
data-ipsquote-contentid="105179" data-ipsquote-contentcommentid="2768819">
TEXT TO BE INSERTED</blockquote></div><p><span>​</span></p>`
So I need to find the content, save Username, postID (or commentid), time and text and replace it with the correct format.
I've been researching regex for about a week now with no sucess on how to make this happen
Anybody could help? Thank you
You should try for youself and give us the regex you tried, but here is a first step for you :
.*?>QUOTE\((?P<name>.*)\ \#\ (?P<date>[^\)]+).*?pid\=(?P<pid>[0-9]*).*?\<\!\-\-quotec\-\-\>(?P<text>.*?)\<\!\-\-QuoteEnd\-\-\>
See here how it works : https://regex101.com/r/dM0eG3/1 and how the match information corresponds to your need.
NB : you must remove all new line characters before applying this regex, but this is fairly easy to do in PHP or in any language you might use to create your db upgrade script.
That will extract all the relevant information from your text. Replacing these in the new format is left as an exercise for the reader.

Separating a list of labels and values from a body of text with preg_match_all

I'm setting up a PHP script that will have emails piped to it from a maintenance help desk. These emails are sent from a web form used by our client company, which I have no control over. The emails are standarised in format but contain a list with labels that is fed from the web form. I want to use regular expressions to split out this list and put the labels and values into an array, which I can feed into my own database. I have got a working solution on the go but I'm very new at regex and I'm sure there is a better / more efficient way to do it.
An example of an email that I may recieve:
Dear *MY COMPANY*,
A new job has been raised, please see details below.
If you are unable to action this job request, please notify the Maintenance Help Desk on xxx-xxxx as soon as possible.
Job Type: Man In Van
Job Code: 1462399
Due Date: 27/09/2012 07:21:10
Response Time: Man In Van
Pub Number: 234
Pub Name: pub name, location
Pub Address: 123 somewhere, some place XX1 7XX
Pub Post Code: XX1 7XX
Pub Telephone Number: xxx xxxx
Placed By: Ben
Date/time placed: 20/09/2012 07:21:10
Trade Type: Man In Van
Description: List of jobs emailed by Chris, carried out by Martin Baker. No callout on system currently, although jobs already completed, just need signing off.
For any queries, please either contact the pub directly, telephone the Maintenance Help Desk on xxx-xxxx or reply to this e-mail.
Many Thanks
*CLIENT COMPANY*
There is more boilerplate around it, and obviously the email headers and such, but you get the idea. Each email will only contain one list, and the labels will remain the same, although I would like to future proof it so should they add new fields I will not need to change my code. I want to end up with an array such as:
$job['Job Type'] = Man in van
$job['Job Code'] = 1462399
...
$job['Description'] = List of all jobs emailed ... just need signing off.
Although I can be confident that the format will not change, every form is user input and as such may be unpredictable, particularly the description, which may contain line breaks.
This is the code I am using at the moment:
// Rip out the job details from the email
preg_match_all('/job type\:.*description\:.*\s{3}F/is', $the_email, $jobs);
for each job returned (should always be one but hey)
foreach($jobs[0] as $job_details) {
// Get the variables from the job description
preg_match_all('/(\w[^\:]*)\: ([\w\d][^\*]+)/i', $job_details, $the_vars);
}
// For each row returned, put into an array with the first group as the key and the second as the value
for ($i=0; $i<count($the_vars[0]); $i++) {
$arr[$the_vars[1][$i]] = $the_vars[2][$i];
}
It works, but it is ugly and I'm sure there is a better way. The main problem I am having is the description section, as I cannot simply search for the text following the ':' sign up until a line break, as the description itself may contain line breaks.
Any advice would be much appreciated!
Still not the prettiest thing in the world but it should work just fine!
preg_match_all('/\s{3}[ ]*([^:]+): ([^\n]+)/', $subject, $matches);
$job = array_combine($matches[1], $matches[2]);
preg_match_all('/Description\: (.*)\s{3}For any queries/is', $subject, $match);
$job['Description'] = trim($match[1][0]);
The first preg_match_all does what you said doesn't really work, just grabs all of the fields by whitespace, colon and newline.
The second one replaces the potentially erroneous Description key that the first one filled in.

Why doesn't sprintf() print anything? [duplicate]

This question already has answers here:
Why doesn't sprintf() output anything?
(3 answers)
Closed 11 months ago.
I'm using sprintf() in PHP but it displays nothing on the page.
Here's what I'm doing:
$lang['request_paragraph']="Please check your email for a message from %s (%s). This message contains instructions for initiating the issuance of a new password for your participant number. If you did not receive the email message from %s within 10-15 minutes, please make sure that your email provider does not have a spam filter that is blocking our email from reaching your Inbox. You will not be able to receive email from us if your email provider is using a mail-blocking device. Click on the button below to send the validation email again if it appears to have been blocked or never received. You will only be able to re-send the validation email 3 more times.";
$company="Company Name";
$admin_email="admin#company.com";
sprintf($lang['request_paragraph'],$company,$admin_email,$company);
Doing an echo on each individual string displays each string correctly, so what am I doing wrong?
I need to use sprintf() because I'm working with language files and it makes it much more simple than splitting the paragraph into pieces in the language definitions.
sprintf returns a variable (a string).
You need printf
edit to echo sprintf($lang['request_paragraph'],$company,$admin_email,$company);
You need to echo the sprintf too.
sprintf function just do the sequential replace of placeholder (type specifiers) characters in the first string argument and does not echo,print or output anything but return the formatted string.
so your line of code:
sprintf($lang['request_paragraph'],$company,$admin_email,$company);
will be replaced by:
echo sprintf($lang['request_paragraph'],$company,$admin_email,$company);
or
print(sprintf($lang['request_paragraph'],$company,$admin_email,$company));

Categories