|
一、环境说明
我们通常需要发送邮件,用于报警,或邮件验证等需求,本次的环境要求如下:
CentOS 6.x 最小化安装,安装postfix(一般系统安装好自带的邮件系统),如果没有请如下操作:
#yum install postfix -y
python 2.6+
二、postfix简介
postfix是linux平台邮件系统,默认安装,并且自动开机运行,无需过多的配置,但有一点需要说明,postfix所
在主机绑定了备案域名则默认不会当作垃圾邮件,而所在主机没有绑定备案域名,大多邮件厂商会认为是垃圾邮件.
三、pytho发邮件脚本
#cat send_mail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
| #!/usr/bin/python
#coding:utf-8
import smtplib
#用来定义邮件
from email.mime.text import MIMEText
#邮件发送的内容
msg= """
linux基础
链接:http://pan.baidu.com/s/1mhBDhnM
"""
#接收人以字符串的形式列出
to_str = """
1549214808@qq.com,
"""
#接收人列表
to_list = to_str.replace("\n","").split(",")
#发送人
from_user = "wgdbl@localhost"
#邮件的标题
title = "san 的 Linux福利"
mail = MIMEText(msg,"plain","utf-8")
#发送的内容 #内容的类型 #内容的编码
mail["Subject"] = title
mail["From"] = from_user
mail["To"] = to_str
#登录smtp服务器
#postfix smtp 服务器地址: localhost (也可以是绑定了的域名)
# smtp 服务器地址端口: 25 (默认)
smtp_server = "localhost"
smtp_port = 25
server = smtplib.SMTP(smtp_server,smtp_port)
server.sendmail(from_user,to_list,mail.as_string())
#发送人
#接收人列表
#发送的内容
server.quit()
|
#python send_mail.py
打开QQ邮箱如图:
如上所述次实验postfix所在主机没有备案域名解析绑定,所以在QQ邮箱中是在垃圾箱中,而我的公司邮件没有收到!网易过滤掉了!
另外以上是针对本地的postfix 默认smtp非SSL发送的,还可通过 对QQ如下的 加密码认证发送邮件
有兴趣的可以试下
1
2
3
4
5
| smtp_server = "smtp.qq.com"
smtp_port = 465
server = smtplib.SMTP_SSL(smtp_server,smtp_port)
server.login(from_user,"tqnmomfayqpodjdh")
server.sendmail(from_user,to_list,mail.as_string())
|
四、带添加附件的脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
| #!/usr/bin/python
#coding:utf-8
import smtplib #负责登录smtp服务器的
from email.mime.text import MIMEText #用来定义邮件的
from email import MIMEMultipart #定义发送邮件的根容器
from email import MIMEBase #定义附件
from email import Encoders #对附件进行编码
msg= """
linux基础
链接:http://pan.baidu.com/s/1mhBDhnM
"""
#接收人字符串
to_str = """
,
1549214808@qq.com,
"""
###以上邮箱为了隐私写的是假的
#接收人列表
to_list = to_str.replace("\n","").split(",")
from_user = "root@localhost"#发送人
title = "san 的 Linux福利" #邮件的标题
#实例化一个邮件根容器
message = MIMEMultipart.MIMEMultipart()
#定义文本项
mail = MIMEText(msg,"plain","utf-8")
#发送的内容 #内容的类型 #内容的编码
message.attach(mail)
#附件的类型的变量
mintype,subtype = "application","octet-stream"
#定义附件的类型
file_message = MIMEBase.MIMEBase(mintype,subtype)
#附件添加内容
with open("by.txt","rb") as f:
file_message.set_payload(f.read())
#对附件进行编码
Encoders.encode_base64(file_message)
#附件的头部定义
file_message.add_header(
"Content-Disposition",
"attachment",
filename = "by.txt"
)
message.attach(file_message)
message["Subject"] = title
message["From"] = from_user
message["To"] = to_str
#登录smtp服务器 #qq smtp 服务器地:localhost #smtp 服务器地址端口: 25
smtp_server = "localhost"
smtp_port = 25
#server = smtplib.SMTP_SSL(smtp_server,smtp_port)
server = smtplib.SMTP(smtp_server,smtp_port)
#server.login(from_user,"") #如果是smtp ssl 加密的需要用户名密码登录
server.sendmail(from_user,to_list,message.as_string())
#发送人 #接收人列表 #发送的内容
server.quit()
|
效果如下图:
|
|
|