管理服务器时经常需要写一些监测脚本 然后在出问题的时候通过邮件来通知 接下来为大家介绍用最简单的方式来实现 Linux 发送邮件:


Centos、RedHat 系统 mail 命令

1、关闭本机的sendmail服务或者postfix服务

#执行下面的命令 各位大侠都对号入座吧#sendmialservice sendmail stopchkconfig sendmail off#postfixservice postfix stopchkconfig postfix off#再狠一点就直接卸载吧..yum remove sendmailyum remove postfix

2、然后修改/etc/mail.rc 在文件末尾增加以下内容 指定外部的smtp服务器地址、帐号密码等

$ vi /etc/mail.rc set from=123456@qq.comset smtp=smtp.qq.com set smtp-auth-user=123456set smtp-auth-password=facesohoset smtp-auth=login 

这里使用了QQ邮箱的SMTP 需要做以下配置:

qq邮箱通过生成授权码来设置密码:

3、然后通过命令来发送邮件

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

Ubuntu 系统 heirloom-mailx 命令

1、安装heirloom-mailx

sudo apt-get install heirloom-mailx

2、配置

vi /etc/nail.rc 添加 QQ 邮箱开放的需要认证的smtp服务器:

set from=429240967@qq.comset smtp=smtp.qq.comset smtp-auth-user=429240967@qq.comset smtp-auth-password=facesohoset smtp-auth=login

3、在命令行发邮件给QQ邮箱用户

echo "邮件内容" | heirloom-mailx -s "邮件标题" 123@qq.com