犹豫和反复浪费了大量时间。

与朋友言

在完成一个邮件发送程序之前我根本不明白什么是邮件,哪怕已经读过廖雪峰大神的文章,没有贬低大神的意思,大神的博客已经非常的详细, 是我的眼大肚皮小毛病在作祟,由一个邮件程序入门python3的确是很不错的,如果可能,我希望朋友们从廖大神的python2程序自行琢磨出python3版本,这对理解字符编码很有帮助。

利器在手,天下我有,python编程推荐pycharm,有了pycharm有飞起来的冲动,fly fly fly

有码才快乐

1 #begin

 2 import smtplib, email
 3 #from and import key word make it possible that use a variable stand for a specified module name
 4 from email import encoders
 5 from email.header import Header
 6 from email.mime.text import MIMEText
 7 from email.mime.multipart import MIMEMultipart
 8 from email.mime.base import MIMEBase
 9 #from key word copy a module variable name to a specified domain
 from email.utils import parseaddr, formataddr
 
 #def a function to format email address
 def _format_addr(s):
     (name, addr) = parseaddr(s)
     return formataddr(( \
         Header(name, 'utf-8').encode(), \
         addr))
 
 #get address and other by input
 #from_addr = input('From: ')
 #from_name = input('MyName: ')
 #password = input('Password: ')
 #to_addr = input('To: ')
 #to_name = input('FriendName: ')
 #smtp_server = input('SMTP server: ')
 #heading = input('Heading: ')
 #main_body = input('Main body: ')
 from_addr = 'xxxx@xxxx.com'
 from_name = 'guy'
 password = 'xxxxx'
 #hehe, this is my email
 to_addr = 'zdyx0379@163.com'
 to_name = 'zhaodan'
 smtp_server = 'smtp.qq.com'
 heading = 'For my friend'
 #text email body
 main_body_text = 'i miss you, old friend'
 #html email body, say hello and show a picture
 main_body_html = '<html>'+\
             '<body>'+\
             '<h1>hello, friend</h1>'+\
             '<p><img src="cid:0"></p>'+\
             '</body>'+\
             '</html>'
 from_attr = from_name + ' < ' + from_addr + ' > '
 to_attr = to_name + ' < ' + to_addr + ' > '
 
 #email.mime.multipart can include accessory
 #email.mime.text only support text, we can add email.mime.text to email.mime.multipart by attach method
 msg = MIMEMultipart('alternative')
 #msg.attach(MIMEText(main_body, 'plain', 'utf-8'))
 msg.attach(MIMEText(main_body_html, 'html', 'utf-8'))
 #email to be send is a list, here
 msg['From'] = _format_addr(from_attr)
 msg['To'] = _format_addr(to_attr)
 msg['Subject'] = Header(heading, 'utf-8').encode()
 with open('E:\\1.jpg', 'rb') as f:
            mime = MIMEBase('image', 'jpg', filename = '1.jpg')
            mime.add_header('Context-Disposition', 'attachment', filename = '1.jpg')
            #set cid of html, then can show img in mail body by html refrence to cid:0
            mime.add_header('Content-ID', '<0>')
            mime.add_header('X-Attachment-ID', '')
            #read file and attach file context to mime
            mime.set_payload(f.read())
            #encode by base64 code
            encoders.encode_base64(mime)
            #attach mime to msg as accessory
            msg.attach(mime)
 
 #add a text email, just in case reciver can't parse html email
 msg.attach(MIMEText(main_body_text, 'plain', 'utf-8'))
 server = smtplib.SMTP(smtp_server, 25)
 server.starttls()
 server.set_debuglevel(1)
 server.connect(smtp_server, 25)
 server.helo()
 server.ehlo()
 server.login(from_addr, password)
 server.sendmail(from_addr, [to_addr], msg.as_string())
 server.quit()
 #end

邮件分为文本邮件和html邮件两种:

文本邮件正文仅支持文字,支持插入附件;

html邮件支持在html中插入文字、图片、链接等,同样支持插入附件,现在经常看到的就是html邮件。

发送文本邮件及设置收件人、发件人名称等见我的上一篇博客,这次只介绍一下html邮件的一些特殊之处。

html邮件 

邮件正文应当是html格式的<html><body><h1>helloworld</h1></body></html>.

邮件正文显示的图片可以通过引用的方式从附件获取,当正文显示该图片以后,附件中不再显示;具体操作步骤:附件中添加header的时候指定一个标签ID(mime.add_header('Content-ID', '<0>')), html正文引用该标签(<p><img src="cid:0"></p>)。

python支持发送加密的SMTP邮件,只需要在连接邮件服务器之前建立安全连接(server.starttls())

