#!/bin/bash ## sillymail ## simple 'telnet' email client ## exercise in bash, except, smtp, ... ## # requires expect # mimencode (for attachments), .deb=metamail # # Koen Noens, november 2008 # This is free software under the terms and conditions of GPL v3 ## TODO : mechanism to take command line arguments # handle script errors # modify expect script so that it not only handles SMTP "250 OK" # # param SERVER="$1" PORT=25 RELAY="$SERVER $PORT" ## params for attachment withAttach="n" MIME="MIME-Version: 1.0 " ENCODING="Content-transfer-encoding: base64; " CONTENTTYPE="Content-Type: multipart/mixed; " BOUNDARY="000MultipartBoundary000MultipartBoundary0000" ATTACH_TYPE="Content-Type: application/octet-stream; " ATTACH_NAME="" ATT_ENCODED="" BODY=$(mktemp) ATT_ENCODED=$(mktemp) ## User Interface echo; echo -n "HELO: "; read HELO; echo -n "mail from: "; read FROM; echo -n "mail to: "; read TO; echo -n "subject: "; read SUBJ; echo -n "with attachment [yn]? "; read withAttach; if [ "$SERVER" = "" ]; then echo -n "mail server: " ; read SERVER fi usage (){ echo; echo "$0 server" echo "where server is the fqdn of a mail server" } getAttachment () { echo; echo -n "attachment: attach file (path): " ; read ATTACH; if [ -f $ATTACH ]; then echo "encoding and attaching $ATTACH .... " mimencode $ATTACH -o $ATT_ENCODED ATTACH_NAME=$(basename $ATTACH) else echo "$ATTACH : file not found, aborting" exit 1; fi } ## create mail msg echo "write message. Ctrl+D when Done"; echo (cat > $BODY) if [ "$withAttach" = "y" ]; then getAttachment #modify body to multi-part BODYTEMP=$(mktemp) cat $BODY > $BODYTEMP; rm $BODY BODY=$(mktemp) echo "$MIME" >> $BODY echo -e "$CONTENTTYPE boundary=\\\"$BOUNDARY\\\"" >> $BODY #From, to, can be repeated here for mail client display #CC and BCC can be put here if the have a RCPT TO in envelop echo -e "Subject: $SUBJ \n\n" >> $BODY #subject line marks the end of the extra headers in DATA #newlines are significant in boundaries, don't change. ### text part ### echo -e "\n--$BOUNDARY\n\n" >> $BODY cat $BODYTEMP >> $BODY ### attachment ### echo -e "\n--$BOUNDARY" >> $BODY echo "$ENCODING" >> $BODY echo -e "$ATTACH_TYPE name=$ATTACH_NAME \n" >> $BODY cat $ATT_ENCODED >> $BODY ### body end ### echo -e "\n--$BOUNDARY--\n" >> $BODY # # # # # Attachements also require additional mail headers # so there's another withAttach==y section in # the protocol section of this script # # # # fi ## do protocol with expect expect <