Thursday, November 1, 2012

Mailing attachments. No UUencode; no mailx -a or mutt

On a lot of older systems you probably have uuencode to add attachments to mail / mailx from a *nix system.  If your system is missing uuencode or doesnt have a mail client that supports attachments, you might need to try base64 instead. There is the base64 command, if not can use 'openssl base64'.
Below is a snippet that works, and gets passed directly to sendmail. In this case it was an ascii text file.
The parenthesis subshell everything so the whole block of text will get passed to sendmail and parsed correctly. Without the headers, just a base64 blob of text would appear to the recipient.
 ( echo "to: $recipients"
  echo "subject: Report for rsync for from_leads on `date`"
  echo "mime-version: 1.0"
  echo "content-type: multipart/related; boundary=xBoundaryStringx"
  echo
  echo "--xBoundaryStringx"
  echo "content-type: text/plain"
  echo "Body Text"
  echo "Body Text"
  echo "--xBoundaryStringx"
  echo "content-type: text/plain; charset=us-ascii; name=$filename"
  echo "content-transfer-encoding: base64"
  echo
  openssl base64 < $filename ) |sendmail -t -i
  



This was what I had to use to send log attachments on an RHEL 5.8 system that had no mail client supporting attachings, did not have uuencode and was not allowed to have any further packages installed.

10 comments:

  1. This is awesome!!! you rock man. Please keep us being enriched with the ocean of knowledge you have.

    ReplyDelete
  2. How to send excel file as an attachment by using the above script?

    ReplyDelete
    Replies
    1. I personally have not tested it, but you might just try attaching the excel sheet as is.

      Delete
  3. It is not working for Linux.. any idea for Linux pls?

    ReplyDelete
  4. Hey I tried to use this process to send an excel file and wound up with a text file attachment...

    ReplyDelete
  5. thanks, the scrip run ok with atach file *.txt, but file *.gz not run ok. Do you have suggestions?

    ReplyDelete