Register
It is currently Tue Sep 30, 2014 5:50 am

Mail sender


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sat Dec 31, 2011 9:25 am   

Joined: Sat Dec 31, 2011 9:06 am
Posts: 1
Hello, I am writing or rather trying to write an email sending script.
How do I want it to look, first I got a file containting email adresses, like this

email@gmail.com [fam]
somedude@gmail.com [f]
MisterT@hotmail.com [w]
black@gmail.com [f]
email5@gmail.com [fam]
etc.
So I got a list and every email has its parameter like [fam] for family so if I chose to send emails to family only I pick fam only.

Next I got a script that actually sends the email I want it to pick only the right emails in a loop like

for email in $(cat emaillist)
do
cat emailcontent | mail -s "Subject" email ( with the parameter I have chosen, one or more or none to send email for the whole list)
done

However I am not sure how to do that because It actually does send whole list as many times as the list has lines. Any help? Thank you for any help in advance and Happy New Year.


Top
 Profile  
 PostPosted: Sat Dec 31, 2011 10:35 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 549
hi,

files are not read using a for loop, because it'll loop on each element output, and you can't be sure of what you'll get.
instead, use a while loop, like this
Code:
while read cat_01 cat_02
do if [ "$cat_01" = "whatIwant" ]
   then echo "$cat_01 is whatIwant"
   fi
done
this is very basic.
as there is only two elements per line, you could easily feed an associative array, and choose family or friends, or else, from the index.

searching the net you'll surely find other scripts like yours: agenda, address book...


Top
 Profile  
 PostPosted: Sat Jan 07, 2012 12:15 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Here you go.

Code:
#!/bin/bash
# Mail specific email addresses which are assigned to a certain category.

if ! type -P mailx > /dev/null; then
    echo 'E: mailx(1) has to be installed to use this script.' >&2
    exit 1
fi

_usage() {
    cat >&2 <<- _USAGE_
    Usage: ./${0##*/} [ARGS]
   
    -l <address_list>      Specify the address list
    -t <address_type>      Specify the address type (eg. fam, f, w)
    -s <email_subject>     Specify email's subject
    -e <email_file>        Specify email's filename
    -v                     Enable verbose mode
    -h                     Display this usage message
    _USAGE_
}

if ((! $#)) || [[ $1 = -h ]]; then
    _usage
    exit 1
fi

while getopts :l:t:s:e:v opts; do
    case $opts in
        l) addr_list=$OPTARG ;;
        t) addr_type=$OPTARG ;;
        s) email_subject=$OPTARG ;;
        e) email_file=$OPTARG ;;
        v) verbose=1
    esac
done

if [[ ! $addr_list || ! $addr_type ||
      ! $email_subject || ! $email_file ]]; then

    _usage
    exit 1
fi

files_exist=1
for f in "$addr_list" "$email_file"; do
    if [[ ! -f $f ]]; then
        printf 'E: could not find the file « %s ».\n' "$f" >&2
        files_exist=0
    fi 
done

if ((! files_exist)); then
    exit 1
fi

while IFS=' []' read -r addr type _; do
    if [[ $type = $addr_type ]]; then
        addr_to_mail+=("$addr")
    fi 
done < "$addr_list"

mailx -s "$email_subject" "${addr_to_mail[@]}" < "$email_file"

if ((verbose)); then
    printf 'The content of « %s » has been sent to the following addresses:\n' "$email_file"
    printf '%s\n' "${addr_to_mail[@]}"
fi



Make sure to put tabs before every line which are in the following part, otherwise it won't work.
Code:
    Usage: ./${0##*/} [ARGS]
   
    -l <address_list>      Specify the address list
    -t <address_type>      Specify the address type (eg. fam, f, w)
    -s <email_subject>     Specify email's subject
    -e <email_file>        Specify email's filename
    -v                     Enable verbose mode
    -h                     Display this usage message
    _USAGE_


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP