Sure that's possible. Assuming you have a .my.cnf in the home directory of the user that will execute the script. This to make sure the script won't ask for a password, and also there's no password inside the script. Also make sure the file has the proper rights so only root and the user can read the script:
shell$ ls -l ~/.my.cnf
-rw------- 1 zaubara zaubara 41 Nov 12 2003 /home/zaubara/.my.cnf
shell$ cat ~/.my.cnf
Now you can make a shell-script like this:
QUERY="select email from mailtable"
echo "$QUERY" | mysql yourdatabase | while read address ; do
cat filewithmessage.txt | mailx -s "some subject" "$address"
- For sending mail this way, you need to have a MTA (mailserver, e.g. sendmail) running on the system
- If you don't have the "mailx" program, try "mail".