由於種種原因,需要由我這個兼職運維每天發送對賬單文件給運營同學,研究下 Linux 發送郵件,希望對大家有所幫助。
安裝
<code> $ yum install -y mailx $ mail --h/<code>
SSL 證書
配置 SSL 證書,否則會提示 “Error in certificate: Peer’s certificate issuer is not recognized.”。
<code>
$
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
$
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.
/<code>生成完成之後,需要在 /etc/mail.rc 配置文件中,修改 nss-config-dir 為上面命令生成的目錄 ~/.certs。
配置文件
以下以阿里雲郵箱配置為例,其他郵箱類似。
<code> $ vi /etc/mail.rc
set
from
=daodaotest@xxx.comset
smtp=smtps://set
smtp-auth-user
=daodaotest@xxx.comset
smtp-auth-password
=xxxxxset
smtp-auth=loginset
nss-config-dir=~/.certs//<code>使用
<code> $ echo
"郵件內容"
| mail -s
"郵件標題"
daodaotest@163.com echo"郵件內容,請查收"
| mail -v -c"daodaotest@163.com,daodaotest@qq.com"
-s
"郵件標題"
-a daodaotest.zip daodaotest@163.com/<code>參數說明:
更多參數說明使用 man mailx 命令查看。
問題
Unexpected EOF on SMTP connection
我個人遇到的問題是設置 smtp 參數時,沒有添加 smtps 協議。
<code>
set
smtp=smtp.mxhichina.com:465
set
smtp=smtps:set
smtp=smtps:/<code>Error in certificate: Peer’s certificate issuer has been marked as not trusted by the.
沒有配置 SSL 證書。
使用場景
定時給運營同學發送對賬單文件
<code> $ cat sendRecFile.sh
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
usageexit
1fi
RE_USERS=$1
CC_USERS=$2
$3
if
["X
$DAY
" =="X"
];then
DAY=$(date -d yesterday +%Y%m%d)fi
RE_PATH="/data/ftp/PE/RUI/response"
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
1fi
fi
"/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
1fi
fi
if
[ -d"
${RE_PATH}
/${DAY}
" ];then
cp -r"
${RE_PATH}
/${DAY}
"cd
/tmp ||exit
${ZIPEXE}
-r"
${DAY}
.zip""
${DAY}
"else
printf
"Cannot find %s."
"
${RE_PATH}
/${DAY}
"exit
1fi
printf
"您好: \n\n 附件為 %s 對賬單文件,請查收。\n"
"
${DAY}
" |${MAILEXE}
-c"
${CC_USERS}
" -s"[定時自動發送]
${DAY}
對賬單文件 ""/tmp/
${DAY}
.zip""
${RE_USERS}
"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 daodaotest@163.com daodaotest@163.com >>/root/ops-scripts/sendRecFile.log/<code>