由於種種原因,需要由我這個兼職運維每天發送對賬單文件給運營同學,故研究下 Linux 發送郵件,希望對大家有所幫助。
安裝
# Centos,安裝 mailx
$ yum install -y mailx
# 查看幫助
$ mail --h
SSL 證書
配置 SSL 證書,否則會提示 “Error in certificate: Peer’s certificate issuer is not recognized.”。
# 生成證書
$ mkdir ~/.certs
$ echo -n | openssl s_client -connect smtp.mxhichina.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/ali.crt
$ certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/ali.crt
$ certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/ali.crt
# 查看證書權限
$ cd ~/.certs && ll
總用量 80
-rw-r--r-- 1 root root 2277 4月 19 10:47 ali.crt
-rw------- 1 root root 65536 4月 19 10:56 cert8.db
-rw------- 1 root root 16384 4月 19 10:56 key3.db
-rw------- 1 root root 16384 4月 19 10:48 secmod.db
# 驗證,顯示如下信息表示 SSL 證書配置生成及安裝完成
$ certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i ali.crt
Notice: Trust flag u is set automatically if the private key is present.
生成完成之後,需要在 /etc/mail.rc
配置文件中,修改 nss-config-dir
爲上面命令生成的目錄 ~/.certs
。
配置文件
以下以阿里雲郵箱配置爲例,其他郵箱類似。
# 配置郵箱參數,文件末尾添加以下內容
$ vi /etc/mail.rc
# ssl 校驗設置,配置 SSL 證書就可以註釋掉
# set ssl-verify=ignore
# 郵箱賬戶,對方收到郵件時顯示的發件人
set from=[email protected]
# smtp 服務器地址,不要忘記添加:smtps
set smtp=smtps://smtp.mxhichina.com:465
# 郵箱賬戶
set smtp-auth-user=[email protected]
# 郵箱密碼,部分郵箱(163)爲授權密碼而非郵箱密碼
set smtp-auth-password=xxxxx
# smtp 認證方式。默認是 login
set smtp-auth=login
# 設置 nss 配置目錄,上一步驟 SSL 證書目錄
set nss-config-dir=~/.certs/
使用
# 發送郵件
$ echo "郵件內容" | mail -s "郵件標題" [email protected]
# 發送郵件,添加抄送人及附件
echo "郵件內容,請查收" | mail -v -c "[email protected],[email protected]" -s "郵件標題" -a daodaotest.zip [email protected]
參數說明:
-s <郵件主題>
:指定郵件的主題;-c <地址>
:指定抄送人,多個收件人之間用逗號分隔;-b <地址>
:指定密送人,多個收件人之間用逗號分隔;-a
:參數後面跟的文件,將作爲附件發送出去;-v
:執行時,顯示詳細的信息。
更多參數說明使用 man mailx
命令查看。
問題
Unexpected EOF on SMTP connection
我個人遇到的問題是設置 smtp
參數時,沒有添加 smtps
協議。
# /etc/mail.rc 之前配置
set smtp=smtp.mxhichina.com:465
# /etc/mail.rc 修改後的配置
set smtp=smtps://smtp.mxhichina.com:465
Error in certificate: Peer’s certificate issuer has been marked as not trusted by the.
沒有配置 SSL
證書,具體配置見[生成 SSL 證書](#生成 SSL 證書)。
使用場景
定時給運營同學發送對賬單文件
# 腳本內容
$ cat sendRecFile.sh
#!/bin/bash
# 定時給運營同學發送對賬單文件
# 使用方法
usage() {
printf "Usage: sh %s RE_USERS CC_USERS [DAY]" "$0"
printf "\n"
printf "\n\t RE_USERS 收件人,多個收件人之間用逗號分隔"
printf "\n\t CC_USERS 抄送人,多個收件人之間用逗號分隔"
printf "\n\t DAY 發送對賬文件日期,默認爲:T-1"
}
# 判斷參數
if [ $# -lt 2 ]; then
usage
exit 1
fi
# 收件人
RE_USERS=$1
# 抄送人
CC_USERS=$2
# 對賬文件日期
DAY=$3
if [ "X$DAY" == "X" ]; then
DAY=$(date -d yesterday +%Y%m%d)
fi
# 對賬文件路徑
RE_PATH="/data/ftp/PE/RUI/response"
# 確定 ‘zip’ 命令位置
ZIPEXE="/usr/bin/zip"
if [ ! -x "${ZIPEXE}" ]; then
ZIPEXE="/usr/bin/zip"
if [ ! -x "${ZIPEXE}" ]; then
printf "Unable to locate 'zip'."
printf "Please report this message along with the location of the command on your system."
exit 1
fi
fi
# 確定 ‘mail’ 命令位置
MAILEXE="/usr/bin/mail"
if [ ! -x "${MAILEXE}" ]; then
MAILEXE="/usr/bin/mail"
if [ ! -x "${MAILEXE}" ]; then
printf "Unable to locate 'mail'."
printf "Please report this message along with the location of the command on your system."
exit 1
fi
fi
# 打包對賬單
if [ -d "${RE_PATH}/${DAY}" ]; then
cp -r "${RE_PATH}/${DAY}" /tmp
cd /tmp || exit
${ZIPEXE} -r "${DAY}.zip" "${DAY}"
else
printf "Cannot find %s." "${RE_PATH}/${DAY}"
exit 1
fi
# 發送郵件
printf "您好: \n\n 附件爲 %s 對賬單文件,請查收。\n" "${DAY}" | ${MAILEXE} -c "${CC_USERS}" -s "[定時自動發送] ${DAY} 對賬單文件 " -a "/tmp/${DAY}.zip" "${RE_USERS}"
# 發送完成,刪除 zip
if [ -f "/tmp/${DAY}.zip" ]; then
rm -rf "/tmp/${DAY}.zip"
fi
exit 0
# 使用方法
$ sh sendRecFile.sh
Usage: sh ./sendRecFile.sh RE_USERS CC_USERS [DAY]
RE_USERS 收件人,多個收件人之間用逗號分隔
CC_USERS 抄送人,多個收件人之間用逗號分隔
DAY 發送對賬文件日期,默認爲:T-1
# 定時任務設置
$ crontab -e
0 9 * * * /root/ops-scripts/sendRecFile.sh [email protected] [email protected] >>/root/ops-scripts/sendRecFile.log
微信公衆號:daodaotest