最终发送的邮件如上图,广告一下我的Git: https://github.com/find2014/email-smtp  ,目前学习python3,中短期目标是建立一个django架构的个人博客,有共同兴趣的朋友请私信我的邮箱。

【Python3】SMTP发送邮件的更多相关文章

  1. Python3 SMTP发送邮件

    SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. python的smtplib提供了一 ...

  2. 吴裕雄--天生自然python学习笔记:Python3 SMTP发送邮件

    SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. python的smtplib提供了一 ...

  3. Python3实现发送邮件和发送短信验证码

    Python3实现发送邮件和发送短信验证码 Python3实现发送邮件: import smtplib from email.mime.text import MIMEText from email. ...

  4. php用smtp发送邮件

    php用smtp发送邮件 1.其实用smtp协议发送邮件很简单,用框架或者原生都可以,我们需要用到class.phpmailer.php 和class.smtp.php,大家可以去网上下载. 这是一个 ...

  5. phpmailer,smtp发送邮件实例(转)

    一,用phpmailer发送邮件 查看复制打印? <?php   include "class.phpmailer.php";    //包函邮件发送类      //邮件发 ...

  6. python通过SMTP发送邮件失败,报错505/535

    python通过SMTP发送邮件失败:错误1:smtplib.SMTPAuthenticationError: (550, b'User has no permission')    我们使用pyth ...

  7. linux 下 用phpmailer类smtp发送邮件始终不成功,提示:ERROR: Failed to co

    https://zhidao.baidu.com/question/509191264.html?fr=iks&word=PHPMailerSMTP+connect()+failed& ...

  8. python大法好——Python SMTP发送邮件

    Python SMTP发送邮件 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. py ...

  9. 运维监控-Zabbix Server 使用QQ SMTP发送邮件报警及定制报警内容

    运维监控-Zabbix Server 使用QQ SMTP发送邮件报警及定制报警内容 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客采用腾讯邮箱,想必大家都对QQ很了解,所以 ...

  10. gitlab配置通过smtp发送邮件(QQ exmail腾讯企业为例)

    gitlab配置通过smtp发送邮件(QQ exmail腾讯企业为例) 首先祭出官网文档链接:https://docs.gitlab.com/omnibus/settings/smtp.html 其实 ...

随机推荐

  1. UVa 536 Tree Recovery

    题意:给出一颗二叉树的前序遍历和中序遍历,输出其后序遍历 用杭电1710的代码改一点,就可以了. #include<iostream> #include<cstdio> #in ...

  2. OpenGL 顶点缓存对象

    顶点缓存对象(Vertex Buffer Object,简称 VBO),允许开发者根据情况把顶点数据放到显存中. 如果不用 VBO,用 glVertexPointer / glNormalPointe ...

  3. 【JS】<select>标签小结

    循环时通过<c:if>来判断是否为默认选中 <select name="select" id="month"> <c:forEac ...

  4. MyBatis学习 之 三、动态SQL语句

    目录(?)[-] 三动态SQL语句 selectKey 标签 if标签 if where 的条件判断 if set 的更新语句 if trim代替whereset标签 trim代替set choose ...

  5. Canvas处理头像上传

    未分类 最近社区系统需要支持移动端,其中涉及到用户头像上传,头像有大中小三种尺寸,在PC端,社区用Flash来处理头像编辑和生成,但该Flash控件的界面不友好而且移动端对Flash的支持不好,考虑到 ...

  6. oracle 11g在安装过程中出现监听程序未启动或数据库服务未注册到该监听程序

    15511477451 原文 oracle 11g在安装过程中出现监听程序未启动或数据库服务未注册到该监听程序? 环境:win7 64位系统.oracle11g数据库 问题描述:在win7 64位系统 ...

  7. Hadoop集群中Hbase的介绍、安装、使用

    导读 HBase – Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群. 一.Hbase ...

  8. iBatis 和MyBatis区别

    从  iBatis  到  MyBatis ,你准备好了吗? 对于从事 Java EE 的开发人员来说,iBatis 是一个再熟悉不过的持久层框架了,在 Hibernate.JPA 这样的一站式对象 ...

  9. Spring 事务管理原理探究

    此处先粘贴出Spring事务需要的配置内容: 1.Spring事务管理器的配置文件: 2.一个普通的JPA框架(此处是mybatis)的配置文件: <bean id="sqlSessi ...

  10. iBeacon开发

    什么是iBeacons iBeacons是苹果在2013年WWDC上推出一项基于蓝牙4.0(Bluetooth LE | BLE | Bluetooth Smart)的精准微定位技术,当你的手持设备靠 ...