"washing" IncomingEmail.Message contents to make it readable

If you have questions or if you want to share your opinion about Aware IM post your message on this forum
Post Reply
joben
Posts: 43
Joined: Wed Nov 06, 2019 9:49 pm
Location: Sweden
Contact:

"washing" IncomingEmail.Message contents to make it readable

Post by joben »

I'm using the Incoming Email feature and want to clean the contents of the Message attribute so I can present it in an edit form.

For instance, if you send an email to the system, your email client will most likely include HTML and CSS.
This is how the Message attribute looks like:
Skärmbild 2021-04-08 095446.png
Skärmbild 2021-04-08 095446.png (14.66 KiB) Viewed 69 times
This is how the Message attribute looks like after using some regular expressions:

Code: Select all

/*
1. Strip HTML tags. 
2. Remove weird CSS created by the email client. 
3. remove space if it appears at least 3 times in a row. 
4. remove newline if it appears at least 3 times in a row. 
5. Remove   character that messes upp saving the form.
*/

REPLACE_PATTERN(REPLACE_PATTERN(REPLACE_PATTERN(REPLACE_PATTERN(REPLACE_PATTERN(InkommandeMail.Message,'\<[^\>]+\>',''),'.*;}',''),'\s{3}',''),'\n{3}',''),'&nbsp;','')
Skärmbild 2021-04-08 104530.png
Skärmbild 2021-04-08 104530.png (7.28 KiB) Viewed 69 times
This works fine with emails composed in Outlook, but it also feels a bit primitive.

Is there a smarter way to "wash" the Message field, or preferably, get the plain text version so it doesn't require processing?
Regards, Joakim

Image
BobK
Posts: 464
Joined: Thu Jan 31, 2008 2:14 pm
Location: Cincinnati, Ohio, USA

Re: "washing" IncomingEmail.Message contents to make it readable

Post by BobK »

...get the plain text version so it doesn't require processing?
I do not know of anyway within AwareIM to get just the text of an email without requiring some sort of processing.
There might be a setting on the email client to send emails as text only. But that would require you having control of how the emails are sent.

If you do not have control of sending the emails, IMO using regular expressions to clean up the message is the best way to go.

That being said, here are a couple of suggestions on you regular expressions, which you could ignore since your regular expressions are working.

'\s{3}' matches exactly 3 characters of whitespace.
If you really mean "remove space if it appears at least 3 times in a row."
use '\s{3,}' to match/delete 3 or more characters of whitespace.

As mentioned above, \s matches whitespace. \n is considered whitespace so the check for '\n{3}' is not needed.

Note: The above were tested on a Windows 10 system.
Bob
hpl123
Posts: 2333
Joined: Fri Feb 01, 2013 1:13 pm
Location: Scandinavia

Re: "washing" IncomingEmail.Message contents to make it readable

Post by hpl123 »

I don´t use email internally in Aware (yet) but isn´t it possible to display the email in HTML instead i.e display the email the way it was (in most cases) designed. I GUESS you get the entire HTML of the email.
Henrik (V8 Developer Ed. - Windows)
Jaymer
Posts: 1889
Joined: Tue Jan 13, 2015 10:58 am
Location: Tampa, FL
Contact:

Re: "washing" IncomingEmail.Message contents to make it readable

Post by Jaymer »

you can display the message without doing any processing.
using an HTML field will show the email.
... unless you actually need to strip it for processing of some sort.
Click Here to see a collection of my tips & hacks on this forum? Or search for "JaymerTip" in the search bar at the top.

Jaymer
Aware Programming & Consulting - Tampa FL
joben
Posts: 43
Joined: Wed Nov 06, 2019 9:49 pm
Location: Sweden
Contact:

Re: "washing" IncomingEmail.Message contents to make it readable

Post by joben »

BobK wrote: Thu Apr 08, 2021 5:40 pm
...get the plain text version so it doesn't require processing?
I do not know of anyway within AwareIM to get just the text of an email without requiring some sort of processing.
Damn it. Guess I'm stuck with regex :)
BobK wrote: Thu Apr 08, 2021 5:40 pm If you do not have control of sending the emails, IMO using regular expressions to clean up the message is the best way to go.
This option has been considered, and sadly I can't control this.
BobK wrote: Thu Apr 08, 2021 5:40 pm That being said, here are a couple of suggestions on you regular expressions, which you could ignore since your regular expressions are working.

'\s{3}' matches exactly 3 characters of whitespace.
If you really mean "remove space if it appears at least 3 times in a row."
use '\s{3,}' to match/delete 3 or more characters of whitespace.

As mentioned above, \s matches whitespace. \n is considered whitespace so the check for '\n{3}' is not needed.
Thanks!
I am a newbie when it comes to regex, so don't be surprised if the code is pretty bad.
Regards, Joakim

Image
joben
Posts: 43
Joined: Wed Nov 06, 2019 9:49 pm
Location: Sweden
Contact:

Re: "washing" IncomingEmail.Message contents to make it readable

Post by joben »

hpl123 wrote: Thu Apr 08, 2021 7:37 pm I don´t use email internally in Aware (yet) but isn´t it possible to display the email in HTML instead i.e display the email the way it was (in most cases) designed. I GUESS you get the entire HTML of the email.
Jaymer wrote: Thu Apr 08, 2021 8:53 pm you can display the message without doing any processing.
using an HTML field will show the email.
... unless you actually need to strip it for processing of some sort.
HTML display works fine! AwareIM can even read and display the inline images inside the message.

However, in this scenario, we do need an editable field that is pre-populated by the plain text contents of the email message.
The regex solution has worked fine so far, so I will keep using it in lack of better options.

Thanks!
Regards, Joakim

Image
hpl123
Posts: 2333
Joined: Fri Feb 01, 2013 1:13 pm
Location: Scandinavia

Re: "washing" IncomingEmail.Message contents to make it readable

Post by hpl123 »

joben wrote: Fri Apr 09, 2021 6:01 pm
hpl123 wrote: Thu Apr 08, 2021 7:37 pm I don´t use email internally in Aware (yet) but isn´t it possible to display the email in HTML instead i.e display the email the way it was (in most cases) designed. I GUESS you get the entire HTML of the email.
Jaymer wrote: Thu Apr 08, 2021 8:53 pm you can display the message without doing any processing.
using an HTML field will show the email.
... unless you actually need to strip it for processing of some sort.
HTML display works fine! AwareIM can even read and display the inline images inside the message.

However, in this scenario, we do need an editable field that is pre-populated by the plain text contents of the email message.
The regex solution has worked fine so far, so I will keep using it in lack of better options.

Thanks!
It is probably possible to code a Gmail clone or similar in Aware as well i.e where you see the HTML email and then respond in the same "dialog". Take a look at Ranfab (sample application) under "Contacts" and "Communication" for a cool way of using a timeline as a email thread kind of thing and something like that with a couple of plain text attributes for replies and you almost have a Gmail in Aware :D.
Henrik (V8 Developer Ed. - Windows)
Post Reply