前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python2 群发 html 或文本邮件

python2 群发 html 或文本邮件

作者头像
用户1177713
发布2018-02-24 10:29:38
8130
发布2018-02-24 10:29:38
举报
文章被收录于专栏:数据之美
代码语言:javascript
复制
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)

————————————————————————————————————————————

代码语言:javascript
复制
#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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档