• 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实现网页链接提取的方法分享
  • Python+微信接口实现运维报警
  • Python中的Function定义方法第1/2页
  • 用Python实现协同过滤的教程
  • Python Queue模块详细介绍及实例
  • SQLite3中文编码 Python的实现
  • python subprocess 杀掉全部派生的子进程方法
  • Python中用于转换字母为小写的lower()方法使用简介
  • Python列表计数及插入实例
  • Python中处理时间的几种方法小结

文章分类

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

最近更新的内容

    • 使用FastCGI部署Python的Django应用的教程
    • Python实现脚本锁功能(同时只能执行一个脚本)
    • 详解Django框架中的视图级缓存
    • python利用elaphe制作二维条形码实现代码
    • Django与遗留的数据库整合的方法指南
    • python原始套接字编程示例分享
    • 更改Python命令行交互提示符的方法
    • Python中异常重试的解决方案详解
    • python实现同时给多个变量赋值的方法
    • 下载给定网页上图片的方法

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

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