本文共 1139 字,大约阅读时间需要 3 分钟。
Linux发送邮件命令
mail如果没有mail命令
yum -y install postfix如果只是发送邮件不接收邮件的时候postfix不用开启
命令格式
-s <邮件主题>:指定邮件的主题 -c <地址>:添加邮件抄送人,多个人时用逗号隔开 -b <地址>:添加邮件暗送人 -a <附件>: 添加附件例1:
echo “正文内容”| mail -s “邮件标题” xxxxx@xx.com例2:
mail -s “邮件标题” xxxxx@xx.com < /etc/hosts 把hosts的内容当做正文内容发送例3 发送附件
mail -s “邮件标题” -a call.txt xxxxx@xx.com <call.txt例4 抄送
mail -s “状态” -a call.txt -c yyyyy@xx.com xxxxx@xx.com <call.txt 发送给xxxxx@xx.com 抄送给 yyyyy@xx.com使用外部邮箱发送邮件
现在很多邮箱服务都有对发送地址进行验证,因此使用外部邮箱可以减少被拦截的情况。 默认是使用本地邮件服务进行发送邮件,通过配置/etc/mail.rc可以使用外部的邮箱来发送邮件。 vim /etc/mail.rc set from=xxxxx@xx.com smtp=smtp.xx.com smtp-auth-user=xxx smtp-auth-password=xxx smtp-auth=loginfrom= 邮箱地址
smtp= smtp服务器地址 smtp-auth-user= 认证用户名 smtp-auth-password= 认证密码 smtp-auth= 认证方式写了一个定时任务 每周五检查服务器状态 发送邮件
50 17 * * 5 /bin/sh /root/detecting.sh "free -m" > /root/service_$(date +\%F).txt51 17 * * 5 /bin/sh /root/detecting.sh "df -TH" >> /root/service_$(date +\%F).txt52 17 * * 5 /bin/sh /root/detecting.sh "uptime" >> /root/service_$(date +\%F).txt53 17 * * 5 mail -s "service status messages" -a /root/service_$(date +\%F).txt -c yyyyy@xx.com xxxxx@xx.com< /root/call.txt
注:在计划任务中%需要转义
转载地址:http://rdrpb.baihongyu.com/