Hi,
One thing to think about here is whether e-mailing these logins in the clear is actually the best way to distribute usernames and passwords to your users. Depending on your setup, there are likely to be various points at which someone could potentially sniff these messages in clear text as they're passing by, and thusly obtain your users' credentials.
However, if you are set on doing things this way, here's a solution.
Firstly, I've made a slight change to the format of letter.txt, so that it now looks like this:
$ cat letter.txt
Hello,
Here is your login : _USERNAME_ and your password : _PASSWORD_
cordially
$
So I'm using _USERNAME_ and _PASSWORD_ as placeholders here for what will be the real login details, as we'll see in a bit.
As for your maillist.txt, that's basically the same:
$ cat maillist.txt
unixforum password-U unixforum@localhost
root password-R root@localhost
$
So the first field is the username to be e-mailed, the second is the password to be e-mailed, and the third is the e-mail address to which the details should be sent.
Lastly, here's the actual script itself:
$ cat my_script.sh
#!/bin/bash
cat maillist.txt | while read -r line
do
username=`echo $line | awk '{print $1}'`
password=`echo $line | awk '{print $2}'`
email=`echo $line | awk '{print $3}'`
cat letter.txt | sed -e s/_USERNAME_/$username/g -e s/_PASSWORD_/$password/g | mail -s "Your subject" $email
done
$
So the idea is that we read in each line of maillist.txt, break out the username, password and e-mail address, do a search-and-replace in letter.txt to swap out the underscore-bracketed placeholders for the real details, then we e-mail them to the address on file.
And here's a sample session of it being run:
$ whoami
unixforum
$ ./my_script.sh
$ mail
"/var/mail/unixforum": 1 message 1 new
>N 1 unixforum@localhost Sat Feb 11 10:28 17/719 Your subject
? 1
<e-mail headers appear here, redacted>
Hello,
Here is your login : unixforum and your password : password-U
cordially
? d 1
? q
Held 0 messages in /var/mail/unixforum
$
Hope this helps.