Send Email from command line

Pablo Sanchez pablo at blueoakdb.com
Thu Oct 20 11:36:47 BST 2016


[ Comments below, in-line ]


On Thu, 20 Oct 2016 10:24:41 +0200, Sascha Manns wrote:

> Hello List,
>
> some years ago i read about the possibility to compose an email with Kmail
> from the command line.
> Maybe anyone can explain me, how to do this?
>

Hi,

Please see the attached shell script.  It's not exactly what you're 
asking but better than nothing.

Cheers,

--
Pablo Sanchez - Blueoak Database Engineering, Inc
Ph:    819.459.1926        iNum:  883.5100.0990.1054

-------------- next part --------------
#!/bin/bash

#
# Get the composer up and running.  We may specify 
# the following optional arguments:
#
# o subject
# o attachment(s)
# o cc
# o bcc
# o to
#
# Version:  1.1
# Author:   pablo at blueoakdb.com
#
# Reference(s)
# ------------
# [ http://kb.mozillazine.org/Command_line_arguments_%28Thunderbird%29 ]
# 

#
# Defaults
#

#
# Default our format to plain text
#
FORMAT="format=2,"

#
# End of defaults
#

#
# Collect any arguments
#
while getopts fa:s:c:b:t: my_arg ; do
   case $my_arg in
      s)    SUBJECT="subject='""$OPTARG""'";
            SUBJECT_SEP=",";;
      a)    if echo "$OPTARG" | grep "^/" > /dev/null 2>&1 ; then
               ATTACHMENT_LIST="$OPTARG"$ATTACHMENT_SEP"$ATTACHMENT_LIST"
               ATTACHMENT_SEP=","
            else
               ATTACHMENT_LIST=`pwd`/"$OPTARG"$ATTACHMENT_SEP"$ATTACHMENT_LIST"
               ATTACHMENT_SEP=","
            fi;;
      t)    TO_LIST="${TO_LIST}${TO_SEP}${OPTARG}"
            TO_SEP=",";;
      f)    FORMAT="format=1,";;
      c)    CC_LIST="${CC_LIST}${CC_SEP}${OPTARG}"
            CC_SEP=",";;
      b)    BCC_LIST="${BCC_LIST}${BCC_SEP}${OPTARG}"
            BCC_SEP=",";;
      h|?)  echo "`basename $0` [ -f(ormat HTML) ] [ -a path-to-file ] [-s subject] [-c cc ] [-b bcc ] [-t to ] [ to-list ]"
            echo
            echo "where"
            echo " o multiple e-mail addresses are separated by ','"
            echo " o multiple attachments are specified by repeated -a invocations"
            echo " o default format is text, use -f to toggle to HTML"
            echo
            echo "Examples"
            echo "========"
            echo "Start a message to \`me at example.com' and \`you at example.com'"
            echo "---------------------------------------------------------"
            echo
            echo "   `basename $0` me at example.com you at example.com"
            echo
            echo "Start a message to the above with the Subject \`time for lunch?'"
            echo "---------------------------------------------------------------"
            echo
            echo "   `basename $0` -s \"time for lunch?\" me at example.com you at example.com"
            echo
            echo "Now cc: \`brother' and \`sister' and bcc: \`mom' and \`dad'"
            echo "-------------------------------------------------------"
            echo
            echo "   `basename $0` -s \"time for lunch?\" -b \"dad;mom\" -c \"brother;sister\" me at example.com you at example.com"
            echo
            echo "Start a message with a couple of files:  one file has a space and the other is one directory above"
            echo "--------------------------------------------------------------------------------------------------"
            echo
            echo "   `basename $0` -a a\ b -a ../oracle_index_internals.pdf"
            echo
            exit 1;;
   esac
done

shift $(($OPTIND - 1))

#
# If necessary, create the BCC string
#
if [ -n "$BCC_LIST" ] ; then
   BCC="bcc='""$BCC_LIST""'";
fi

#
# If necessary, create the CC string
#
if [ -n "$CC_LIST" ] ; then
   CC="cc='""$CC_LIST""'";
fi

#
# If necessary, create the CC string
#
if [ -n "$TO_LIST" ] ; then
   TO="to='""$TO_LIST""'";
fi

#
# Implicity, we allow a to-list (no -t required)
#
if [ $# -gt 0 ] ; then
   _TO=`echo $@ | sed 's/ /,/g'`

   if [ -z "$TO" ] ; then
      TO="to='""$_TO"
   else
      #
      # Remove the trailing quote
      #
      TO=`echo $TO | sed "s/'$//"`

      TO="$TO"$TO_SEP"$_TO"
   fi

   #
   # Terminate the to-string
   #
   if [ -n "$TO" ] ; then
      TO="$TO""'"
   fi
fi

#
# Paste together our attachments
#
if [ -n "$ATTACHMENT_LIST" ] ; then
   ATTACHMENTS="attachment='"$ATTACHMENT_LIST"'"
fi 

MY_ARGUMENTS="${FORMAT}${SUBJECT}${SUBJECT_SEP}${ATTACHMENTS}${ATTACHMENT_SEP}${CC}${CC_SEP}${BCC}${BCC_SEP}${TO}${TO_SEP}"

#
# Remove a straggling comma
#
MY_ARGUMENTS=`echo $MY_ARGUMENTS | sed 's/,$//'`

#
# Debug
#
# echo $MY_ARGUMENTS

`eval thunderbird -compose \"$MY_ARGUMENTS\"`

exit 0


More information about the kdepim-users mailing list