Send email to all users in a MySQL database - mysql

I can query our MySQL database to get the email address of all registered user accounts. I want to send an email to every user, one by one.
Query
SELECT email FROM users
Output
email
-------
email#domain.com
email#domain.com
email#domain.com
Email
cat <<EOF | sendmail -t
To: %%u.email%%
Subject: Testing
From: sender#example.com
This is a test message
EOF
How can I use the email addresses returned from the MySQL database instead of %%u.email%%?

Giving you have defined sendmail and query scripts:
while read -r email; do ./sendmail "${email}"; done < <(./query | head -n +2)

Related

Saving sip registration/unregistration in Asterisk into MySQL

How can I save sip accounts registration/unregistration into MySQL?
I have only one idea at the moment ...
Now I'm trying to parse asterisk's full log:
cat /var/log/asterisk/full | grep "egistered SIP " | awk '{print $1,$2,$3,$6,$7,$8}' | sort -t" " -k2 -n
and saving information into table:
sip_registrations (id, sip, datetime, event[1 - registration; 2 - unregistration] )
Is there any easier way?
Thanks.
You can use sip realtime(peers from db) and set it to update state of peer.
You can listen AMI interface for event.
You can read logs and parse.

SQL Errors using like and %

My script is designed to cycle through environments, hit a database and check to see if any first/last name combination has an associated user ID. I am not sure why I'm getting the error listed below.
#/bin/bash
#specify user to search for *******DO NOT PUT SPACE, ie. jaylefler
echo "Please enter user first name: "
read first_name
echo "Please enter user last name: "
read last_name
echo "Please enter LDAP User ID: "
read ldapuser
echo "Please enter LDAP password: "
stty -echo
read ldappw
stty echo
#logs to write output to
log="ui_${username}_access.log"
finallog="${username}_ui_access.txt"
#create file if not exist, else null it
[[ -f ${log} ]] && cat /dev/null > ${log} || touch ${log}
[[ -f ${finallog} ]] && cat /dev/null > ${finallog} || touch ${log}
#log it all
{
echo "environment"
sshpass -p $ldappw ssh $ldapuser#54.123.777.567 'mysql -h host -u user - ppassword database -e \
"select user_id from users where first like "%'${first_name}'%" and last like "%'${last_name}'%";"'
} > $log
When I execute the script, I receive the following errors:
Please enter user first name:
jay
Please enter user last name:
lefler
Please enter LDAP User ID:
jlefler
Please enter LDAP password:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%jay% and last like %lefler%' at line 1
jlefler#ubuntu:~/Desktop$
I'm not that familiar with this type of scripting, but it would seem to me that around %jay% and %lefler% there would need to be quotations so that the SQL appears as:
select user_id from users where first like '%jay%' and last like '%lefler%';
The primary problem in your code is that the quotation marks are inside of the percent signs. :)
The second problem is that you have created something that is vulnerable to SQL injection. I have absolutely no idea how to create bound parameters from a Bash script, but you need to figure that out!
The query to MySQL is supposed to output something like this:
SELECT * FROM pet WHERE name LIKE '%w%';
In bash one of the way to concatenate is as follow:
mystring="some ${string1} arbitrary ${string2} text"
EDIT:
So combining all Plus making somethin to expand the strings inside ''. The problem is that shell Can not expand strings between single quotations marks.
Something like this should work:
CommandFinale=$($sqlCommand -e "select user_id from users where first like '%${first_name}%' and last like '%${last_name}%';")
Then use the variable CommandFinale in you connection chain.
I have not bee able to test it but it should be like that or very similar.

Need to display alternative text when query results return no match

My script cycles through multiple environments and if a user is found, it prints out their ID. The desired output is as follows:
environment1
<user_id>
However, my script is currently working to print out every environment even if there isn't a user id, like follows:
environment1
<user_id>
environment2
environment3
environment4
<user_id>
I would like the code to print out "NO USER FOUND" if the user_id does not exist, rather than excluding the environment altogether.
The code below is what is being utilized:
#log it all
{
echo "environment"
sshpass -p $ldappw ssh $ldapuser#12.345.67.89 'mysql --skip-column-names -hhost -u user -ppassword database -e \
"select user_id from users where first like '"'%${first_name}%' and last like '%${last_name}%';"'"'
} > $log
Any help would be much appreciated for this issue. Unfortunately I'm just beginning to learn more advanced MySQL and Linux command line tools and am not quite proficient enough to know how to handle this problem.
may be
select ifnull(user_id,'NO USER FOUND') from users where first like '"'%${first_name}%' and last like '%${last_name}%';

How to send e-mail to users within a database?

I want to send an e-mail to users within a database.
SQL query:
SELECT nickname, email FROM db_Users;
I want the e-mail to use both columns for example:
To: %email
Hi %nickname,
Message
Can someone show me basic example to do it using a shell script? I just want the basics to know how to get started and not a fully written script :-)
Thanks
Usually I use parameter for replace variables like that. Something like this :
--top of script
NAME=$(mysql -u $USER $DBNAME -p$PWD -sN -e "SELECT nickname FROM db_Users")
EMAIL=$(mysql -u $USER $DBNAME -p$PWD -sN -e "SELECT email FROM db_Users")
echo "${NAME} Message for you" | mail -s "This is the subject" ${EMAIL}
You could do a loop fot each user, including all members in list :
I=0
for i in ${NAME[#]}
do
-- E-mail
I=`expr $I + 1`
done

mysql insert into host table when matching username and uid found in user table

I have a command line script to insert users into a host table. But I want to only allow the add to host when the username and UID match that of the username and uid in the user table.
I have this.. Which works fine... [even without the INTO]
doquery2() {
mysql -e "$query2" -h $DBHOST --password=******* userdb
}
INSERT hosts SET hostname=\"$HostName\",
username=\"$UserName\",
password=\"$Password\",
UID=\"$zUID\",
GID=\"$zGID\",
Full_Name=\"${FullName:=empty}\",
Homedir=\"${HomeDir:=empty}\",
Default_shell=\"${DefaultShell:=empty}\""
But I want to add the lookup from the user table and only add to host when username and UID match.
Any ideas?