0

I have table named student in a mysql database. This table has column named email.

I use following command to send email to specific student, identified by pid:

$email=$(mysql --login-path=local --database=ccps -ss -e "select email from student where pid=132054");   
echo "This is sample mail" | mail -s "Test" $email

I want to know how to send email to each and every student in the database. As I don't know how to use loop on column email.

Thank You.

1
  • Using mail like this is an extremely ugly way of doing it, you really should use a scripting language that can compose full email messages, but if you're just testing then the trick is xargs. Commented Apr 5, 2017 at 20:13

2 Answers 2

1

For playing around, something like this would work for you. But there are much better ways of doing this.

read -ra student_emails <<< $(mysql --login-path=local --database=ccps -ss \
                                     -e "select email from student order by pid;")
for email in ${student_emails[@]}
do
    echo "This is sample mail" | mail -s "Test" ${email[0]}
done
Sign up to request clarification or add additional context in comments.

Comments

0

The most direct way I can think of is:

for email in `mysql --login-path=local --database=ccps -ss -e "select email from student order by pid;"`
do
        echo "This is sample mail" | mail -s "Test" $email
done

If you haven't come across them before, the quotes just before mysql and at the end of the command are the left-handed ones, not the usual single quotes. As an earlier comment says, something like PHP would be better/easier at constructing more sophisticated emails.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.