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

与朋友言

在完成一个邮件发送程序之前我根本不明白什么是邮件,哪怕已经读过廖雪峰大神的文章,没有贬低大神的意思,大神的博客已经非常的详细, 是我的眼大肚皮小毛病在作祟,由一个邮件程序入门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. Fatal NI connect error 12170

    Fatal NI connect error 12170 转载:http://www.xifenfei.com/1812.html 今天在一台服务器的日志文件中,发现如下信息: Fatal NI co ...

  2. PhoneGap 安装体验

    npm -v #显示版本,检查npm 是否正确安装. npm install express #安装express模块 npm install -g express #加上 -g 启用global安装 ...

  3. php服务器安装memcache

    https://pecl.php.net/get/memcache-3.0.8.tgz wget https://pecl.php.net/get/memcache-3.0.8.tgzgzip -d ...

  4. HDU 5311 Hidden String (暴力)

    题意:今天是BestCoder一周年纪念日. 比赛管理员Soda有一个长度为n的字符串s. 他想要知道能否找到s的三个互不相交的子串s[l1..r1], s[l2..r2], s[l3..r3]满足下 ...

  5. 【RMQ问题】求数组区间最大值,NYOJ-1185-最大最小值

    转自:http://blog.csdn.net/lilongherolilong/article/details/6624390 先挖好坑,明天该去郑轻找虐 RMQ(Range Minimum/Max ...

  6. 【转】linux驱动开发的经典书籍

    原文网址:http://www.cnblogs.com/xmphoenix/archive/2012/03/27/2420044.html Linux驱动学习的最大困惑在于书籍的缺乏,市面上最常见的书 ...

  7. 【转】linux下a.out >outfile 2>&1重定向问题

    原文网址:http://blog.chinaunix.net/uid-25909722-id-2912890.html 转自:http://blog.chinaunix.net/space.php?u ...

  8. Log4NET简介

    log4net库是Apache log4j框架在Microsoft .NET平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台.文件.数据库等)的工具. 前提 最近做项目需要记录系统日志和用 ...

  9. 只用css实现“每列四行,加载完一列后数据自动填充到下一列”的效果

    只用css实现“每列四行,加载完一列后数据自动填充到下一列”的效果.这个题目用图表示如下: 如果将题目换成“只用css实现每行四列,加载完一行后数据自动填充到下一行”,那这个问题就简单多了,相信大家都 ...

  10. [Everyday Mathematics]20150122

    设 $f:[0,1]\to [0,1]$. (1). 若 $f$ 连续, 试证: $\exists\ \xi\in [0,1],\st f(\xi)=\xi$. (2). 若 $f$ 单调递增, 试证 ...