Score:-1

mailx Set Name of Attached File

sy flag

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" [email protected]

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?

diya avatar
la flag
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 ...`
Score:0
sy flag

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 ..."

Score:0
sy flag

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" [email protected]
rm -rf "$tmpDir"
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.