脚本宝典收集整理的这篇文章主要介绍了

python邮件发送方法备份

脚本宝典小编觉得挺不错的,现在分享给大家,也给大家做个参考,希望能帮助你少写一行代码,多一份安全和惬意。

def send_html_mail_file(self, params, is_debug=0):
        from email.mime.text import MIMEText
        from email.mime.multipart import MIMEMultipart
        import smtplib
        import random
        from email.mime.text import MIMEText

        if is_debug:
            mail_host = const.SYSTEM_EMAIL_TEST['SMTP_HOST']  #设置服务器
            mail_user = const.SYSTEM_EMAIL_TEST['SMTP_USER']   #用户名
            mail_pass = const.SYSTEM_EMAIL_TEST['SMTP_PASS']   #口令
            mail_from = const.SYSTEM_EMAIL_TEST['FROM_EMAIL']
            mail_name = const.SYSTEM_EMAIL_TEST['FROM_NAME']

        else:
            mail_host = const.SYSTEM_EMAIL['SMTP_HOST']  #设置服务器
            mail_user = const.SYSTEM_EMAIL['SMTP_USER']   #用户名
            mail_pass = const.SYSTEM_EMAIL['SMTP_PASS']   #口令
            mail_from = const.SYSTEM_EMAIL['FROM_EMAIL']
            mail_name = const.SYSTEM_EMAIL['FROM_NAME']

            num = random.randint(0,10)
            system_users = const.SYSTEM_EMAIL_USER
            mail_user = system_users[num]
            mail_from = mail_user

        to = params['to']
        cc = ''
        bcc = ""

        files = ''
        if 'files' in params:
            files = params['files']

        try:
            msg = MIMEMultipart()

            content = MIMEText(params['msg'],_subtype='html',_charset='utf-8') #plain
            msg.attach(content)


            if type(files) == list and len(files):
                for file in files:
                    path = str(file['path'])
                    if os.name is not 'posix': # 非 linux系统
                        ipos = path.find("/")
                        if ipos > -1:
                            path = path.replace("/", "\")
                            print [path]
                    att1 = MIMEText(open(path, 'rb').read(), 'base64', 'gb2312')
                    att1["Content-Type"] = 'application/octet-stream'
                    att1["Content-Disposition"] = 'attachment; filename="'+str(file['filename'])+'"'#这里的filename可以任意写,写什么名字,邮件中显示什么名字
                    msg.attach(att1)

            me = mail_name+"<"+mail_from+">"
            msg['Subject'] = params['title']
            msg['From'] = me
            msg['To'] = to
            msg['Cc'] = cc
            msg['Bcc'] = bcc
            to_list = str(to).split(";")

            server = smtplib.SMTP()
            server.connect(mail_host)
            server.login(mail_user,mail_pass)
            server.sendmail(me, [to_list,cc,bcc], msg.as_string())
            server.close()
            return {"error":False, "error_info":'', "mail_user":mail_user}
        except Exception, e:
            print str(e)
            return {"error":True, "error_info":str(e),"mail_user":mail_user}

总结

以上是脚本宝典为你收集整理的

python邮件发送方法备份

全部内容,希望文章能够帮你解决

python邮件发送方法备份

所遇到的程序开发问题,欢迎加入QQ群277859234一起讨论学习。如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典网站推荐给程序员好友。 本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。

80%的人都看过