Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to track sent emails 1

Status
Not open for further replies.

marym26

Programmer
Feb 16, 2002
5
US
Please help!!!
I have to create a bash script which mails the contents of a file to other users. I have to also create another file in my home directory which contains a log of the emails that I have sent. The script is supposed to append to this file the current date and time followed by the email address of the person receiving the file. I'm supposed to used 2 parameters - one of the file to be sent and the other the email address the file is to be sent to. How do I do this? It's supposed to be very simple to do - just a few lines - but I'm going nuts trying to figure it out. Thanks for any help anybody can give me.
 
Something like this should work:
[tt]
for person in list of emails
do
cat file | mail $person
echo `date` >> log
echo $person >> log
cat file >> log
done
[tt]
Cheers, Neil
 
Neil - I appreciate your help but I'm a total newbie and still have some questions. I assume the $person is a variable I have to assign. How???? Also how do you create a list of emails? When I cat the file is this file the file I'm sending in the emails or a file I've created for the tracking process? Thanks.
 
OK. So if your email list is stored in a file called [tt]emails.txt[/tt] like:
Code:
fred@xyz.com
bill@abc.com
Then you can use:
[tt]
EMAILS_FILE=/some/path/emails.txt
MESSAGE_FILE=/some/path/message.txt
LOG_FILE=/some/path/mail.log

while read EMAIL
do
cat $MESSAGE_FILE | mail $EMAIL
echo `date` >> $LOG_FILE
echo $EMAIL >> $LOG_FILE
cat $MESSAGE_FILE >> $LOG_FILE
done < $EMAILS_FILE
[/tt]
The [tt]$EMAILS_FILE[/tt] is piped into the [tt]while[/tt] command, so [tt]$EMAIL[/tt] is set each time.
Depending on your needs, you could have the message file as an argument to your script instead of hardcoding this.
Cheers, Neil
 
Neil - I've got the date working. But I'm still having problems with the name of the email recipient. What's comimg up is the path name to email (/home/myname/email). What am I doing wrong? I don't understand the while/do. The system I'm using isn't accepting that command. It' a bash shell if that helps. I was thinking of using cat to get the name in but the problem there is that sent email is currently not being saved into a file and I don't know how to do this. Thanks once again.
 
If you are familiar with other language syntaxes, then you can think of [tt]do[/tt] as an opening brace [tt]{[/tt] and [tt]done[/tt] as a closing brace [tt]}[/tt]. The [tt]<[/tt] symbol on the last line sends the contents of the [tt]$EMAILS_FILE[/tt] file to the [tt]standard input[/tt] of the [tt]while[/tt] loop. The [tt]read[/tt] command simply reads a single line from the [tt]standard input[/tt] and stores this value in [tt]$EMAIL[/tt].
If you could post your code snippet, we can have a look to see if there are any bugs...

Cheers, Neil :)
 
Hi Neil
the script so far is:
set EMAILS_FILE = marym26@aol.com
set MESSAGE_FILE = /home/myname/letter
set LOG_FILE = /home/myname/mail.log
cat $MESSAGE_FILE | mail $EMAILS_FILE
echo 'date' >> $LOG_FILE
echo $EMAILS_FILE >> $LOG_FILE

The output when I cat mail.log is:
the date info
my aol address

That's exactly how I want the output but I don't understand how to assign more than one email address so that the output lists date name date name date name.

I had put the email addresses in a file and assigned that file to the EMAILS_FILE but that didn't work.

Thanks
 
The [tt]set var = val[/tt] style is more [tt]csh[/tt] than [tt]bash[/tt]. Try the [tt]var=val[/tt] style instead. Note there should be no spaces in the assignment. Try a simple example to make sure you are reading in the emails correctly, eg:
Code:
EMAILS_FILE=/some/path/emails.txt
while read EMAIL
do
    echo &quot;found $EMAIL&quot;
done < $EMAILS_FILE
From there, replace the echo with the commands you need.
Cheers, Neil
 
Neil
My brain has finally started to grasp what exactly is going on and I've got the program working.
Thank you for all your help and patience.
M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top