• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python发送邮件示例(支持中文邮件标题)

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python示例程序,python简单代码示例,python代码示例,python序列乘法示例,python示例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

    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)
</div>

测试

    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)
</div>

</div>

您可能想查找下面的文章:

  • Python编程生成随机用户名及密码的方法示例
  • Python判断变量是否为Json格式的字符串示例
  • python实现逻辑回归的方法示例
  • python分割列表(list)的方法示例
  • Python编程生成随机用户名及密码的方法示例
  • Python判断变量是否为Json格式的字符串示例
  • python实现逻辑回归的方法示例
  • Python实现对字符串的加密解密方法示例
  • Python列表切片用法示例
  • python利用拉链法实现字典方法示例

相关文章

  • 在Python中使用cookielib和urllib2配合PyQuery抓取网页信息
  • 理解Python垃圾回收机制
  • python正则表达式中的括号匹配问题
  • Python中.py文件打包成exe可执行文件详解
  • Python中使用中文的方法
  • Python实现简单的四则运算计算器
  • python实现文件名批量替换和内容替换
  • Python的collections模块中的OrderedDict有序字典
  • 详细解析Python当中的数据类型和变量
  • Python中几个比较常见的名词解释

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python获取糗百图片代码实例
    • python使用mailbox打印电子邮件的方法
    • python实现的重启关机程序实例
    • Python函数式编程指南(一):函数式编程概述
    • python smtplib模块发送SSL/TLS安全邮件实例
    • 详解Python命令行解析工具Argparse
    • Python中类的定义、继承及使用对象实例详解
    • 介绍Python的@property装饰器的用法
    • python使用socket远程连接错误处理方法
    • Python实现批量检测HTTP服务的状态

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有