python发送邮件示例(支持中文邮件标题)

yipeiwu_com5年前Python基础

复制代码 代码如下:

def sendmail(login={},mail={}):
    '''\
    @param login login['user'] login['passwd']
    @param mail mail['to_addr'] mail['subject'] mail['content'] mail['attach']
    '''
    from datetime import datetime
    from base64 import b64encode
    import smtplib, mimetypes
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    from email.mime.image import MIMEImage

    user_info = login['user'].split('@')
    mail_configure = {}
    mail_configure['mail_encoding'] = 'utf-8'
    mail_configure['mail_supplier'] = user_info[1]
    mail_configure['from_addr'] = login['user']
    mail_configure['server_host'] = 'smtp.%s' % mail_configure['mail_supplier']
    error = None

    try:
        email = MIMEMultipart()
        email['from'] = mail_configure['from_addr']
        email['to'] = mail['to_addr']
        email['subject'] = '=?%s?B?%s?=' % (mail_configure['mail_encoding'],b64encode(mail['subject']))
        email_content = MIMEText(mail['content'], _charset=mail_configure['mail_encoding'])
        email.attach(email_content)

        if 'attach' in mail:
            for i in mail['attach']:
                ctype, encoding = mimetypes.guess_type(i)
                if ctype is None or not encoding is None:
                    ctype = 'application/octet-stream'
                maintype, subtype = ctype.split('/', 1)
                att = MIMEImage((lambda f: (f.read(), f.close()))(open(i, 'rb'))[0], _subtype = subtype)
                att.add_header('Content-Disposition', 'attachment', filename = i)
                email.attach(att)

        smtp = smtplib.SMTP()
        smtp.connect(mail_configure['server_host'])
        smtp.login(user_info[0], login['passwd'])
        smtp.sendmail(mail_configure['from_addr'], mail['to_addr'], email.as_string())
        smtp.quit()
    except Exception as e:
        error = e

    return (mail_configure['from_addr'], mail['to_addr'], error)

测试

复制代码 代码如下:

def t21():
    login = {
        'user':'ak43@sina.com',
        'passwd':'hello@d'
    }
    mail = {
        'to_addr':'ak32@sina.com;ak32@21cn.com',
        'subject':'不带附件的测试邮件',
        'content':'''\
        sz002718,友邦吊顶
        sz002719,麦趣尔
        sz002722,金轮股份
        ''',
    }
    print sendmail(login, mail)

    login = {
        'user':'hellot@sina.com',
        'passwd':'hello#world'
    }
    mail = {
        'to_addr':'tom12@sina.com;tom12@21cn.com',
        'subject':'带附件的测试邮件',
        'content':'''\
        sz002718,友邦吊顶
        sz002719,麦趣尔
        sz002722,金轮股份
        ''',
        'attach':['e:/a/a.txt']
    }
    print sendmail(login, mail)

相关文章

python判断文件夹内是否存在指定后缀文件的实例

该代码主要是基于python实现判断指定文件夹下是否存在指定后缀的文件。代码如下: import os Your_Dir='你的文件夹/' Files=os.listdir(Yo...

python 3调用百度OCR API实现剪贴板文字识别

本程序调用百度OCR API对剪贴板的图片文字识别,配合CaptureScreen软件,可快速识别文字。 #!python3 import urllib.request, urlli...

python导入模块交叉引用的方法

实际项目中遇到python模块相互引用问题,查资料,终于算是弄明白了。 首先交叉引用或是相互引用,实际上就是导入循环,关于导入循环的详细说明,可见我摘自《python核心编程》第二版的摘...

python 随机森林算法及其优化详解

前言 优化随机森林算法,正确率提高1%~5%(已经有90%+的正确率,再调高会导致过拟合) 论文当然是参考的,毕竟出现早的算法都被人研究烂了,什么优化基本都做过。而人类最高明之处就是懂...

python实现人脸识别代码

python实现人脸识别代码

从实时视频流中识别出人脸区域,从原理上看,其依然属于机器学习的领域之一,本质上与谷歌利用深度学习识别出猫没有什么区别。程序通过大量的人脸图片数据进行训练,利用数学算法建立建立可靠的人脸特...