[e-mailing] send login pwd to several users

Hello,
I would like to send logins and passwords to users.
As input I have a file where there are the login, passwords and email addresses.

Can you help me because I'm not a good scripter
cordially

cat maillist.txt$1          $2            $3
login1    password1 @mail1
login2    password2 @mail2
login3    password3 @mail3
cat letter.txt
Hello,
Here is your login : $1 and your password : $2
cordially
vi my_script.sh#!/bin/bash
cat maillist.txt|while read user
do
cat letter.txt|mail -s "Your subject" $3
done
exit

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.

You CAN use $1 and $2 in your files when taking preventive measures, but it normally is not wise to use reserved words or similar. Slightly adapting drysdalk's proposal, try:

while read username password email
  do sed "s/\$1/$username/; s/\$2/$password/;" file1 | mail -s "Your subject" $email
  done < file2