Using procmail to forward inline - procmail

I would like to use procmail to forward a message to another email address. Both the headers and body of the incoming message should be in the body of the outgoing message (inline forwarding).
Example incoming message:
From: outside#example.com
To: me#example.com
Subject: Test
Date: Mon, 03 Nov 2014 05:00:04 GMT
This is a test
The forwarded message should be like this:
From: me#example.com
To: thirdparty#example.com
Subject: Fwd: Test
Date: Mon, 03 Nov 2014 05:01:00 GMT
From: outside#example.com
To: me#example.com
Subject: Test
Date: Mon, 03 Nov 2014 05:00:04 GMT
This is a test
Can this be done using procmail, maybe in conjunction with something like formail?

Easy enough.
:0
* Some conditions, perhaps? Omit this line to forward unconditionally
* ^Subject:[ ]*\/.*
| (echo From: me#example.com; echo To: thirdparty#example.com; \
echo "Subject: Fwd: $MATCH"; echo; cat -) | $SENDMAIL -t
If you don't care about forwarding the original Subject header verbatim, this can be simplified additionally.
The -t flag to sendmail says to use whatever To: and Cc: headers are in the message to determine the recipient. I omitted generating a Date: because (most imitations of) Sendmail will do that for you.
The stuff in the square brackets should be one space and one tab, as usual.
If you want to keep a copy, either add Bcc: yourself (and take care to not have the incoming copy trigger a mail loop!) or change :0 to :0c which makes Procmail continue down the rest if the recipe file.

Related

Extracting string to variable using regex bash

