Example 1. 定期清理 mail quere
# vi /usr/local/sbin/my-queue-cleanup.sh
#!/bin/sh mailq |grep MAILER-DAEMON | awk '{printf $1} {print " deferred"}' | tr -d '*!' | xargs -n 2 postsuper -d # chmod 500 /usr/local/sbin/my-queue-cleanup.sh # crontab -e 0 5 * * * /usr/local/sbin/my-queue-cleanup.sh |
另一种方式是比对 spamassassin 或 hits
# vi /usr/local/sbin/my-queue-cleanup.sh
#!/bin/sh TMPFILE=/tmp/clean.queue.$$ DEFERDIR=/var/spool/postfix/deferred # collect the filenames mailq |grep MAILER-DAEMON | cut -f1 -d ' ' > $TMPFILE for DEFERFILE in `cat $TMPFILE` do FILEPATH=`find $DEFERDIR -name $DEFERFILE` egrep -i 'spamassassin|hits\=[0-9]{1,2}\.[0-9]' $FILEPATH > /dev/null if [ $? -eq 0 ] then # deferred message is most likely spam postsuper -d $DEFERFILE deferred fi done rm -f $TMPFILE > /dev/null |
Example 2. 创建添加用户脚本
# vi /sbin/addmailuser
#!/bin/sh uid=450 gid=450 status=1 filter=DUNNO quota=104857600 mysql_name=mail mysql_password=password echo 'Enter user name:' read name if [ "$name" = "" ]; then echo 'Must input name!' exit fi echo 'Enter user password:[123]' read password if [ "$password" = "" ]; then password=123 fi echo 'Enter user forward:['$name']' read forward if [ "$forward" = "" ]; then forward=$name fi echo 'Enter user domain:[mydomain.com]' read domain if [ "$domain" = "" ]; then domain=mydomain.com fi echo 'Enter user home:[/mail/domains/'$domain'/'$name']' read home if [ "$home" = "" ]; then home=/mail/domains/$domain/$name fi echo 'Enter user Maildir:['$home'/Maildir]' read maildir if [ "$maildir" = "" ]; then maildir=$home/Maildir fi echo 'name= '$name echo 'password= '$password echo 'forward= '$forward echo 'domain= '$domain echo 'home= '$home echo 'maildir= '$maildir echo 'status= '$status echo 'filter= '$filter echo 'quota= '$quota echo "If under value is right,please input 'y' any Enter:" read mychoice if [ "$mychoice" = "y" ]; then echo "use mail;" > tmp_addmailuser.sql fi echo "INSERT INTO USER (USERNAME,PASSWORD,CLEAR_PASSWORD,FORWARD,DOMAIN,HOMEDIR,MAILDIR,MAIL) VALUES ('$name','','$password',' $forward','$domain','$home','$maildir','$name@$domain');" >> tmp_addmailuser.sql /usr/local/mysql/bin/mysql -u$mysql_name -p$mysql_password < tmp_addmailuser.sql rm tmp_addmailuser.sql MailUserDir=$home mkdir -p $MailUserDir /usr/local/courier/bin/maildirmake $MailUserDir/Maildir;chmod -R 700 $MailUserDir;chown -R maildrop:maildrop $MailUserDir exit fi # chmod 755 /sbin/addmailuser |
Example 3. 创建删除用户脚本
# vi /sbin/delmailuser
#!/bin/sh uid=450 gid=450 status=1 filter=DUNNO quota=104857600 mysql_name=mail mysql_password=password echo 'Enter user name:' read name if [ "$name" = "" ]; then echo 'Must input name!' exit fi echo 'Enter user domain:[mydomain.com]' read domain if [ "$domain" = "" ]; then domain=mydomain.com fi echo "use mail;" > tmp_delmailuser.sql echo "select USERNAME,DOMAIN,HOMEDIR,MAILDIR,MAIL from USER where (USERNAME='"$name"' and MAIL='"$name@$domain"' and DOMAIN='" $domain"');" >> tmp_delmailuser.sql /usr/local/mysql/bin/mysql -u$mysql_name -p$mysql_password < tmp_delmailuser.sql | grep $name@$domain > tmp_delmailuser user=`awk '{ print $1 }' tmp_delmailuser` mail=`awk '{ print $5 }' tmp_delmailuser` home=`awk '{ print $3 }' tmp_delmailuser` maildir=`awk '{ print $4 }' tmp_delmailuser` if [ "$home" = "" ]; then echo "No $name in $domain , please check and input again." rm -rf tmp_delmailuser.sql rm -rf tmp_delmailuser exit fi echo 'name= '$name echo 'domain= '$domain echo 'mail= '$name@$domain echo 'home= '$home echo 'maildir= '$maildir echo "if under value is right,please input 'y' and Enter:" read mychoice if [ "$mychoice" = "y" ]; then echo "use mail;" > tmp_delmailuser.sql echo "delete from USER where (MAIL='"$name@$domain"' and USERNAME='"$name"' and DOMAIN='"$domain"');" >> tmp_delmailuser.sql /usr/local/mysql/bin/mysql -u$mysql_name -p$mysql_password < tmp_delmailuser.sql rm -rf tmp_delmailuser.sql rm -rf tmp_delmailuser MailUserDir=$home rm -rf $MailUserDir exit fi # chmod 755 /sbin/delmailuser |
邮件群发
# groupmail Subject all ./file
Example 4. groupmail
#!/bin/bash SUBJECT=$1 MYSQL_LOCATE=/usr/local/mysql/bin MYSQL_USER=mailer MYSQL_PASS=mailer MYSQL_DB=mailer CONTENT_FILE=$3 case "$2" in all) SQL="select email from user;" ;; *) SQL="select email from user where flag='$2';" ;; esac /bin/mail -s ${SUBJECT} `echo ${SQL} | ${MYSQL_LOCATE}/mysql -u${MYSQL_USER} -p${MYSQL_PASS} ${MYSQL_DB} | sed -e '1d'` < ${CONTENT_FILE} exit 0 |
生成地址本
# mysql2csv_address
[root@linuxas3 tmp]# ./mysql2csv_address Enter password: [root@linuxas3 tmp]# cat address.csv |
Example 5. mysql2csv_address
#!/bin/bash # author: netkiller(at)9812.net echo "姓名,电子邮件地址" | awk -F "," '{ print $1 "," $2 "\r"}' >address.csv echo "select name,user from postfix_users where dept='Sales';" | \ mysql -u root -p postfix | \ awk -F " " '{ print $1 "," $2 "\r"}' | \ sed -e '1d' >>address.csv |
作者使用putty管理服务器,所以上面脚本中输入的汉是在UTF-8编码,需要改成gb18030
Foxmail 不识别UNIX like下产生的文件需要在每行最后加"\r",即回车 Outlook不需要
iconv -f utf-8 -t gb18030 mysql2csv_address -o /usr/bin/mysql2csv_address