i am very new to linux/Debian and i asked on debianforum.de for help, how to send a internal e-mail to every registered mailbox in e. g. VimbAdmin.
So i was bored yesterday and i decided trying to make my first own bash script, and here it is:
Code: Select all
#!/bin/bash
#Script to send a message to every mailbox registered in a MySQL database
#Script by Michael Skalda
#skawa.de
#There are two ways of running the script:
#Way 1: without parameter: Script will ask you for subject and message content
#Way 2: with parameter: ./rundmail.sh subject messagecontent
#check if parameters are empty
if [ -z "$1" ] && [ -z "$2" ]
then
#if both are empty, subject and messagecontent will be requested per read -p
echo 'Send a mail to every user in your Mail-Database!'
read -p "Subject: " subject
echo 'HTML is activated! example: wordwrap <br>, paragraph with <p>'
read -p "Message: " message
#if there is at least one parameter given, it will check if $2 is empty
elif [ -z "$2" ]
then
#if $2 is empty, user will be prompt to write the message, again with read -p
subject="$1"
echo 'Subject: ' $subject
echo 'HTML is activated! example: wordwrap <br>, paragraph with <p>'
read -p "Message: " message
else
#when both parameters are given, the mail will be instantly sent
subject="$1"
message="$2"
echo 'Subject: ' $subject
echo 'Message: ' $message
echo 'Sending E-Mail...'
fi
#MySQL-User with access to your mailbox database
sql_user="vimbadmin"
#MySQL-User's password
sql_psw="changeme"
#your mailbox database (e.g. vimbadmin)
sql_database="vimbadmin"
#set the sender name/mailbox
postmaster="postmaster@skawa.de"
#read the sql entrys of the column "mailbox" and saves it to the array "emails". The column name is skipped, cause otherwise it will try to send an mail to "mailbox"
while read line
do
emails+=("$line")
done < <(mysql --skip-column-names -u${sql_user} -p${sql_psw} ${sql_database} -e "SELECT username FROM mailbox")
#send the e-mail with the just gathered data
echo "$message" | mailx -a 'Content-Type: text/html' -a "From: $postmaster" -s "$subject" ${emails[@]}
echo 'Success! Mail was sent to the following mailboxes:'
for i in ${emails[@]}; do
echo $i
done
Thank you )