Mailx recipient from mysql database

Dear All,

Can I make Mailx to read recipient address from a mysql database?

I already tried emailing with bash script:

SUBJECT="TEST"
export EMAIL_ADDRESS=`mysql -uroot -pabcde smsd -e "SELECT email FROM recipient"`
mysql -uroot -pabcde smsd -e "SELECT ID, SenderName, Body FROM inbox" | mailx -s "$SUBJECT" -a From:sales@acme.com "$EMAIL_ADDRESS�

It return this error:

export: 13: supervisor@gmail.com: bad variable name

the table has 2 row with two email address each.

but no luck. Any help would be very appriciated. Thanks

Not sure why you are getting that error. But why do you need export here? Just assign the email address to that variable and use it. Execute the script using -x option i.e. bash -x yours_script , this might give us something to look into.

--ahamed

i need export because i need it to send email to multiple email accounts (using mysql database) that being registered with a software that I developed. is there any better way?

thanks

Yes I get it, but here you dont need export, just a variable should do it.
Did you try without export? how about the requested output with -x option?

--ahamed

thanks Ahamed,

using the same script like before with -x option, it stil gives me this error:

export: 13: supervisor@gmail.com: bad variable name

I dont know how to change it with variable, please help me with the code, im new with bash script.

Just remove the keyword export from your code and try.

--ahamed

Do you mean like this:

SUBJECT="TEST"
EMAIL_ADDRESS=`mysql -uroot -pabcde smsd -e "SELECT email FROM recipient"`
mysql -uroot -pabcde smsd -e "SELECT ID, SenderName, Body FROM inbox" | mailx -s "$SUBJECT" -a From:sales@acme.com "$EMAIL_ADDRESS�

output:

root@testapp:/etc# sh -x daemon_emailer.sh
+ SUBJECT=TEST
+ mysql -uroot -pabcde smsd -e SELECT email FROM recipient
+ EMAIL_ADDRESS=email
supervisor@gmail.com
marketing@gmail.com
daemon_emailer.sh: 14: Syntax error: Unterminated quoted string

Yes, but we need more modifications. I dont have a mysql setup right now. So can you paste the output of

mysql -uroot -pabcde smsd -N -s -r -e SELECT email FROM recipient

We basically want to remove the header and new line from the sql ouput.

--ahamed

---------- Post updated at 01:44 AM ---------- Previous update was at 01:42 AM ----------

Also, please check the double quotes at the end after EMAIL_ADDRESS.

--ahamed

yes, you're right. we need to remove the new line :smiley:

i do this:

mysql -uroot -pabcde smsd -N -s -r -e "SELECT email FROM recipient"

output:

supervisor@gmail.com
marketing@gmail.com

Try this and if you still get any errors, please paste the output with -x option

EMAIL_ADDRESS=$( mysql -uroot -pgurlia001 smsd -N -s -r -e "SELECT email FROM recipient" | tr '\n' ' ')

--ahamed

change script to:

SUBJECT="TEST"
EMAIL_ADDRESS=$( mysql -uroot -pgurlia001 smsd -N -s -r -e "SELECT email FROM recipient" | tr '\n' ' ')
mysql -uroot -pgurlia001 smsd -e "SELECT ID, SenderName, Body FROM inbox" | mailx -s "$SUBJECT" -a From:sales@acme.com "$EMAIL_ADDRESS�

now in one line but with error, output:

+ SUBJECT=TEST
+ mysql -uroot -pgurlia001 smsd -N -s -r -e SELECT email FROM recipient
+ tr \n  
+ EMAIL_ADDRESS=supervisor@gmail.com marketing@gmail.com  
daemon_emailer.sh: 15: Syntax error: Unterminated quoted string
EMAIL_ADDRESS="$( mysql -uroot -pgurlia001 smsd -N -s -r -e "SELECT email FROM recipient" | tr '\n' ' ')"

--ahamed

---------- Post updated at 02:17 AM ---------- Previous update was at 02:15 AM ----------

Sorry, updated the code. Please try this.

--ahamed

1 Like

already changed the script with:

SUBJECT="TEST"
EMAIL_ADDRESS="$( mysql -uroot -pgurlia001 smsd -N -s -r -e "SELECT email FROM recipient" | tr '\n' ' ')"
mysql -uroot -pgurlia001 smsd -e "SELECT ID, SenderName, Body FROM inbox" | mailx -s "$SUBJECT" -a From:sales@acme.com "$EMAIL_ADDRESS�

output:

+ SUBJECT=TEST
+ tr \n  
+ mysql -uroot -pgurlia001 smsd -N -s -r -e SELECT email FROM recipient
+ EMAIL_ADDRESS=supervisor@gmail.com marketing@gmail.com 
daemon_emailer.sh: 15: Syntax error: Unterminated quoted string

Did you notice the double quote in the last statement(...|mailx..) i.e. after $EMAIL_ADDRESS? is it a copy paste error or is there something wrong in the script itself?

--ahamed

1 Like

already checked it twice, already use double quote :

EMAIL_ADDRESS="$( mysql -uroot -pgurlia001 smsd -N -s -r -e "SELECT email FROM recipient" | tr '\n' ' ')"

and for mailx:

mysql -uroot -pgurlia001 smsd -e "SELECT ID, SenderName, Body FROM inbox" | mailx -s "$SUBJECT" -a From:sales@acme.com "$EMAIL_ADDRESS�

---------- Post updated at 04:39 AM ---------- Previous update was at 04:38 AM ----------

ups... do you mean:

mysql -uroot -pgurlia001 smsd -e "SELECT ID, SenderName, Body FROM inbox" | mailx -s "$SUBJECT" -a From:sales@acme.com $EMAIL_ADDRESS

?

---------- Post updated at 04:42 AM ---------- Previous update was at 04:39 AM ----------

YES IT WORKS! Thank You Brother! now it can send email to multiple email accounts using database. thank you very much