Shell发送邮件+附件

    xiaoxiao2022-07-05  178

    Shell发送邮件+附件

    1. 生成QQ邮箱授权码2. 服务器配置2.1 关闭sendmail的服务2.2 开启postfix服务2.3 创建认证2.3 配置mail.rc 3. 测试3.1 直接发送普通文字邮件3.2 将文件内容作为邮件内容发送3.3 shell发送无附件的邮件3.4 Shell发送带附件的邮件3.5 Shell发送带附件的邮件(优化)

    shell发送邮件,这里以QQ邮箱为发送方进行操作的

    1. 生成QQ邮箱授权码

    2. 服务器配置

    2.1 关闭sendmail的服务

    service sendmail status

    如果有安装,就关闭sendmail的服务,并关闭开机自启动

    service sendmail stop chkconfig sendmail off

    2.2 开启postfix服务

    service postfix status service postfix start

    postfix start失败,执行检查命令postfix check 解决:安装mysql-libs

    rpm -qa|grep mysql yum install -y mysql-libs

    设置postfix 开机自启动

    chkconfig postfix on

    2.3 创建认证

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

    2.3 配置mail.rc

    vi /etc/mail.rc

    文件尾部添加下面内容:

    set from=131413688@qq.com set smtp=smtp.qq.com set smtp-auth-user=131413688 #授权码 set smtp-auth-password=xxxxx set smtp-auth=login set smtp-use-starttls set ssl-verify=ignore set nss-config-dir=/root/.certs

    说明:上面的from、smtp-auth-user换成自己的QQ号,xxxxx替换成第1步生成的授权码

    3. 测试

    3.1 直接发送普通文字邮件

    echo "hello word" | mail -s "title" 131413688@qq.com

    接收到的邮件:

    3.2 将文件内容作为邮件内容发送

    cd /root/data/shell vim mailContent.txt

    文件内容为:

    Hello Jack, Nice to meet you. I'm from shenzhen, China.

    执行发送邮件命令

    cd /root/data/shell cat mailContent.txt | mail -s "旅游向导" 131413688@qq.com

    接收到的邮件:

    3.3 shell发送无附件的邮件

    1)编写shell脚本

    vim /root/shell/mail/mail_noattachment.sh

    文件内容为:

    #!/bin/bash FROM_EMAIL="131413688@qq.com" TO_EMAIL="100861008@qq.com,100861999@qq.com" JOB_NAME="TEST" RUNNINGNUM=1 echo -e "`date "+%Y-%m-%d %H:%M:%S"` : The 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}

    说明:FROM_EMAIL、TO_EMAIL都换成自己的邮箱来测哈!

    2)执行shell脚本发送邮件

    sh /root/shell/mail/mail_noattachment.sh

    接收到的邮件:【我TO_EMAIL里指定的2两个邮箱都收到了邮件】

    3.4 Shell发送带附件的邮件

    1)准备2个附件: 第一个log日志文件

    vim /root/shell/mail/error1.log

    文件内容为【加中文可以测试附件是否会中文乱码】:

    错误日志1: This is the exception 1.1... This is the exception 1.2...

    第二个log日志文件

    vim /root/shell/mail/error2.log

    文件内容为:

    错误日志2: This is the exception 2.1... This is the exception 2.2...

    2)编写shell脚本

    vim /root/shell/mail/mail_attachment.sh

    文件内容为:

    #!/bin/bash FROM_EMAIL="131413688@qq.com" TO_EMAIL="100861008@qq.com,100861999@qq.com" LOG1=/root/shell/mail/error1.log LOG2=/root/shell/mail/error2.log echo -e "`date "+%Y-%m-%d %H:%M:%S"` : Please to check the fail sql attachement." | mailx \ -r "From: alertAdmin <${FROM_EMAIL}>" \ -a ${LOG1} \ -a ${LOG2} \ -s "Critical:DSHS fail sql." ${TO_EMAIL}

    3)执行shell脚本发送邮件

    sh /root/shell/mail/mail_attachment.sh

    接收到的邮件: TO_EMAIL指定的邮箱都收到了带附件的邮件,且附件里的中文并没有乱码。

    3.5 Shell发送带附件的邮件(优化)

    3.4 虽然实现了发送附件的功能,但邮件标题和发送哪些附件都是写死在shell脚本中的,灵活性不够,难道每次发个邮件还得修改shell脚本? 所以生产上是把邮件标题、发送哪些附件等信息,通过参数传入的。

    1)编写shell脚本

    vim /root/shell/mail/mail_attachment2.sh

    文件内容为:

    #!/bin/bash # $#表示参数个数 if [ $# -lt 2 ]; then echo "Error:Missing parameters" echo "Useage: sh mail_attachement.sh <mail-title> <attachment1> <attachment2> ..." exit 1 fi FROM_EMAIL="131413688@qq.com" TO_EMAIL="100861008@qq.com,100861999@qq.com" #从第2个参数开始,用-a拼出要发送的附件 first=1 attachment="" for i in "$@" do if [ $first -eq 1 ]; then first=0 else attachment="$attachment -a $i " fi done echo -e "`date "+%Y-%m-%d %H:%M:%S"` : Please to check the fail sql attachement." | mailx \ -r "From: alertAdmin <${FROM_EMAIL}>" \ $attachment \ -s "$1" ${TO_EMAIL}

    注意点: for i in “$@” 这里有个坑,$@ 带上双引号是将每个参数都看作一份数据,彼此之间是独立的,这样能避免一个带空格的参数被拆分成多个参数的问题。

    2)执行shell脚本发送邮件【指定邮件标题时故意带了个空格,哈哈】

    sh /root/shell/mail/mail_attachment2.sh "Hadoop HDFS启动失败" /root/shell/mail/error1.log /root/shell/mail/error2.log

    接收到的邮件:

    最新回复(0)