#coding=utf-8
import smtplib, mimetypes
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import base64, time
import Log
USER = "273612158"
PASSWORD = "********"
SERVER = "smtp.
userName = base64.encodestring(USER).strip()
password = base64.encodestring(PASSWORD).strip()
smtp = smtplib.SMTP()
def init():
'''@summary: 初始化连接邮件服务器
'''
smtp.connect(SERVER)
smtp.docmd('helo', USER)
smtp.docmd('auth login')
smtp.docmd(userName)
type, msg = smtp.docmd(password)
if type == 250:
return 1
else:
return 0
def sendMail(subject, strFrom, toUsers, plainText):
'''@summary: txt发送内容 '''
smtp.set_debuglevel(1)
#调试状态
msg = MIMEMultipart()
msg['From'] = strFrom
if type(toUsers) is list:
msg['To'] = ','.join(toUsers)
msg['To'] =
toUsers
msg['Subject'] = subject.encode('gbk')
msgtxt = MIMEText(plainText, 'plain', 'utf-8')
msg.attach(msgtxt)
val = True
try:
for i in range(1):
val = smtp.sendmail(strFrom, toUsers, msg.as_string())
except Exception, e:
Log.error(str(e))
return 0
if not val :
return 1
def sendHtmlMail(subject, strFrom, toUsers, htmlText):
'''@summary: html发送内容 '''
smtp.set_debuglevel(1)
msg = MIMEMultipart()
msg['From'] = strFrom
if type(toUsers) is list:
msg['To'] = ','.join(toUsers)
msg['To'] =
toUsers
msg['Subject'] = subject.encode('gbk')
msgText = MIMEText(htmlText, 'html', 'utf-8')
msg.attach(msgText)
fileName = r'gmock-1.5.0.tar.gz'
ctype, encoding = mimetypes.guess_type(fileName)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
att1 = MIMEImage((lambda f: (f.read(), f.close()))(open(fileName, 'rb'))[0], _subtype = subtype)
att1.add_header('Content-Disposition', 'attachment', filename = fileName)
msg.attach(att1)
val = True
try:
val = smtp.sendmail(strFrom, toUsers, msg.as_string())
except Exception, e:
Log.error(str(e))
return 0
if not val :
return 1
init()
#初始化连接邮件服务器
if __name__ == '__main__':
#
for i in range(2):
#
sendMail('啊呀', 'wangxd@', ['273612158@'], '人工湖')