I want to send the content of file.txt as the body and file.png as attachment.
I only have mail cli (mail (GNU Mailutils) 3.10). No other software package can be installed, so Mutt is out of the question for example.
I can have one or the other, like this:
mail -s "xxx" [email protected] < file.txt
gives me the text file as mail content.
mail --content-type='image/png' -A file.png -s "xxx" [email protected]
gives me the png as attachment.
But when I try to do what I need it doesn't work:
echo "-" | mail --content-type='image/png' -A file.png --content-type='text/plain' -A file.txt -s "xxx" [email protected]
gives me both as attachments which is not really what I want,
cat file.txt | mail --content-type='image/png' -A file.png -s "xxx" [email protected]
fails with no mail sent,
mail --content-type='image/png' -A file.png -s "xxx" [email protected] < file.txt
sends the mail but the body remains empty, the text file is a garbled attachment.
Is it possible to do this?
I have seen it done with mutt (e.g. here or here) but as I said, I only have mail available.
1 Answer 1
This was so simple...
msg=$(cat file.txt)
echo "${msg}" | mail --content-type='image/png' -A file.png --content-type='text/plain' -A file.txt -s "xxx" [email protected]
Strange thing is that echo $msg skips the newlines, even with -e, but in the received message it is properly displayed with newlines...
-
1Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.2025年08月28日 10:55:00 +00:00Commented Aug 28 at 10:55
-
also, that shell code is actually pretty um, suboptimal!
msg=$(cat file.txt)
;echo "${msg]" | mail ...
is a lot less sensible than a simple<file.txt mail ...
(and will have interesting moments whenfile.txt
contains null bytes)Marcus Müller– Marcus Müller2025年08月28日 16:00:59 +00:00Commented Aug 28 at 16:00 -
Well, yes, except that does not work as I said in my initial question. I would have done that if it worked. I'd be really curious as to why redirecting the file contents to mail works only when there is no attachment, it's a mystery to me.Oliver Henriot– Oliver Henriot2025年08月29日 11:59:53 +00:00Commented Aug 29 at 11:59