Actions

 Langue:
 Flux RSS:


JMail - Envoi de mails avec pièce jointe


Présentation

    Ce script est l'équivalent de la commande mailx, mais ajoute la possibilité d'envoyer des fichiers en pièce jointe au message.
     
    La syntaxe est la suivante:
      jmail -s "Sujet du mail" [-sender adresse_mail_emetteur] [-html] [-cc adresse_mail_en_copie] [-j fichier_a_attacher] adresse_mail_destinataire
      L'option -html permet d'envoyer un mail au format html
      L'option -cc permet d'ajouter des personnes en copie du message

Le script

    #!/bin/ksh
    # Version 1.00 - 09/07/2000
    # Auteur: Jerome DESMOULINS
    USAGE="Usage: $0 [-s 'Subject'] [-sender email_of_sender] [-html] [-cc E-mail] [[-j File] ...] E-mail [[E-mail] ...]"
    Mail_hub=`hostname `
    Mail_user=$LOGNAME
    Boundary_mark="GM_Fecit"
    Subject="De `uname -n | cut -d'.' -f1`"
    Sender_address="$Mail_user@$Mail_hub"
    Format="text"
    
    export TERM=vt100
    
    
    # Analyse des parametres
    while [ "$*" != "" ]; do
            case $1 in
        "-s") Subject=$2; shift 2;;
        "-sender") Sender_address=$2; shift 2;;
        "-j") [ -r $2 ] || { echo "$0: Ne peut lire $2"; exit 1; }
              File_lst="$File_lst $2"; shift 2;;
        "-html") Format="html"; shift;;
        "-cc") Mail_cc="$Mail_cc $2"; shift; shift;;
        *@*) Mail_lst="$Mail_lst $1"; shift;;
            esac
    
    done
    
    [ -z "$Mail_lst" ] && { echo "$USAGE\r  You must specify at least one !";
    exit 1; }
    
    ( sleep 1
      echo "HELO $Mail_hub";
      if test $Sender_address = "$Mail_user@$Mail_hub"
      then
    
        echo "MAIL FROM: <$Mail_user@$Mail_hub>"; sleep 1
      else
        echo "MAIL FROM: <$Sender_address>"; sleep 1
      fi
    
      for MailAddress in $Mail_lst; do
          echo "RCPT TO: <$MailAddress>";
      done
      echo "DATA"; sleep 1
      if test $Sender_address = "$Mail_user@$Mail_hub"
      then
    
        echo "From: $Mail_user@$Mail_hub ($0)"
      else
        echo "From: $Sender_address ($0)"
      fi
      echo "Subject: $Subject"
      echo "To: `echo $Mail_lst| tr ' ' ';'`"
      echo "Cc: `echo $Mail_cc| tr ' ' ';'`"
      echo "Date: `date `"
      echo "MIME-Version: 1.0"
      echo "X-Mailer: $0"
      echo "Content-Type: multipart/mixed; boundary=\"$Boundary_mark\""
      echo
    
      echo "--$Boundary_mark"
      echo "Content-Type: text/$Format"
      echo ""
      # Le message
      cat <&0 | sed -e 's/^\.$/\.\./'
      echo "\n\n\n"
      # Les pieces jointes
    
      for File in $File_lst; do
          File_name=`basename $File`
          echo "--$Boundary_mark"
          echo "Content-Type: text/plain; name=\"$File_name\""
          echo "Content-Disposition: attachment; filename=\"$File_name\""
          echo ""
          cat $File | sed -e 's/^\.$/\.\./'
          echo "--$Boundary_mark"
      done
    
      echo "."; sleep 1
      echo "QUIT"; sleep 1
      echo "O\003"
    ) | telnet $Mail_hub 25 > /dev/null 2>&1
    
    exit 0
    

 

Retour