Hi Monks,
this is my first post in this site and I'm a new user... also i don't have much knowledge of perl :(, sorry. I know that perl it's a great language with which you can do fantastic things, but my work has taken me in other directions. Now my question.

I want to add a warning message like this:

CYBER SECURITY WARNING: This email is from an external source - be careful of attachments and links

In top of all incoming emails that come from external sources using exim, the best option that I can see is write a script thats is calling during transport, this script can read the entire mail and can do some modifications to it. In the exim docs are an example writed in perl, and seems like this is the best option to do this. Using this script as base I have done this:

please don't laugh too much :D

#!/usr/bin/perl $/ = ""; # set paragraph mode chomp($headers = <STDIN>); # read a paragraph, remove trailing newlin +es $/ = "\n"; # unset paragraph mode printf(STDOUT "%s\n\n", $headers); while (<STDIN>) { if ($_ eq "Content-Type: text/plain; charset=utf-8\n"){ $p = 'y'; } if ($_ eq "Content-Type: text/html; charset=utf-8\n"){ $y = 'y'; } printf(STDOUT "%s\n", $_); if ( $p eq 'y'){ printf (STDOUT "%s\n", "WARNING IN PLAIN TEXT") ; $p = 'n'; } if ( $y eq 'y'){ printf (STDOUT "%s\n", 'HTML WARNING') ; $y = 'n'; } }

with the first tests everything seemed to work well, but when I tested it with real emails I had received the bugs began to appear. According to the MIME standard there are a lot of headers that can appear in the mail body (Content-Type, Content-Disposition, Content-Transfer-Encoding, etc), see the following link for more info:

https://en.wikipedia.org/wiki/MIME

I don't have always the same headers, sometimes they appear just above de body, others there are others headers before the body, depends on the origin server, some examples at bottom. I'm desperate and frustrated.

Can someone help me to extract only the part of the message, either in html, in plain text or coded in base64 so I can add the warning?

Thank you very much

------------------------------- SOME EXAMPLES -------------------------------

Example 1:
MIME-Version: 1.0
Content-type: text/html; charset=iso-8859-1
From: ******** <*******@**********>
Message-Id: <20200910203400.8DD062046864@********>
Date: Thu, 10 Sep 2020 22:34:00 +0200 (CEST)
... more headers ...
the html message

Example 2:
x-ms-exchange-transport-crosstenantheadersstamped: DB6PR0501MB2792
Content-Type: multipart/related;
boundary="_005_DB6PR0502MB2934154A8B24C72DCD99E1E78A240DB6PR0502MB2934_";
type="multipart/alternative"
MIME-Version: 1.0
... more headers ...
Content-Type: multipart/alternative;
boundary="_000_DB6PR0502MB2934154A8B24C72DCD99E1E78A240DB6PR0502MB2934_"

--_000_DB6PR0502MB2934154A8B24C72DCD99E1E78A240DB6PR0502MB2934_
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64

the message in plain text encoded in base64

Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: base64

the message in html encoded in base64

Example 3:
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_2475097_1688559153.1599727641537"
Thread-Topic: *************************************************************
Thread-Index: AdaFB4a1gtdC+dbyQ2GK8Qrlu/b37EPAIenT

------=_Part_2475097_1688559153.1599727641537
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

the message in plain text

Content-Type: application/pdf;
name="=?utf-8?filenamef?="
Content-Disposition: attachment;
filename="=?utf-8?filename?="
Content-Transfer-Encoding: base64


In reply to Perl script for body rewrite by fgarpe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.