系统环境:Centos7.4
[root@centos ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
首先提醒一下安装系统最小化安装固然最快最节省资源,但是缺点也很多,比如装完很多命令没有(例如vim),很多包没有,甚至按TAB键都不能自动不全的都有。所以在机器配置足够的情况下尽量安装DVD-iso,而不是minimal版本。好了废话不多说,进入主题如何收发邮件:
收邮件直接敲mail好了
[root@centos ~]# mail
-bash: /usr/bin/mail: No such file or directory
最小化安装系统的问题来了,没有这个命令。糟糕,那如何解决呢?肯定是缺少了一个安装包造成的,那么鬼知道是要装哪个包才会有这个命令啊。问题不大,不要慌,用下面这个命令查询一下
[root@centos ~]# yum whatprovides "*/bin/mail"
---------------------------------------------------------------------------------------
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile
mailx-12.5-19.el7.x86_64 : Enhanced implementation of the mailx command
Repo : os
Matched from:
Filename : /bin/mail
-----------------------------------------------------------------------------------------
(虚线我自己加的,隔开有效命令方便查看)第四行显示有一个mailx的包提供了这条命令,那就装这个包就好了。
[root@centos ~]# yum install mailx -y
此处省略了安装信息显示。。。
[root@centos ~]# mail
No mail for root
没有root用户的邮件
那么发邮件呢?
目前有三种:一,用mail命令发送,第二种sh脚本,第三种py脚本。
注意: 1. 网易邮箱你得登录电脑版网易邮箱在设置里面添加一个POP3授权密码,这个密码和你的邮箱密码是不一样的,授权成功后才能在非网易平台登录使用。
2. 腾讯云服务器你得登录控制台,点自己的用户名那里,选择25端口解封才行。还有你得云服务器安全组规则也得保证25端口是允许状态,本地虚拟机请忽略。
第一种:mail命令
[root@centos ~]# yum install mailx -y
[root@centos ~]# vi /etc/mail.rc
编辑配置文件,加上这五条,以163网易邮箱为例,把xxxx替换成自己的邮箱相关信息。
--------------------------------------------------------------------------------------------
set from=xxxxx@163.com #显示发自哪里
set smtp=smtp.163.com:25 #25是邮件服务端口
set smtp-auth-user=xxxxx@163.com #认证用户
set smtp-auth-password=xxxxx #认证密码
set smtp-auth=login #登录(实测不加这一条也可以)
--------------------------------------------------------------------------------------------
接着防火墙开放端口
[root@centos ~] firewall-cmd --permanent --add-port=25/tcp
[root@centos ~] firewall-cmd --reload
做完这些,基本可以保证发送成功了。
[root@centos ~]# mail -s test2018 alex@xxx.com
123
EOT
进入编辑模式,输入123,回车,按CTRL+D结束。
或者
[root@centos ~] echo "tengxunyun test " | mail -s "test123" alex@xxx.com
拿起手机,发现收到邮件了。
第二种:sh脚本
这种脚本只是在安装了mailx的情况下的才能操作,也就是像第一种的升级版。同样需要1.安装mailx包,2.配置/etc/mail.rc以,3.开放防火墙端口。
[root@centos~]# yum install mailx -y
[root@centos~]# vi sendmail.sh
#!/bin/sh
#export.UTF-8
echo "$3" | sed s/'\r'//g | mailx -s "$2" $1
[root@centos~]# chmod +x sendmail.sh
第三种:py脚本
[root@centos ~]# vi mail.py
-----------------------------------------------------------------------------------------
#!/bin/env python
#coding:utf-8
'''
发送txt文本邮件
'''
import smtplib
from email.mime.text import MIMEText
from sys import argv
mailto_list=[]
mail_host="smtp.163.com:25" #设置服务器
mail_user="xxx@163.com" #发件用户名(换成自己的)
mail_pass="xxx" #口令(换成自己的)
#mail_postfix="163.com" #发件箱的后缀
debug_level=0 #是否开启debug
def send_mail(to_list,sub,content):
me=mail_user
msg = MIMEText(content,_subtype='plain',_charset='utf-8')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
server = smtplib.SMTP()
server.set_debuglevel(debug_level)
server.connect(mail_host)
server.login(mail_user,mail_pass)
server.sendmail(me, to_list, msg.as_string())
server.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == '__main__':
try:
mailto_list=argv[1].split(';')
sub=argv[2]
content=argv[3]
except:
print "python send_mail.py 'user1@xx.com;user2@xx.com' sub content"
exit()
if send_mail(mailto_list,sub,content):
print "发送成功"
else:
print "发送失败"
--------------------------------------------------------------------------------------------
以上是脚本内容
赋予执行权限
[root@centos~]# chmod +x mail.py
接着防火墙开放端口
[root@centos ~] firewall-cmd --permanent --add-port=25/tcp
[root@centos ~] firewall-cmd --reload
[root@centos ~]# ./mail.py alex@xxxx.com 123 234
发送成功
手机收到了邮件了
发表评论