2

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.

asked Aug 28 at 10:40

1 Answer 1

0

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

answered Aug 28 at 10:47
3
  • 1
    Your 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. Commented 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 when file.txt contains null bytes) Commented 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. Commented Aug 29 at 11:59

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.