from email import MIMEMultipart
from email import MIMEText
from email import MIMEImage
from email.utils import COMMASPACE, formatdate
def sendmailFunc(htmlContent, userMailList, title):
try:
if (len(htmlContent) < 1):
print "htmlContent is None!"
userMailList = defaultEmailList
#必须引用MIMEMultipart.MIMEMultipart,否则'LazyImporter' object is not callable
msg = MIMEMultipart.MIMEMultipart('alternative')
msg['Subject'] = (title).decode('utf-8')
#From 必须与login、sendmail一致,否则qq邮箱会判断为垃圾邮件。
msg['From'] = 'test@163.com'
msg['To'] = COMMASPACE.join(userMailList)
msg['Date'] = formatdate(localtime=True)
msg.attach(MIMEText.MIMEText(htmlContent, 'html', 'utf-8'))
smtp = smtplib.SMTP()
smtp.connect("smtp.exmail.qq.com", "25")
# 此处为公司web邮箱安全授权码,而非 oa 登录密码。
smtp.login('test@163.com', '安全授权码')
if len(msg.as_string()) > 0:
smtp.sendmail('test@163.com', userMailList, msg.as_string())
smtp.close()
print getNowTime() + " ------------>> mail send success..."
except Exception, e:
print getNowTime() + " -------- " + str(e)
————————————————————————————————————————————
#coding:utf-8
from writeLog import *
import urllib2
import smtplib
import datetime
import re, sys
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.Utils import COMMASPACE, formatdate
reload(sys)
sys.setdefaultencoding('utf-8')
def sendMailHtml(title, date, logData, receviers):
msg = MIMEMultipart('alternative')
msg['Subject'] = (title + '【%s】' % date).decode('utf-8')
msg['From'] = 'noreply@ooxx.com'
msg['To'] = COMMASPACE.join(receviers)
msg['Date'] = formatdate(localtime=True)
msg.attach(MIMEText(logData, 'html', 'utf-8'))
smtp = smtplib.SMTP()
smtp.connect("smtp.exmail.qq.com", "25")
smtp.login('noreply@ooxx.com', 'passwd')
if (len(msg.as_string()) > 0):
smtp.sendmail('noreply@ooxx.com', receviers, msg.as_string())
smtp.close()
def sendMailAtt(title, date, logData, receviers):
#创建一个带附件的实例
msg = MIMEMultipart()
#构造附件
att = MIMEText(logData, 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename=' + infoLogName.split("/")[-1]
msg.attach(att)
#加邮件头
msg['Subject'] = (title + '【%s】' % date).decode('utf-8')
msg['From'] = 'noreply@ooxx.com'
msg['To'] = COMMASPACE.join(receviers)
msg['Date'] = formatdate(localtime=True)
#发送邮件
smtp = smtplib.SMTP()
smtp.connect("smtp.exmail.qq.com", "25")
smtp.login('noreply@ooxx.com', 'passwd')
if (len(msg.as_string()) > 0):
smtp.sendmail('noreply@ooxx.com', receviers, msg.as_string())
smtp.close()
if __name__ == '__main__':
dateTime = datetime.date.today() - datetime.timedelta(days=0)
strDate = str(dateTime.strftime('%Y-%m-%d'))
print strDate
try:
receivers = ['username@ooxx.com']
logData = os.popen("sort -t$'\t' -k2n " + infoLogName).read()
sendMailAtt('你的邮件名 - ', strDate, logData, receivers)
errorLogger.info("------------>> send success...")
except Exception, e:
errorLogger.info("--------" + str(e))
Refer:
[1] Python 发送邮件可以添加附件
http://www.oschina.net/code/snippet_221343_49994
[2] TypeError: 'LazyImporter' object is not callable
http://stackoverflow.com/questions/34348069/typeerror-lazyimporter-object-is-not-callable
[3] LazyImporter not callable
http://blog.gmane.org/gmane.comp.python.mime.devel/month=20061201