-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathemail_utils.py
More file actions
58 lines (46 loc) · 1.68 KB
/
email_utils.py
File metadata and controls
58 lines (46 loc) · 1.68 KB
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
56
57
58
# -*- coding: utf-8 -*-
"""
Send email util
"""
import os.path
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE
class EmailWrapper(object):
def __init__(self, smtp_host, smtp_port, username, passwd,
from_addr, tls=False):
self.smtp_host = smtp_host
self.smtp_port = smtp_port
self.username = username
self.passwd = passwd
self.from_addr = from_addr
self.tls = tls
@property
def server(self):
server = smtplib.SMTP(self.smtp_host, self.smtp_port)
if self.tls:
server.ehlo()
server.starttls()
server.ehlo()
server.login(self.username, self.passwd)
return server
def sendmail(self, to_addrs, subject, text, text_type='html', files=[]):
if isinstance(to_addrs, basestring):
to_addrs = [to_addrs]
msg = MIMEMultipart()
msg['From'] = self.from_addr
msg['To'] = COMMASPACE.join(to_addrs)
msg['Subject'] = subject
msg.attach(MIMEText(text, text_type))
for f in files:
att = MIMEText(open(f, 'rb').read(), 'base64', 'utf-8')
att.add_header('Content-Type', 'application/octet-stream')
att.add_header('Content-Disposition', 'attachment',
filename=os.path.basename(f))
msg.attach(att)
self.server.sendmail(self.from_addr, to_addrs, msg.as_string())
if __name__ == '__main__':
wrapper = EmailWrapper('smtp.qq.com', 25, 'qq号', '密码',
'qq号@qq.com')
wrapper.sendmail('to@qq.com', 'subject', 'content')