I have a string which is like:
Return-Path: bT.41aywtru20=krja5b54hplm=k29fsc7grl#fake.link.com
Received-SPF: pass (fake.link.com: Sender is authorized to use 'bt.41aywtru20=krja5b54hplm=k29fsc7grl#fake.link.com' in 'mfrom' identity (mechanism 'include:spf.smtp2go.com' matched)) receiver=pmxlab01.permission.email; identity=mailfrom; envelope-from="bt.41aywtru20=krja5b54hplm=k29fsc7grl#fake.link.com"; helo=e2i353.smtp2go.com; client-ip=103.2.141.97
Received: from e2i353.smtp2go.com (e2i353.smtp2go.com [103.2.141.97])
by mailserver.fake.com(Proxmox) with ESMTP id A4F983E1048
for <fake#fake.com>; Tue, 24 Aug 2021 14:47:20 +0100 (BST)
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed;
d=smtpcorp.com; s=a1-4; h=Feedback-ID:X-Smtpcorp-Track:Message-Id:Subject:
Date:To:From:Reply-To:Sender:List-Unsubscribe;
bh=cTg4MkkE2uaIjpApjJYQFK3RgYiMF3bwCj8UZjFO4NE=; b=STU7lctit7L5LJ2tA3Re1fe4II
lXJbY/SBXTGqCHh9p4K86aLK5Bvz98Q7eR9xwjFib6x4NoZZ5L1fke0XQERd1eQvxkl9R+kRIGU8A
QOtrLPpt8coN8P+syoaTRR4pDJQG9OfJO1fON9OaOP8HwnEg/91ie6Cm+wQRxjwyat859uAcu89Xv
6/mrcequkSp6kfiQN4goZ7vMYJYfBYuooslbTciaK4SYIfxdINyrrWGA6QhJPobdW0uuedRNY5jBG
OdMbVmm7FTpxDJs51rB1PTIcFQ8W1oypcttqSgCjI+5eMVrabU/IoIxhX5F0Cn3zm7E9CHlaJuLt1
CRXVbwdw==;
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=fake.com; i=#fakelink.com; q=dns/txt; s=s575655;
t=1629812840; h=from : subject : to : message-id : date;
bh=cTg4MkkE2uaIjpApjJYQFK3RgYiMF3bwCj8UZjFO4NE=;
b=TEeEsPNLf7Wi6b8aaxE6JvfymfBKYjLq7izcUVrOXTW7sGIznxOA5udhfmDh15Fgp6Qgh
Kv5HX9uPNa8TEeoaJ+gV/4KERuscnc4GXEHwo0eclktx6f6JI5h1/q+qCe34+cN/EweaP5n
iOs+nrzsRuWn/iQ0Yck+b4IXVWHoTW8298xmBNuC1JF4jIVXREJFAC0nACfGU03OlpjDXf/
qvI6Ffnn5YGTNxgIkOdrtymaqOvjG9NM0PWtgSkvsTCJdUvxkrI+rRUG6ixiNi+vifqwvox
aQ6BRnMmeNK7A954Dy9r9r09QzbTthsBsi+lORKH7DntBKhm7Rb5/Q9j0xVA==
Received: from [10.176.58.103] (helo=SmtpCorp) by smtpcorp.com with esmtpsa
(TLS1.2:ECDHE_SECP256R1__RSA_SHA256__AES_256_GCM:256)
(Exim 4.94.2-S2G) (envelope-from <tomtest#fakelink.com>)
id 1mIWls-TRjyEC-AK for fake#fake.com; Tue, 24 Aug 2021 13:47:20 +0000
Received: from [10.86.20.232] (helo=DESKTOP-69OG2R3)
by smtpcorp.com with esmtpsa (TLS1.2:ECDHE_RSA_SECP256R1__AES_256_GCM:256)
(Exim 4.94.2-S2G) (envelope-from <fakename#fakelink.com>)
id 1mIWlr-9EFPsz-U0 for fake#fake.com; Tue, 24 Aug 2021 13:47:19 +0000
MIME-Version: 1.0
From: fake#fake2.com
To: fake#fake.com
Date: 24 Aug 2021 14:46:30 +0100
Subject: Test Email 2xM9e5Dj
Content-Type: multipart/alternative;
boundary=--boundary_11_ddba370a-13e2-4ffc-8b36-0eb7a5cde80e
Message-Id: <E1mIWlr-9EFPsz-U0#message-id.smtpcorp.com>
X-Smtpcorp-Track: 1XmW_r9EFeszl0.JChXLDDjoy7xH
Feedback-ID: 575655m:575655aVI_MaS:575655sNpPp5WOdD
X-Report-Abuse: Please forward a copy of this message, including all headers,
to <abuse-report#smtp2go.com>
----boundary_11_ddba370a-13e2-4ffc-8b36-0eb7a5cde80e
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
This is a text message
----boundary_11_ddba370a-13e2-4ffc-8b36-0eb7a5cde80e
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
This is a html message
----boundary_11_ddba370a-13e2-4ffc-8b36-0eb7a5cde80e--
This is stored in a variable called $emailText
I'm trying to use a regex to take the From part out of the text
From: fake#fake2.com
My regex isnt super strong, however my testing looks like this works: (?<=From: ).*.
But when I try and take the text out, it appears I can't get the regex to go through properly.
echo [[ $emailText =~ (?<=From: ).*. ]]
bash regex doesn't support lookbehind or lookahead assertions.
It is much easier to use a non-regex approach using awk here:
awk -F ': ' '$1 == "From" {print $2}' <<< "$emailText"
fake#fake2.com
With bash:
[[ "$emailText" =~ From:\ ([^$'\n']*) ]] && echo "${BASH_REMATCH[1]}"
Output:
fake#fake2.com
With your shown samples, attempts; please try following awk code. Simple explanation would be, checking condition if 1st field is From: then print 2nd field of that line.
awk '$1=="From:"{print $2}' Input_file
2nd solution: In case you have only 1 entry of From: in whole file then try following, where we can use exit function to exit from Input_file after printing the matched line, to stop un-necessary reading of whole Input_file.
awk '$1=="From:"{print $2;exit}' Input_file
Assuming you only want the email terminus, here's a quick and dirty Awk script.
awk '/^$/ { exit 1 }
/^From: .* <[^<>#]+#[^<>]+>/ {
split($0, g, /[<>]/); print g[1]; exit }
/^From: / { print $2; exit }' file.eml
This should work correctly for all these cases:
From: Real Name <real.name#example.com>
From: "Name, Real" <outlook#torture.example.com>
From: terminus#example.com
From: terminus#example.com (Real Name)
From: =?q?utf-8?Real_N=A3=E4me?= <real.name#example.com>
As especially the last example should convince you, you will need significantly more work if you also need the full name of the correspondent in normalized form.
If there should be a mail address present, you can match it first using awk (without the unsupported need for lookarounds)
awk 'match($0, /^From: [^[:space:]#]+#[^[:space:]#]+$/) {
print $2
}' <<< "$emailText"
Output
fake#fake2.com

Why might mutt email be accepted/rejected by windows recipient as a function of alphabetic string content in the body of html file being sent?

Calling mutt-1.5.24 on linux.
I'm seeing some very odd behavior when emailing an html file from linux to windows/outlook using mutt on linux. Example of the mutt call...
mutt -e 'set content_type=text/html' -s 'yuk, yuk, yuk' 'moe.howard#stooge.com' < a.html
The email does not show up on the windows side. mutt returned no error or warning on the linux side. Now, here's the odd part... If I global/replace the string "pcie" in the body of the html to "pcix", the email appears on the windows/outlook side just fine. OR... if I global/replace "ity" to "..." it also works fine (even if I leave "pcie" alone). But changing "ity" to "xxx" fails. Very odd character sensitivity behavior like this.
In my home dir on the linux side I see a file ~/sent getting created. The header (whether the email made it to the windows/outlook side or not) looks like...
From m.howard#theserver.stooge.com Thu Jan 28 18:49:29 2021
Date: Thu, 28 Jan 2021 18:49:29 -0500
From: Moe Howard <mhoward#theserver.stooge.com>
To: moe.howard#stooge.com
Subject: yuk, yuk, yuk
Message-ID: <20210128234929.GA48266#atletx7-reg062.amd.com>
MIME-Version: 1.0
Content-Type: text/html; charset=us-ascii
Content-Disposition: inline
User-Agent: Mutt/1.5.24 (2015-08-30)
Status: RO
Content-Length: 20537
Lines: 122
<html>
....etc... the rest of the html which firefox reads just fine if I get rid of the header above
Grasping at straws. Looking at the "charset=us-ascii" in the "sent" file thinking it should be something else ? So I tried providing other options by adding "-e 'set assumed_charset=utf-8:us-ascii'" to the command. No luck.
Any ideas what might be happening and what a solution might be ?
Figured it out. All my email actually arrived in Outlook. It's just that it got sent to the junk folder, labeled as spam. So if the body of the html contains "pcie", it's spam. But "pcix" is not. Got to go undo that now.

Why isn't booliean expression working

I am using the following regular expression to filter some junk emails
\bNeotube\b | \bNeotubeTV\b
Here is a sample of the junk email header:
Return-path: <uranus#pschic.info>
Envelope-to: coben#jesusmylord.org
Delivery-date: Thu, 25 May 2017 14:18:58 +0200
Received: from [45.59.120.18] (port=30375 helo=pschic.info)
by ok1057.kvchosting.com with esmtp (Exim 4.89)
(envelope-from <uranus#pschic.info>)
id 1dDrj8-0002X1-Se
for coben#jesusmylord.org; Thu, 25 May 2017 14:18:58 +0200
From: "NeotubeTV" <uranus#pschic.info>
Date: Thu, 25 May 2017 06:58:18 -0500
MIME-Version: 1.0
Subject: Free TV shows, Sports and New Movies on your TV In HD?
To: <coben#jesusmylord.org>
Message-ID: `
The above expression does not work. However, if I just use
\bNeotubeTV\b
the email is filtered. Why doesn't the above OR statement work?
Thanks for your help.
Chris
It's because you have spaces around the | which is saying that you want include those spaces in your regex.

how to pre-fix a piece of text in github "git log" using shell-script

I need to make a github commit (the text), from the git command git log into a link in an email. So the recipient can click on the link and go directly to the change.
I receive a long list containing lines with the text:
commit some_long_string_of_hexadecimals
and I need to transform this into:
commit https://github.com/account/repo/commit/some_long_string_of_hexadecimals
The log I am receiving contain n-amount of these logs, so I need the script to do this for all instances of this (some_long_string_of_hexadecimals).
Here are a few example log statements:
commit a98a897a67896a987698a769786a987a6987697a6
Author: Some Person <some#email.com>
Date: Thu Sep 29 09:48:52 2016 +0200
long message describing change.
commit a98a897a67896a987698a769786a987a6987697a6
Author: Some Person <some#email.com>
Date: Thu Sep 29 09:48:52 2016 +0200
more description
I'd like it to look like this:
commit https://github.com/account/repo/commit/a98a897a67896a987698a769786a987a6987697a6
Author: Some Person <some#email.com>
Date: Thu Sep 29 09:48:52 2016 +0200
added handling of running tests from within a docker container
How do I achieve this using a shell command ?
Thanks in advance.
awk '$1 == "commit" {$2 = "https://github.com/account/repo/commit/" $2} 1'
check if field 1 equals "commit"
if so, prepend to field 2
if line matched, print modified line, else print line as is

Email header showing, dkim=hardfail (test mode) and dkim=permerror (bad sig)

I am trying to write an email client application by c++ and OpenSSL..
When I am checking and sending email through this application then Gmail and Yahoo both are showing wrong in my email header..
Gmail showing > "dkim=hardfail (test mode) header.i=#".
Yahoo showing > "dkim=permerror (bad sig)".
My Email header for Gamil :
Delivered-To: rajforever6#gmail.com
Received: by 10.112.41.134 with SMTP id f6csp80981lbl;
Tue, 26 Mar 2013 02:25:15 -0700 (PDT)
X-Received: by 10.68.59.199 with SMTP id b7mr22705642pbr.167.1364289914515;
Tue, 26 Mar 2013 02:25:14 -0700 (PDT)
Return-Path: <no_feed#korcot.info>
Received: from smtp.korcot.info ([103.18.168.250])
by mx.google.com with SMTP id ak6si16720576pbd.344.2013.03.26.02.25.08;
Tue, 26 Mar 2013 02:25:14 -0700 (PDT)
Received-SPF: softfail (google.com: domain of transitioning no_feed#korcot.info does not designate 103.18.168.250 as permitted sender) client-ip=103.18.168.250;
Authentication-Results: mx.google.com;
spf=softfail (google.com: domain of transitioning no_feed#korcot.info does not designate 103.18.168.250 as permitted sender) smtp.mail=no_feed#korcot.info;
dkim=hardfail (test mode) header.i=#korcot.info
Date: Tue, 26 Mar 2013 02:25:14 -0700 (PDT)
DKIM-Signature: v=1;
a=rsa-sha256;
s=mail;
d=korcot.info;
t=1364289917;
c=relaxed/relaxed;
h=from:to:subject:message-id:mime-version:content-type;
bh=Q3K/t162lFKWTLs4xBY2FR1vT5pThpI6rnqM7i6p/Vo=;
b=Tgrc2nelY/3HA0M14s4RXFvhonXQzFRtDAD/X/0ITvln6JSWjIxgQSX7WOrfAxb4K
kTXV+Bbw6DKK+2aVi2WrCYdduP5wXJpTVnjaCJQTs2+DtAo3sL1re6tz3uHOpa0PmRS
3qRzfXQqN8QczOv63UjAGO6sL+o5atU3vbRYMxg=
From: "Comfirm"<no_feed#korcot.info>
To: "Kuntal"<rajforever6#gmail.com>
Subject: Key6gg6 ?
Message-ID: <20030712040037.46341.5F8J#football.korcot.info>
MIME-Version: 1.0
Content-Type: text/html
My Email header for Yahoo :
From Comfirm Wed Mar 27 02:26:20 2013
X-Apparently-To: raja_kuntal_majumder#yahoo.com via 106.10.150.84; Tue, 26 Mar 2013 11:26:20 -0700
Return-Path: <no_feed#korcot.info>
Received-SPF: softfail (transitioning domain of korcot.info does not designate 103.18.168.135 as permitted sender)
bm5lciByZWFkeSBJcyBkaW5uZXIgcmVhZHkgSXMgZGlubmVyIHJlYWR5IElz
IGRpbm5lciByZWFkeSBJcyBkaW5uZXIgcmVhZHkgPyBKb2UuIAEwAQEBAQ--
X-YMailISG: v3YQap0WLDt5n8clVc4Zd9ENB5oCUMcfvdmbG1QujbCK18hQ
nhRHHgRhlvqfyO8lOyt.Rwjm8Y1_i35XTte6Pt.JxYeVZXQtQqLe66qLvndr
_RhG.PTZE2AWeRN.tMHi.aAIcS9WfKzOzi9i3tYtSSzZMPBMlBwFu5hBV4qI
v3KO9LCaMGeCe3W4mLsxgbkdddDi_8r8afEb7jAUKVn8i2VyRaJQGELdL5xb
.lNzJ0h_vMs5HseAhRPefe.o9W4A_oMpcairIa9OfDkC3zmZWYbyntXcxNgi
uKtvTwaqyeuUvuyIxrRGlpUy8RFxRaLOlp6s8riVCfLyu1dEE6Bpa9KFlVa0
JtRm0XZ3znl2WBzw1PnHskYZ3X2SHrzz8q7LfmUETa7idziLtkLNqdzZbvJE
nnoc3W3aYmxkgZob25oRD8Q338f9774TfJMjeqTaQLbmMflOW5GqoPYneB0v
hKqmdYUcqeZpVfcRXBVSxzS2_lK7spwOJX_wXKAH5TbiRXORouWvbkw9EHgc
yxqKoTQAqpnc8ibxcfT9cmVQ2KXYXlrl7d0VotpJ.m8I0sMY0mTviVkt6vCI
FJUYScRuGMxjOaYqyURUU5mDM_bxQXRl1WFzEhYBQKsyMOhZVtBBrPyEsCLs
NHASPNKPUS7wuB712WAI3aMqTNV.kNize8nwrm.hj9f9fb2nJwcB.IvuQvKx
Ujd9Pq68B9u80Wd8gtp5rAIy7.B_0x6JHwK14fDWgh1Yr7k1PB8A_daUJh.G
d2f4rgY0CFbg3chmWC7O5AGvODB79.pFfkl8ooZsUpYqdsj5.KX5XLQjsfud
JCsO1870Gtty31aPGvSHRPDGOTJ_lQ6v25BMaCnGS8AtvYbtjW9avY0MHTJw
rnT0_Hzca6oXayZG0CCcvQ6CMd.UnOlIt4pFEWwd9Zlo5BIJ4hd9N99qMbul
mGgNYVRBc0f7DokmkLiHSI29k34ifNyh8Z1qbeKWTRo4FXDBa.vfc9cVWqpp
DVF8FbkpuThc7QJ63cszp98TCdZy2Bq5yOMrqEdK85e141MndRChrWmjUplJ
4e_UWqNmaL17cGPzXuJc3TAD3nsdK9qh1ax.9K.jkRwSm4zgWscqosCPAgOJ
lIOo3L3QScOP9iQ6xOfuzm7dxw44Z3WBHMthoOgw_MGt488EClyDr2PiJT.f
1_BklnxGS2iuYH2TLyY69ESKE0Y4pPGmaboLASqBQkQZ6xaoFM8HDoIB20zK
KrNadXnlkMW.t6RuIcaPX3fOrKzvnKPerZ3NA1kn
X-Originating-IP: [103.18.168.135]
Authentication-Results: mta1360.mail.bf1.yahoo.com from=korcot.info; domainkeys=neutral (no sig); from=korcot.info; dkim=permerror (bad sig)
Received: from 127.0.0.1 (HELO smtp.korcot.info) (103.18.168.135)
by mta1360.mail.bf1.yahoo.com with SMTP; Tue, 26 Mar 2013 11:26:19 -0700
DKIM-Signature: v=1;
a=rsa-sha256;
s=mail;
d=korcot.info;
t=1364322363;
c=relaxed/simple;
h=from:to:subject:mime-version:content-type;
bh=Q3K/t162lFKWTLs4xBY2FR1vT5pThpI6rnqM7i6p/Vo=;
b=THj4wE+EOSKThXGIUPBgcTlWTICIDFikgEvlQVpBotViQTNW+k/dpr58kVZ1HAght
KTdACtbEO2m3A17vxrY1kEQazhwu7pbWIW5TwBfUtIU9SbFBL88AQz2fxxi6BHqEfNh
WYeGG0mEskG9J2JFeNVG0WaaNJ9gTh5t/4nqvVc=
From: "Comfirm" <no_feed#korcot.info>
To: "Kuntal" <raja_kuntal_majumder#yahoo.com>
Subject: Key6gg6 ?
MIME-Version: 1.0
Content-Type: text/html
Content-Length: 287
My TXT records in DNS(godaddy.com) :
# >>>> v=spf1 a mx mx:smtp.korcot.info ip4:103.18.168.250 ~all
mail._domainkey >>>> k=rsa; t=y; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCjKsxH3Hb8UrNZ+Aar7GfJ30dcbS19i+89JA8SSlDEhsFfTAR9UZIolxKtD3HgPzhWGfcoLkQg28NS/Y3tT/WTvfXxryDyFRIbKJox6VbVmmvSmATsik1l2YQSkTTpeHX6nzxgzRwDVvmLfZBZBbO3n+y0YNTucOUsfNFYCL/UAwIDAQAB
_domainkey >>>> t=y; o=~;
Really I don't have knowledge why Gmail and Yahoo showing "dkim=hardfail (test mode) header.i=#" and "dkim=permerror (bad sig)", both header should be "dkim=pass (ok)"..
So please give me advice.
Please tell me what should I do to solve this problem..
Thanks...
You didn't mention whether you are using a standard library to calculate the DKIM signature, or have done your own. I strongly recommend OpenDKIM as a good library to use if you aren't already.
Another place to test is to send a message to the mail reflector at sa-test#sendmail.net. It may give you more diagnostic information.
You should check the header fields that are signed (From, To, Subject, Mime-Version, and Content-Type) between what you sent originally and what was received. Some mailers "helpfully" rewrite these header fields, breaking the signature in the process. For example, it could have signed no_feed#smtp.korcot.info and subsequently changed it to no_feed#korcot.info.