On a development vm running Joomla, I configured it to send emails to /some/path/sendmail.bash
instead of the Sendmail default path /usr/sbin/sendmail
. This was an easy way to log mails to a file for later debugging (including decoding base64 parts) using a bash/awk script while also preventing any emails being send by the CMS.
/some/path/sendmail.bash
:
#!/bin/bash
read -d '' awkScript << 'EOF'
BEGIN {
p=1 #print
multipart=0
boundary=0
part=0
text=0
encoded=0
debug=0
}
!multipart && tolower($0) ~ /^content-type:[ \t]*multipart/ {
#works!multipart && /^Content-Type: multipart/ {
multipart=$2
if (debug) { print "Multipart Email: " multipart }
}
multipart && !boundary && /boundary=/ {
split($1,s,"=")
boundary = s[2]
gsub(/"/, "", boundary) #remove the surrounding quote
if (debug) { print "With boundary: " boundary }
}
#start of new part or end of all parts
multipart && ($0 ~ ("^--" boundary "$") || $0 ~ ("^--" boundary "--") ) {
part=1
text=0
encoded=0
p=1
}
part && !text && tolower($0) ~ /^content-type: text/ {
text=$2
if (debug) { print "Text: " text }
}
part && encoded {
#https://stackoverflow.com/questions/66228621/base64-decode-command-linux-cli-command
if (/^\s*$/) { #skip decoding blank lines
print $0
} else {
system("echo '" $0 "' | base64 -d")
}
}
p #if p then print current line
part && text && !encoded && tolower($0) ~ /^content-transfer-encoding: base64/ {
encoded=$2
if (debug) { print "Encoded: " encoded }
p=0 #make sure this line prints so test after it has printed
}
END {
#print "END"
}
EOF
#echo $awkScript
awk "$awkScript" >> /tmp/sendmail.log
#awk "$awkScript"