使用shell脚本发送邮件带附件

时间:2024-03-29 10:40:16

配置

1、授权码:
在邮箱设置里面开启授权码授权。
使用shell脚本发送邮件带附件
2、启动postfix
1.1 启动postfix
#sendmial
service sendmail stop
chkconfig sendmail off

#postfix
service postfix start
chkconfig postfix on

如果postfix start失败
[[email protected] ~]# postfix check
postfix: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or directory
[[email protected] ~]# rpm -qa|grep mysql
[[email protected] ~]# yum install mysql-libs

创建认证

mkdir -p /root/.certs/
echo -n | openssl s_client -connect smtp.163.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/163.crt
certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/163.crt
certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/163.crt
certutil -L -d /root/.certs
cd /root/.certs
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu"  -d ./ -i 163.crt

配置mail.rc

vi /etc/mail.rc

set [email protected]
set smtp=smtp.163.com
set smtp-auth-user=13816290503
#授权码
set smtp-auth-password=*******
set smtp-auth=login
set smtp-use-starttls
set ssl-verify=ignore
set nss-config-dir=/root/.certs

测试:

echo  "hello word" | mail -s "title"  [email protected]

使用shell脚本发送邮件带附件


使用shell脚本发邮件不加附件

[[email protected] shell]# cat mail_noattachment.sh 
#!/bin/bash 

JOB_NAME="TEST"
FROM_EMAIL="[email protected]"
TO_EMAIL="[email protected]"

RUNNINGNUM=1

echo -e "`date "+%Y-%m-%d %H:%M:%S"` : The current running $JOB_NAME job num is $RUNNINGNUM in 192.168.137.201 ......" | mail \
-r "From: alertAdmin <${FROM_EMAIL}>" \
-s "Warn: Skip the new $JOB_NAME spark job." ${TO_EMAIL}
[[email protected] shell]# 

使用shell脚本发送邮件带附件


使用shell脚本发邮件加附件

[[email protected] shell]# cat data.log 
zhangsan
lisi
wangu
[[email protected] shell]# cat mail_attachment.sh 
#!/bin/bash 


FROM_EMAIL="[email protected]"
TO_EMAIL="[email protected]"

LOG=/root/shell/data.log


echo -e "`date "+%Y-%m-%d %H:%M:%S"` : Please to check the fail sql attachement." | mailx \
-r "From: alertAdmin <${FROM_EMAIL}>" \
-a ${LOG} \
-s "Critical:DSHS fail sql." ${TO_EMAIL}

使用shell脚本发送邮件带附件