10996492526_03d3c81d48_c.jpg

回顾之前的文章《CentOS 6 安装配置 msmtp&mutt 发送监控邮件》 喵斯基简单介绍了 mutt+msmtp 解决方案的配置方法,用于系统监控邮件的发送。同样是介绍如何在Linux系统上配置MailX邮件进行邮件通知,从需求和易用性综合来考虑,如果我们只是需要通过邮件通知一个简单文字内容,那么本篇文章可能更适合。

脚本完整代码

注意:本脚本仅支持Ubuntu系统

#!/bin/bash
 
TheUsage(){
echo -e "\nUsage:\n      bash $0 [email] [password] [smtp] [port]\n"
exit 1
}
 
[ "$#" -ne '4' ] && TheUsage
 
eMailxUser="${1:-}"
eMailxPwd="${2:-}"
eMailxSMTP="${3:-}"
eMailxPort="${4:-}"
 
echo -e "\nUsername: ${eMailxUser}  Password: ${eMailxPwd}\nSMTP:${eMailxSMTP}:${eMailxPort}\n"
 
apt-get -y -qq -f install heirloom-mailx
[ $? -ne '0' ] && echo "Error! It can not install heirloom-mailx." && exit 0
 
# Mailx Config
rm -rf /etc/nail.rc
cat >>/etc/nail.rc<<EOF
# This is the configuration file for Heirloom mailx (/etc/nail.rc)
 
#Defaults
 
set hold
set append
set ask
set crt
set dot
set keep
set emptybox
set indentprefix="> "
set quote
set sendcharsets=iso-8859-1,utf-8
set showname
set showto
set newmail=nopoll
set autocollapse
set markanswered
ignore mime-version content-transfer-encoding
fwdretain subject date from to
 
#User
 
set from="${eMailxUser}" smtp="${eMailxSMTP}" port="${eMailxPort}"
set smtp-auth-user="${eMailxUser}" smtp-auth-password="${eMailxPwd}"
set smtp-auth=login smtp-use-starttls ssl-verify=ignore
 
EOF
 
# Mailx Testing
echo -e "Mailx Testing...\n" && echo "Mailx Testing" |mailx -v -s "Mailx Testing" "${eMailxUser}"

复制上面的代码,保存为 Mailx.sh

配置邮箱

执行下面的命令配置邮箱信息;

bash Mailx.sh [email] [password] [smtp server] [port]

示例1(yandex邮箱):

bash Mailx.sh 'mail@yandex.com' 'password' 'smtp.yandex.com' '465'

示例2(gmail邮箱)

bash Mailx.sh 'user@gmail.com' 'password' 'smtp.gmail.com' '587'

注意: Gmail需开启二次验证,及设置专用应用密码。

发送邮件

echo "邮件内容" |mailx -s "邮件主题" "收件人"

相关推荐

文章作者:喵斯基部落
原文地址:https://www.moewah.com/archives/329.html
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。非商业转载及引用请注明出处(作者、原文链接),商业转载请联系作者获得授权。