Skip to content

Instantly share code, notes, and snippets.

@jondkelley
Last active May 23, 2024 13:28
Show Gist options
  • Save jondkelley/992c45ad7389c56901677515e0786a5b to your computer and use it in GitHub Desktop.
Save jondkelley/992c45ad7389c56901677515e0786a5b to your computer and use it in GitHub Desktop.
Linux Awesome Gists 1: Sending a file from command line one-liners

Send an attachment via email on bash using...

mailx

Install Debian/Ubuntu

  • apt-get install postfix

Alternatively, without the bloat of postfix

  • apt-get install heirloom-mailx

Install Redhat/Centos

yum install mailx

Command

cat mysqldbbackup.sql | mailx [email protected]

Newer versions of mailx support -a flag for attachments!

mailx -a backup.sql -a grants.sql [email protected]

Mutt

Install Debian/Ubuntu

  • apt-get install mutt

Install Redhat/Centos

  • yum install mutt

Command

echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- [email protected]

uuencode and mail

This sends the uuencoded part inline and not as an attachment. Many mail-clients recognize this though and display the uuencoded part as an attachment. Don't use uuencode when other alternatives use MIME.

Command

gzip -c ldiff.sql | uuencode mysqldbbackup.sql.gz  | mail -s "Ldiff from openldap" [email protected]

Mpack

Install Debian/Ubuntu

  • apt-get install mpack

Command

mpack -s subject /dev/stdin [email protected] < file

Sendemail.pl

Command

sendemail -f [email protected] [email protected] -m "Here are your files!" -a file1.jpg file2.zip

Swaks

Install Debian/Ubuntu

  • apt-get install swaks

Command

swaks -tls \
--to ${MAIL_TO} \
--from ${MAIL_FROM} \
--server ${MAIL_SERVER} \
--auth LOGIN \
--auth-user ${MAIL_USER} \
--auth-password ${MAIL_PASSWORD} \
--header "Subject: $MAIL_SUBJECT" \
--header "Content-Type: text/html; charset=UTF-8" \
--body "$MESSAGE" \
--attach mysqldbbackup.sql

BASH and netcat

Install Debian/Ubuntu

apt-get install netcat

Script

#!/bin/sh

# Default reception
TOEMAIL="[email protected]";
# Default Subject
SUBJECT="You got mail - $DATE";
# Default Contents
MSGBODY="Hello, this is the default message body";
# Default Attachment
#ATTACHMENT="/tmp/testoutput"
# Default smtp server
mailserver="smtp.server.ltd"
mailserverPort="25"

showUsage() {
        echo "$0 -a /file/to/attach [-m /message/file] [-M \"Message string\"] -s \"subject\" -r [email protected]"
        echo
        echo "The attachment (-a) is required, if no attachment is used then rather use sendmail directly."
}

fappend() {
    echo "$2">>$1;
}
DATE=`date`

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# This might need correction to work on more places, this is tested at a ubuntu 13.10 machine.  #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
domain=`grep search /etc/resolv.conf | awk '{print $2;}'`
computer=`hostname`
user=`whoami`
FREMAIL="$user@$computer.$domain"

while getopts "M:m:a:s:r:" opt; do
  case $opt in
        s)
          SUBJECT="$OPTARG - $DATE"
          ;;
        r)
          TOEMAIL="$OPTARG"
          ;;
        m)
          MSGBODY=`cat $OPTARG`
          ;;
        M)
          MSGBODY="$OPTARG"
          ;;
        a)
          ATTACHMENT="$OPTARG"
          ;;
        :)
          showUsage
          ;;
        \?)
          showUsage
          ;;
  esac
done

if [ "$ATTACHMENT" = "" ]; then
        showUsage
        exit 1
fi

MIMETYPE=`file --mime-type -b $ATTACHMENT`
TMP="/tmp/tmpmail_"`date +%N`;
BOUNDARY=`date +%s|md5sum|awk '{print $1;}'`
FILENAME=`basename $ATTACHMENT`

DATA=`cat $ATTACHMENT|base64`

rm $TMP 2> /dev/null

fappend $TMP "EHLO $computer.$domain"
fappend $TMP "MAIL FROM:<$FREMAIL>"
fappend $TMP "RCPT TO:<$TOEMAIL>"
fappend $TMP "DATA"
fappend $TMP "From: $FREMAIL"
fappend $TMP "To: $TOEMAIL"
fappend $TMP "Reply-To: $FREMAIL"
fappend $TMP "Subject: $SUBJECT"
fappend $TMP "Content-Type: multipart/mixed; boundary=\"$BOUNDARY\""
fappend $TMP ""
fappend $TMP "This is a MIME formatted message.  If you see this text it means that your"
fappend $TMP "email software does not support MIME formatted messages."
fappend $TMP ""
fappend $TMP "--$BOUNDARY"
fappend $TMP "Content-Type: text/plain; charset=UTF-8; format=flowed"
fappend $TMP "Content-Disposition: inline"
fappend $TMP ""
fappend $TMP "$MSGBODY"
fappend $TMP ""
fappend $TMP ""
fappend $TMP "--$BOUNDARY"
fappend $TMP "Content-Type: $MIMETYPE; name=\"$FILENAME\""
fappend $TMP "Content-Transfer-Encoding: base64"
fappend $TMP "Content-Disposition: attachment; filename=\"$FILENAME\";"
fappend $TMP ""
fappend $TMP "$DATA"
fappend $TMP ""
fappend $TMP ""
fappend $TMP "--$BOUNDARY--"
fappend $TMP ""
fappend $TMP "."
fappend $TMP "quit"

netcat $mailserver $mailserverPort < $TMP >> $TMP
rc="$?"
if [ "$rc" -ne "0" ]; then
    echo "Returncode: $rc"
    echo "Please inspect $TMP"
else
    rm $TMP;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment