The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" iBot UNIX and Linux RSS News 0 01-04-2008 03:00 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
grep to find content in between curly braces, "{" and "}," keshav_rk Shell Programming and Scripting 4 08-09-2007 10:14 PM
how could i make a program mixed with many "|", "<" and ">" strugglingman High Level Programming 2 04-29-2006 08:11 AM
No utpmx entry: you must exec "login" from lowest level "shell" peterpan UNIX for Dummies Questions & Answers 0 01-18-2006 04:15 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-29-2002
plelie2 plelie2 is offline
Registered User
  
 

Join Date: Jul 2002
Location: Belgium
Posts: 27
Angry how to request a "read" or "delivered" receipt for mails

Dears,
I've written a script which allows me to send mails in different formats with different attaches. Now I still want to add a feature to this script. My users would like to be able to receive a "read" or "delivered" receipt for their mails.
The script send mails on behalve of an specific exchange user and is triggered from a web page or another script or a DB. The user (not the unix account) on which behalf the mail will be sent needs confirmation about it. Which codes do I need to use to implement this request ?

P.S.: any enhancements are also welcom. For example the trick I do with mpack, isn't there a more easy way to convert a binary ?
Something in the way of input stream | converter | output stream ?


Underneath you find the script.
Code:
#!/usr/bin/ksh 

########################################
# Script to send mails in HTML or TEXT, with or whitout attach 
# Author : LP 
# Syntax : mailto.sh <TYPE> <SUBJECT> <BODY> <TO_ADDRES> [FROM_ADDRES] [<ATTACHEMENT> <ATTACH_TYPE>] 
########################################

### vars 
################## 
export min_para=4 
export log=`date +%d%m%Y%H%M%S`.$#.log 
first_attach=0 

### functions 
################## 

function Error 
{ 
mailx -s "ERROR TO SEND MAIL " $FROM_ADDRES < $log; 
rm $log; 
exit 1; 
} 

function lower 
{ 
echo $1 | tr [:upper:] [:lower:]; 
} 

function upper 
{ 
echo $1 | tr [:lower:] [:upper:]; 
} 

function Syntax 
{ 
echo "Syntax = mailto.sh <TYPE> <SUBJECT> <BODY> <TO_ADDRES> [FROM_ADDRES] [<ATTACHEMENT> <ATTACH_TYPE>]"; 
echo "Where TYPE = \"t\" for text only mails"; 
echo " \"h\" for html only mails"; 
echo " \"ta\" for text with an attachement"; 
echo " \"ha\" for html with an attachement"; 
echo "You MUST provide a TYPE, this is Mandatory"; 
echo "Where SUBJECT = the subject of the mail (Mandatory)"; 
echo "Where BODY = the name of the file which contains the body for the mail (Mandatory)"; 
echo "Where TO_ADDRES = the mailadress of the recipients (Mandatory)"; 
echo " you can provide as many adresses as you like."; 
echo" in this case you need to enclose the adresses with double quotes"; 
echo" and seperate them with a space."; 
echo " example: \"adres1@exchage.bc adres@aserver.com adres3@belgacom.be\""; 
echo "Where FROM_ADDRES = the mailadress of the sender (Optional)"; 
echo " If not provided, the adres of the unix account will be used."; 
echo "Where ATTACHEMENT = the attachement that has to be sent with the mail (Optional)"; 
echo "Where ATTACH_TYPE = the type of attachement that has to be sent with the mail (Optional)"; 
echo " this type can be set to \"t\" (plain text files) "; 
echo " this type can be set to \"b\" (binary files) "; 
echo " If the type is not provided it will be set to \"t\""; 
echo "The attachement together with the type is 1 parameter you must provide both between quotes"; 
echo " example : \"attach1.txt t\" \"attach2.exe b\" \"attach3.xls b\""; 
echo "----------------------------------------------------------------------------------------------------------------------"; 
echo "Example : mailto.sh ta \"test om te testen\" a_body.txt id079725@exchange.bc patrick@e-lelie.be \"attach.txt t\";"; 
echo "----------------------------------------------------------------------------------------------------------------------"; 
} 

function gres 
{ 
if [ $# -lt "3" ] 
then 
  echo "Usage: gres pattern replacement file"; 
  exit 1; 
fi 
pattern=$1; 
replacement=$2; 
if [ -f $3 ] 
then 
  file=$3; 
else 
  echo "$3 is not a file"; 
  exit 1; 
fi; 
A="`echo | tr '\012' '\001' `"; 

sed -e "s$A$pattern$A$replacement$A" $file; 
} 

function striphtml 
{ 
gres "<[^>]*>" " " $1 > $1.txt; 
grep "<[^>]*>" $1.txt >/dev/null 2>&1; 
while (( $? == 0 )) 
do 
  gres "<[^>]*>" " " $1.txt > $1.tmp; 
  mv $1.tmp $1.txt; 
  grep "<[^>]*>" $1.txt >/dev/null 2>&1 
done 
} 

function mailhead 
{ 
echo "From: $1"; 
recipient=`echo $2 | tr -s " " | tr " " ";"` 
echo "To: $recipient"; 
echo "Subject: $3"; 
echo "Mime-Version: 1.0"; 
} 

function pack_attach 
{ 
while (( $# > 1 )) 
do 
  ATTACH=$1 
  ANAME=`basename $ATTACH` 
  ATYP=$2 
  echo "------NextPart" 
  echo "Content-Type: application/octet-stream;" 
  echo " name=\"$ATTACH\"" 
  if [[ "$ATYP" = [bB] ]] 
  then 
    echo "Content-Transfer-Encoding: base64" 
    mpack -s "" -o $ATTACH.enc $ATTACH 
    lines=`cat $ATTACH.enc | wc -l` 
    x=0 
    while read line 
    do 
      (( x=$x+1 )) 
      if (( $x > 14 && $x < $lines - 1 )) 
      then 
        echo $line >> $ATTACH.tmp 
      fi 
    done < $ATTACH.enc 
    mv $ATTACH.tmp $ATTACH.enc 
    echo "Content-Disposition: attachment;" 
    echo " filename=\"$ANAME\"" 
    echo "" 
    cat $ATTACH.enc 
    rm $ATTACH.enc 
    echo "" 
    shift 
    shift 
  else 
    if [[ "$ATYP" = [tT] ]] 
    then 
      echo "Content-Transfer-Encoding: 7 bit" 
      echo "Content-Disposition: attachment;" 
      echo " filename=\"$ANAME\"" 
      echo "" 
      cat $ATTACH 
      echo "" 
      shift 
      shift 
    else 
      echo "Content-Transfer-Encoding: 7 bit" 
      echo "Content-Disposition: attachment;" 
      echo " filename=\"$ANAME\"" 
      echo "" 
      cat $ATTACH 
      echo "" 
      shift 
    fi 
  fi 
done 
} 

function textonly 
{ 
echo "Content-Type: text/plain;"; 
echo " charset=\"ISO-8859-1\""; 
echo ""; 
cat $1; 
} 

function htmlonly 
{ 
echo "Content-Type: multipart/alternative;"; 
echo " boundary=\"----NextPart\""; 
echo ""; 
echo "This message is in MIME format. Since your mail reader does not understand"; 
echo "this format, some or all of this message may not be legible."; 
echo ""; 
echo "------NextPart"; 
echo "Content-Type: text/plain;"; 
echo " charset=\"ISO-8859-1\""; 
echo "" 

striphtml $1; 
cat $1.txt; 
rm $1.txt; 

echo "------NextPart"; 
echo "Content-Type: text/html;"; 
echo " charset=\"ISO-8859-1\""; 
echo "Content-Transfer-Encoding: quoted-printable" 
echo ""; 
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"; 

cat $1; 

echo "" 
echo "------NextPart--"; 
} 

function textplus 
{ 
echo "Content-Type: multipart/mixed;" 
echo " boundary=\"----NextPart\"" 
echo "" 
echo "This message is in MIME format. Since your mail reader does not understand" 
echo "this format, some or all of this message may not be legible." 
echo "" 
echo "------NextPart" 
textonly $1 
echo "" 
shift 
pack_attach $@ 

echo "------NextPart--" 
} 

function htmlplus 
{ 
echo "Content-Type: multipart/mixed;" 
echo " boundary=\"----NextPart\"" 
echo "" 
echo "This message is in MIME format. Since your mail reader does not understand" 
echo "this format, some or all of this message may not be legible." 
echo "" 
echo "------NextPart" 
htmlonly $1 
echo "" 
shift 
pack_attach $@ 

echo "------NextPart--" 
} 

function sendFile 
{ 
mailtje=mail`date +%H%M%S%d%m%m%y`.msg 
mailhead $FROM_ADDRES "$TO_ADDRES" "$SUBJECT" > $mailtje; 

case $TYPE in 
t) textonly $BODY >> $mailtje;; 
h) htmlonly $BODY >> $mailtje;; 
ta) textplus $BODY $ATTACHMENTS >> $mailtje;; 
ha) htmlplus $BODY $ATTACHMENTS >> $mailtje;; 
esac; 
cat $mailtje | /usr/sbin/sendmail $TO_ADDRES 
rm $mailtje 


} 

# Assign parameters 
#################### 

TYPE=`lower $1`; 
SUBJECT=$2; 
BODY=$3; 
TO_ADDRES=$4; 
FROM_ADDRES=$5; 

# Check parameters 
################## 

x=0 
if (( $# >= $min_para )) 
then 
  echo "Number of parameters is acceptable" > $log 
  else 
  echo "Wrong number of parameters" >> $log 
  echo "$# parameters given" >> $log 
  echo "Given Parameters : $@ " >> $log 
  (( x=$x+1 )) 
fi 

if [[ "$TYPE" = "t" || "$TYPE" = "h" || "$TYPE" = "ta" || "$TYPE" = "ha" ]] 
then 
  echo "Type has correct value" >> $log 
else 
  echo "Type has incorrect value" >> $log 
  (( x=$x+1 )) 
fi 

if [[ -f $BODY ]] 
then 
  echo "Body exists" >> $log 
else 
  echo "Body doesn't exist" >> $log 
  (( x=$x+1 )) 
fi 

checka=`echo $TO_ADDRES | cut -d "@" -f1` 
if [[ "$TO_ADDRES" = "$checka" ]] 
then 
  echo "Adres is not valid" >> $log 
  (( x=$x+1 )) 
else 
  echo "Adres is valid" >> $log 
fi 

if [[ "$FROM_ADDRES" = "" ]] 
then 
  mach=`uname -n` 
  user=`whoami` 
  FROM_ADDRES=$user@$mach.bc 
  first_attach=5 
else 
  if [[ -f `echo $FROM_ADDRES | cut -d" " -f1` ]] 
  then 
    mach=`uname -n` 
    user=`whoami` 
    first_attach=5 
    FROM_ADDRES=$user@$mach.bc 
  else 
    first_attach=6 
  fi 
fi 

par=1 
while (( $par < $first_attach )) 
do 
  (( par=$par+1 )) 
  shift 
done 

if [[ "$TYPE" = "ta" || "$TYPE" = "ha" ]] 
then 
  ATTACHMENTS="" 
  while (( $# > 0 )) 
  do 
    if [[ -f `echo $1 | cut -d" " -f1` ]] 
    then 
      ATTACHMENTS=`echo "$ATTACHMENTS $1"` 
    else 
      echo "this attach \"$1\" is NOT found" >> $log 
      (( x=$x+1 )) 
    fi 
    shift 
  done 
fi 

if (( $x > 0 )) 
then 
  Syntax >> $log 
  Error 
fi; 

# Send the mail 
################### 

sendFile 
rm $log
  #2 (permalink)  
Old 08-06-2002
PxT's Avatar
PxT PxT is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2000
Location: Sacramento, CA
Posts: 909
Re: how to request a "read" or "delivered" receipt for mails

Quote:
Originally posted by plelie2
Dears,
I've written a script which allows me to send mails in different formats with different attaches. Now I still want to add a feature to this script. My users would like to be able to receive a "read" or "delivered" receipt for their mails.

Please see: http://www.sendmail.org/~ca/email/dsn.html
Sponsored Links
Closed Thread

Bookmarks

Tags
mailx, mailx attachment

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0