I am trying to attach a .log file or .txt file to mail command to send an email once my ksh script executed. I am unable to use mutt command as it has been not installed in my machine and i am not supposed to install it. I have tried many ways by googling which has not been helped me to succeed.

Here is my requirement,I need to run some 10 sql queries and result need to send in the email via attachment. I have Check.ksh and i am running this by using nohup command.

nohup Check.ksh > Valid_Check.log 2>&1 &

In Check.ksh

typeset var TO="[email protected]";
typeset var SUB="Report";
typeset var BODY="Check Completed";
typeset var ATTACHMENT=${FILE_PATH}/Valid_Check.log

if [ -f "${ATTACHMENT}" ]; then 
echo "Log file exist";
echo "No Log File";

uuencode Valid_Check.log Valid_Check.log | mail -s "${SUB}" "${BODY}" ${TO}

and tried the below as well

#mailx -s "${SUB}" "${TO}" "${BODY}" < ${FILE_PATH}/Valid_Check.log

#cat ${FILE_PATH}/Valid_Check.log | mailx -s "${SUBJECT}" "${TO}" "${BODY}";

Above code throwing an error like "Valid... User unknown"

Any inputs would be helpful or any other simple/easy approach?


  • The error message suggests that Valid somehow ends up in $TO but the code you posted does not produce this result.
    – tripleee
    Apr 14, 2017 at 9:36
  • Using uuencode should definitely be your last resort. If your mailx supports attachments, definitely use this facility instead. MIME has been widely supported for over 20 years now.
    – tripleee
    Apr 14, 2017 at 9:38
  • Mixing a $BODY with uuencoded standard input might not be supported by your mail or mailx commands, but these are poorly standardized and have different capabilities on different platforms and of course also between versions. What platform are you on and what versions of there software packages are you using?
    – tripleee
    Apr 14, 2017 at 9:42
  • We are using Red Hat Linux version, and when i try this mail -s "${SUB}" ${TO} <<< ${BODY} then it is sending an email but with attachment not sure why it is not working.
    – Samah
    Apr 14, 2017 at 11:50
  • There can only be one body in a traditional RFC822 email and it can come from the command line or standard input but not both. Again, I would suggest you try to find a supported way to send multipart MIME messages intead of legacy uuencode format. As a deperate fallback, append the uuencode data to the body with an empty line in between.
    – tripleee
    Apr 14, 2017 at 12:59

1 Answer 1


If you have a recent enough mailx, use the -a option to add an attachment.

mailx -s "$SUB" -a "Valid_Check.log" "$TO" <<<"$BODY"

See also https://access.redhat.com/solutions/104833

If you are hopelessly stuck in a previous millennium and don't have any software which can handle proper attachments, the uuencode data should simply be appended to the body.

( echo "$BODY"; echo; uuencode Valid_check.log Valid_check.log ) |
mailx -s "$SUB" "$TO"

The basic approach suffers from a race condition, though - the existence of the output file indicates that the cron job has started, not finished. Putting the mail command at the end of the cron job would fix this; then, the email will only be sent when the previous step finishes.

  • Thanks a lot. i don't have a recent enough mailx, so mailx -a is not working but the other echo with uuencode approach worked for me.Thanks agian!
    – Samah
    Apr 14, 2017 at 14:20
  • Hi again,getting some NHÔ” characters while using the above uuencode in the attachment where as the log is having expected echo messages.Any help!? Thanks.
    – Samah
    May 8, 2017 at 11:34
  • It's not clear where these characters are appearing. uuencode simply translates the input into a text-only representation which should be reasonably robust against various mail transport issues. If the extracted attachment contains things which were not present when you encoded it on the sending side, what exactly is failing? If the input contained characters e.g. in an encoding you cannot identify, the email transport is doing what it's supposed to be doing, and the problem was present before you sent the file as well.
    – tripleee
    May 8, 2017 at 11:38
  • If $BODY begins with a dash, it's possible that you are producing an error message from echo which consumes part of the input. Try printf '%s' "$BODY" to exclude this possibility.
    – tripleee
    May 8, 2017 at 11:39
  • These characters are appearing the txt file which has been attached and sent to the file,when i looked at the log file it is printing all the error messages which are there in echo but when i opened the file from mail then in some places these kind of characters are coming.And i have used printf option but still getting the same kind of messages.
    – Samah
    May 8, 2017 at 12:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .