-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
41 lines (34 loc) · 1.56 KB
/
main.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
from docx import Document
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import time
import smtplib
class SendEmail(object):
def __init__(self):
self.to_addrs = ["[email protected]"] # receivers_address
self.sys_date = time.strftime('%Y-%m-%d', time.localtime())
self.from_addr = '[email protected]' # sender_address
self.password = 'xxxxxx' # sender_password
self.smtp_server = 'smtp.mxhichina.com' # sender_smtpserver
def _format_addr(self, s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
def send_email(self, title, path):
document = Document(path)
content = ''
for paragraph in document.paragraphs:
content = content + '\n' + paragraph.text
msg = MIMEText(content, 'plain', 'utf-8')
msg['From'] = self._format_addr(u'MSC_CQU<%s>' % self.from_addr) # sender_name
msg['Subject'] = Header(title, 'utf-8').encode()
server = smtplib.SMTP(self.smtp_server, 25)
server.login(self.from_addr, self.password)
for receiver in self.to_addrs:
msg['To'] = self._format_addr(u'MSC_Member<%s>' % receiver) # receiver_name
server.sendmail(self.from_addr, [receiver], msg.as_string())
time.sleep(1)
server.quit()
sender = SendEmail()
sender.send_email(u'测试邮件', r"D:\EmailSender\test.docx") # word file position
print('Done.')