This is pushing the boundaries of what's possible with mail
. Your first command basically exploits a vulnerability which allows you to inject an arbitrary header by passing in a literal newline followed by the header(s) you want to add - incidentally, this has been prevented in newer versions of mail
on many platforms anyway.
(The echo
without an embedded literal newline, of course, serves no useful purpose at all; it's just an unusually fugly useless use of echo
.)
In theory, if you know what header structure you want to create, you could pass in something like half of the message as a "smuggled-in" sequence of headers, body, etc with the embedded newline trick; but at this point mail
isn't really serving any useful purpose any longer because you are doing the bulk of its job anyway.
My recommendation would be to switch to a properly MIME-capable mail client like mutt
which easily allows you to send multiple MIME body parts from the command line.
If you can't do that, here's a simple crude shell script.
# sendmail might be hidden in either of these
PATH=$PATH:/usr/sbin:/usr/lib
( printf '%\n' \
'To: example@xxx.com' \
'Subject: RackTable Report' \
'Content-type: multipart/mixed; boundary=_fooobar_' \
'Mime-Version: 1.0' \
'' \
'--_fooobar_' \
'Content-type: text/html' \
''
cat email_tmp.html
printf '%\n' \
''
'--_fooobar_' \
'Content/type: text/plain' \
'Content-disposition: attachment; name="CH_sla_list"'
''
cat CH_sla_list
printf '%\n' \
'' \
'--_fooobar_--' )|
sendmail -oi -t
I obviously had to make educated guesses about what email structure you want.
If you want to adapt this, notice that single quotes quote all strings verbatim. If you want to expand a variable, you need double quotes instead, like "To: $1"
This has a number of simplifying assumptions, and will not reliably cope with long lines of text (past 900-something characters), accented characters, or non-ASCII symbols (you get A-z, 0-9, and basic punctuation; but no curly quotes, typographic dashes or spaces, or currency symbols other than $
). It's 7-bit ASCII, like your grandpa's email when the Internet was new.
Some of these constraints can be relaxed if you know what you are doing, but if your requirements are significant, probably look into installing a third-party utility or writing a script in some language with at least proper MIME libraries. There are some reasonably decent third-party utilities which provide more or less the nice parts of command-line mutt
, but none of them are anywhere near as popular as mutt
itself; but if you have e.g. Python and one of the "do what I mean" email libraries, writing a similar custom utility of your own isn't hard, either.