29/06/21

Log raw mail content to file

Hi!

After a long time, I return to write on my personal blog with a personal challenge. I've a Linux box with some scheduled tasks using cron service. I want to send mail to my personal mailbox in case of error of a single task (preferably with error log). It's not hard to configure the environment because just use a simple MTA (personally prefer msmtp) to send mails via command line. But it's hard to set up the values of From and To fields because cron use the default of execution user. 
So, the first question was: How to write the raw content of mail to disk ? 
The solution is to write a simple bash script that replace sendmail and write content of the mail to disk.

  #!/bin/bash
  cat - >> /tmp/$(date +%s).txt
  

Then, create a symbolic link of the script with sendmail name, and it's ready to write raw content to tmp path. The next step is to change the values of From and To fields....easy. With

sed "s/From: root\.*/From: [email protected] /" | sed "s/To: root/To: [email protected]/" | /usr/bin/msmtp $*
and it's done! Now, my mailbox has a set of mail sent from my linux box.

See youu.....

Nessun commento:

Posta un commento