Juan Garcés

Personal Blog

Mails with mailx

March 17th, 2013

With this function you can send mails using mailx command.

#!/bin/sh

send_email()
{
  #
  # Proceso de parámetros
  #
  DIRECCION="$1"
  ASUNTO=""
  MENSAJE=""
  if [ $# -ge 2 ]; then
    if [ "$2" != "-" ]; then
      ASUNTO="$2"
    fi
  fi
  if [ $# -ge 3 ]; then
    if [ "$3" != "-" ]; then
      MENSAJE="$3"
    fi
  fi
  ADJUNTO=""
  if [ $# -ge 4 ]; then
    if [ "$4" != "-" ]; then
      ADJUNTO="$4"
    fi
  fi
  #
  # Envío del email
  #
  ( echo "$MENSAJE";
    if [ "$ADJUNTO" != "" ]; then
      archivo=`echo $ADJUNTO|cut -d'~' -f1`
      nombre=`echo $ADJUNTO|cut -d'~' -f2`
      if [[ "$archivo" == "$nombre" || "$nombre" == "" ]]; then
        nombre=`basename $archivo`
      fi
      if [ -f "$archivo" ]; then
        uuencode "$archivo" "$nombre";
      fi
    fi;
  ) |  mailx -s "$ASUNTO" "$DIRECCION"
}

send_email "yourmail@yourdomain.com" "Subject" "Message body" "attachment.txt"

Juan Garcés

Personal Blog