forked from freeyoung/gen_pdf_zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenmail.py
55 lines (44 loc) · 1.69 KB
/
genmail.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 -*-
import email
import mimetypes
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import smtplib
def sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText, attName):
strFrom = fromAdd
strTo = toAdd
server = authInfo.get('server')
user = authInfo.get('user')
passwd = authInfo.get('password')
if not (server and user and passwd) :
print 'incomplete login info, exit now'
return
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = subject
msgRoot['From'] = strFrom
msgRoot['To'] = ', '.join(strTo)
msgRoot.preamble = 'This is a multi-part message in MIME format.'
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText(plainText, 'plain', 'utf-8')
msgAlternative.attach(msgText)
msgText = MIMEText(htmlText, 'html', 'utf-8')
msgAlternative.attach(msgText)
fp = open(attName, 'rb')
msgImage = MIMEBase('application',"octet-stream")
msgImage.set_payload(fp.read())
fp.close()
msgImage.add_header('Content-Disposition', 'attachment; filename='+attName)
email.Encoders.encode_base64(msgImage)
msgRoot.attach(msgImage)
smtp = smtplib.SMTP()
smtp.connect(server)
smtp.ehlo()
#smtp.starttls()
smtp.login(user, passwd)
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()
return