-1

I have a cron job, whose output I send to mailx:

# Monday morning at midnight; "mail" is symlink to "mailx"
00 00 * * mon python3 script-with-7500line-output.py | mailx -S 'from=noreply' -s "Report" mygroup@mydomain.com

When the email is delivered, the script's output is in an attachment named ATT0001.bin.

We like that it attaches the output rather than puts it in the body. But: is there a way to set the name of the attachment?

  • 1
    Generally I recommend writing a wrapper script to use as the batch job when the requirements become less than trivial. for example make a shell script that redirects the output to a temp file with the correct name and then use `mailx -a filename ...` – diya Nov 22 '22 at 08:01

2 Answers2

0

I like @diya comment. I'll add here as an answer, and shift "accepted answer" to him if he posts his comment as an answer.

"Generally I recommend writing a wrapper script to use as the batch job when the requirements become less than trivial. for example make a shell script that redirects the output to a temp file with the correct name and then usemailx -a filename ..."

0

Posting this just in case someone comes across this and wonders how I did it.

this is a separate file... my production has a bunch of other robustness features (error-handling; template for mktemp; etc), but this is the basics:

#!/bin/bash
tmpDir="$(mktemp -d --tmpdir)"
python3 script-with-7500line-output.py > "${tmpDir}/PreferredAttachmentName.txt"
echo "Email Body" | mail -S "from=noreply" -s "Report" -a "${tmpDir}/PreferredAttachmentName.txt" mygroup@mydomain.com
rm -rf "$tmpDir"