0

I need to send a mail using mail command in centos. Body as HTML input and attachment will be a .txt file.

cat email_temp.html | mail -a "CH_sla_list" -s "$(echo -e "(RackTable Report)\nContent-type: text/html;")" example@xxx.com

If I execute above command, I'm getting a mail with inline (attachment file contents displays).

cat email_temp.html | mail -a "CH_sla_list" -s "$(echo -e "RackTable Report")" example@xxx.com

If I execute above one, I'm getting a mail with attachment but my HTML input displays as text in the mail.

My requirement is to use both HTML & text in the same header in a single command or any workaround will be appreciated.

Note: FYI, I'm using centos-release-7-0.1406.el7.centos.2.5.x86_64

  • I've never had any success getting `mail` to send html mail. I have read in multiple places that `sendmail` is better for this if you have root privaleges. I do not have root privileges at work, so I ended up writing a little `perl` script that imitates the behavior of `mail` but always sends an html message. – Jason Aug 07 '18 at 18:01
  • 1
    @Jason You don't need root privileges to use `sendmail` but you don't really need Sendmail either if you have a suitable version of `mail`. See https://stackoverflow.com/questions/24010230/mailx-send-html-message – tripleee Aug 07 '18 at 19:37
  • @tripleee On my system at work `sendmail` is linked to `sendmail` in sbin and won't even attempt to run. Out of curiosity, I did a `find` on `sendmail` and found one in `/usr/lib/`, and it works fine... I guess I will continue to use my perl script since I took the time to write it. Thank you. – Jason Aug 07 '18 at 21:32

1 Answers1

0

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318