使用Linux的mail命令发邮件

您所在的位置:网站首页 腾讯云添加465端口 使用Linux的mail命令发邮件

使用Linux的mail命令发邮件

2023-12-15 08:26| 来源: 网络整理| 查看: 265

日常运维经常需要系统告警、平台巡检情况定时发送到邮箱,在Linux中设置好配置文件,一个命令就可以搞定,分三步设置(注意邮件服务的端口是否开放,详见第五步):

1、开启邮箱POP3/SMTP/IMAP服务,设置邮箱授权码

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

$ vi /etc/mail.rc set [email protected] set smtp=smtp.yeah.net set smtp-auth-user=123456 set smtp-auth-password=admin set smtp-auth=login #from是你设置stmp服务的邮箱地址,也就是用来发送邮件的邮箱地址 #smtp是发生的外部smtp服务器的地址,看你用的什么邮箱了,在设置里面可以看到 #smtp-auth-user是外部smtp服务器认证的用户名 #smtp-auth-password是外部smtp服务器认证的用户密码(授权码) #smtp-auth是邮件认证的方式

3、用mail命令发送邮件

echo 'hello world' | mail -s 'hi' [email protected]

4、拓展内容--mail命令详解

#四种常用格式发信:#第一种方法,你可以把当前shell当成编辑器来用,编辑完内容后Ctrl-D结束 mail -s test [email protected] #第二种方法,我用的最多,可能是喜欢管道的缘故吧 echo “mail content”|mail -s test [email protected] #第三种方法,以file的内容为邮件内.容发信 mail -s test [email protected]< file #第四种方法,给多个用户发送邮件 mail -s test -c [email protected] [email protected]< file

5、mail服务需要开的端口

注意:阿里云等云服务商默认是封了的25端口的,(发邮件的SMTP的协议默认是25端口),需要把端口号改成465端口,然后给它一个ssl证书。

--参考文章:阿里云服务器发送邮件 - 野猪乔治 - 博客园 (cnblogs.com)

第一步:安装mailx邮箱服务

yum -y install mailx

第二步:在mailx配置文件里添加用户名,密码。mailx的配置文件是/etc/mail.rc这个文件。

set ssl-verify=ignore ### 启用证书 set nss-config-dir=/root/.certs ### 证书存放的位置 set from="[email protected]" ### 邮箱 set smtp="smtps://smtp.yeah.net:465" ### 邮箱服务器地址 set [email protected] ### 邮箱 set smtp-auth-password=admin ### 邮箱授权码(不是邮箱登录密码,不知道的话自己上去开通) set smpt-auth=login ### 启用自动连接登录,就是发送邮件的时候自动连接到163服务器帮你发送邮件

第三步:既然要用到证书,那我们还得在163的服务器上申请一个证书,证书名字自己定义,我这里命令为yeah.crt。

mkdir -p /root/.certs ### 证书存放位置 cd /root/.certs ###下面这几条命令每一条我也不是太明白,我只知道生成了证书和证书文件,和启用的证书!echo -n |openssl s_client -connect smtp.yeah.net:465 |sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' >yeah.crt certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/yeah.crt certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/yeah.crt certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ~/.certs/./ -i yeah.crt certutil -L -d /root/.certs

然后就可以发送邮件了。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3