0

This may sound like a dumb question, but I have tried and searched a lot of options, and none met my requirements

I am trying to send a mail with a report from unix command line using a bash script, I managed to send it with body and attachment using the mailx service, but the body is unformatted and not pleasing to the eye

(echo "$(cat /tmp/report_summary.html)";uuencode /tmp/report_details.xls report_details.xls) |/usr/bin/mailx -s "Report for XYZ" $MAIL_GRP

I tried the sendmail option, which formats the message body perfectly, but does not have an option for attachment

(
echo "From: [email protected]";
echo "To: [email protected]";
echo "Subject: Test Message 1 2 3";
echo "Content-Type: text/html";
echo "MIME-Version: 1.0";
echo "Content-Disposition: inline";
echo "<HTML><BODY><PRE>";
cat /tmp/report_details.html;
echo "</PRE></BODY></HTML>";
) | /usr/sbin/sendmail -t

I do not have mutt option, but I tried sendmail, mailx and mail service and none is able to help me meet my apparently simple need

Unix OS: Solaris 10 8/07 s10s_u4wos_12b SPARC

Note: Don't tag this as duplicate with existing answers, as they either have option to send attachment or html body or attachment with plain body.

3
  • IIRC, there needs to be a blank line between headers and content, and the line endings should be CRLF (although that probably doesn't matter). Commented Jun 1, 2018 at 21:48
  • FWIW, your sendmail command worked fine when I tried it on my Ubuntu 16.04 system using the Postfix implementation of the sendmail binary. Commented Jun 1, 2018 at 22:59
  • @Anthony Geoghegan: Both work fine individually, but as I said send mail only sends body, does not have option for attachment Commented Jun 2, 2018 at 13:35

1 Answer 1

1

Sending "single MIME part" using sendmail (low level command) is pretty easy.

Your script grave bugs:

  1. Missing "END OF HEADERS" (empty line)
  2. Missing -i sendmail command line option

Your script suggested improvements:

  1. Moving recipients from headers to sendmail command line
  2. Missing charset in Content-type (unless you use US-ASCII)
  3. Missing Content-Transfer-Encoding (unless you use US-ASCII = 7bit)

Corrected script:

#!/bin/sh
(
# Email headers and html header body in body 
cat - <<END
From: [email protected]
To: [email protected]
Subject: Test Message 1 2 3
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Content-Disposition: inline

<HTML><BODY><PRE>
END
# file to be "HTMLized" 
cat /tmp/report_details.html
# html footer in body
cat - << END_FOOTER
</PRE></BODY></HTML>
END_FOOTER
)
| /usr/sbin/sendmail -t -- '[email protected]'
Sign up to request clarification or add additional context in comments.

2 Comments

My sendmail script works fine, I am not able to send attachment, as mentioned in my question
sendmail command (provided by sendmail/postfix/exim/...) is a "low level tool". There is no command line option for attachment(s) - you must generate properly formatted message yourself. It is pretty simple for "single MIME part" message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.