from email import MIMEMultipartfrom email import MIMETextfrom email import MIMEImagefrom email.utils import COMMASPACE, formatdatedef 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-8from writeLog import *import urllib2import smtplibimport datetimeimport re, sysfrom email.MIMEMultipart import MIMEMultipartfrom email.MIMEText import MIMETextfrom email.MIMEImage import MIMEImagefrom email.Utils import COMMASPACE, formatdatereload(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 发送邮件可以添加附件
[2] TypeError: 'LazyImporter' object is not callable
[3] LazyImporter not callable