-
Notifications
You must be signed in to change notification settings - Fork 1
/
send_email.py
41 lines (33 loc) · 1.1 KB
/
send_email.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
# coding:utf-8
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
# 发送邮件函数
def send_email(from_addr, password, to_addr, txt_head, txt_msg):
# 163网易邮箱服务器地址
smtp_server = 'smtp.163.com'
msg = MIMEText(txt_msg, 'html', 'utf-8')
msg['Subject'] = Header(txt_head, 'utf-8').encode()
msg['From'] = _format_addr(from_addr)
msg['To'] = _format_addr(to_addr)
# 发送邮件
server = smtplib.SMTP(smtp_server, 25)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
print("邮件发送成功。")
server.quit()
if __name__ == '__main__':
# 发件人地址
from_addr = '[email protected]'
# 邮箱密码
password = 'xxx'
# 收件人地址
to_addr = '[email protected]'
# 设置邮件信息
txt_head = 'head'
txt_msg = 'msg'
send_email(from_addr, password, to_addr, txt_head, txt_msg)