-
Notifications
You must be signed in to change notification settings - Fork 0
/
emailUtils.py
55 lines (48 loc) · 2.13 KB
/
emailUtils.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
Created on 2018年12月4日
@author: chuanqin
'''
#=========================================================================
# below original code comes from the link:https://blog.csdn.net/gpf951101/article/details/78909233
# great thanks to this great man. I modify the code to suit my own project.
# also reference for attachment of email
# https://github.com/rootzhongfengshan/python_practical/blob/master/SentMail/SentMailWithAttachment.py
# also write one blog of own->https://blog.csdn.net/u011233383/article/details/84794295
#=========================================================================
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import logging
import smtplib
import traceback
from com.qcq.config.config import settings
from com.qcq.const.message import email_default_signature
def send_mail(recv, title, content, attachments, mail_host = 'smtp.163.com', port = 465):
username = settings.get(u'email section', u'email user')
passwd = settings.get(u'email section', u'email password')
content = '<h1>' + content + '</h1><p>' + email_default_signature + '.</p>'
msg = MIMEText(content, 'html')
message = MIMEMultipart()
message['Subject'] = title
message['From'] = username
message['To'] = recv
message.attach(msg)
for fileName in attachments:
attachment = MIMEText(open(fileName, 'rb').read(), 'base64', 'utf-8')
attachment["Content-Type"] = 'application/octet-stream'
attachment["Content-Disposition"] = 'attachment; filename="' + \
fileName + '"'
message.attach(attachment)
try:
logging.info('Begin Connect...')
smtp = smtplib.SMTP_SSL(mail_host, port = port)
logging.info('Begin Login... %s' % (username))
smtp.login(username, passwd)
logging.info('Begin Send... to %s' % (':'.join(recv)))
smtp.sendmail(username, recv, message.as_string())
smtp.quit()
logging.info('email send success.')
except Exception, exc:
logging.warn('Exception happened:%s' %
traceback.print_exc(), exc_info = True)