-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.py
44 lines (39 loc) · 1.33 KB
/
mail.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
#!/usr/bin/env python
#-- coding: utf-8 --
import sys
import os
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding)
#############################################################################
# mail service
#
###
# sender = send_mail_address
# receivers = connect_email
# mail_template_file='report.txt'
# mail_type=success/error
##############################################################################
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def pysendmail(sender,receivers,question,stat):
mail_host= 'smtp.mxhichina.com' #设置服务器
mail_user='[email protected]' #用户名
mail_pass='123qwe!@#QWE' #口令
mail_template_file='./template/report.txt'
mail_content=open(mail_template_file).read()
body= mail_content%(question,stat)
message = MIMEText(body, 'plain', 'utf-8')
message['From'] = Header(sender, 'utf-8')
message['To'] = Header(receivers, 'utf-8')
subject = "系统报警邮件----- "+question+stat
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP(mail_host,25)
smtpObj.login(mail_user,mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
return "Success! to send "+receivers
except smtplib.SMTPException:
return "Mail Error!!"