【Python3】SMTP发送邮件
犹豫和反复浪费了大量时间。
与朋友言
在完成一个邮件发送程序之前我根本不明白什么是邮件,哪怕已经读过廖雪峰大神的文章,没有贬低大神的意思,大神的博客已经非常的详细, 是我的眼大肚皮小毛病在作祟,由一个邮件程序入门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发送邮件的更多相关文章
- Python3 SMTP发送邮件
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. python的smtplib提供了一 ...
- 吴裕雄--天生自然python学习笔记:Python3 SMTP发送邮件
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. python的smtplib提供了一 ...
- Python3实现发送邮件和发送短信验证码
Python3实现发送邮件和发送短信验证码 Python3实现发送邮件: import smtplib from email.mime.text import MIMEText from email. ...
- php用smtp发送邮件
php用smtp发送邮件 1.其实用smtp协议发送邮件很简单,用框架或者原生都可以,我们需要用到class.phpmailer.php 和class.smtp.php,大家可以去网上下载. 这是一个 ...
- phpmailer,smtp发送邮件实例(转)
一,用phpmailer发送邮件 查看复制打印? <?php include "class.phpmailer.php"; //包函邮件发送类 //邮件发 ...
- python通过SMTP发送邮件失败,报错505/535
python通过SMTP发送邮件失败:错误1:smtplib.SMTPAuthenticationError: (550, b'User has no permission') 我们使用pyth ...
- linux 下 用phpmailer类smtp发送邮件始终不成功,提示:ERROR: Failed to co
https://zhidao.baidu.com/question/509191264.html?fr=iks&word=PHPMailerSMTP+connect()+failed& ...
- python大法好——Python SMTP发送邮件
Python SMTP发送邮件 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. py ...
- 运维监控-Zabbix Server 使用QQ SMTP发送邮件报警及定制报警内容
运维监控-Zabbix Server 使用QQ SMTP发送邮件报警及定制报警内容 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客采用腾讯邮箱,想必大家都对QQ很了解,所以 ...
- gitlab配置通过smtp发送邮件(QQ exmail腾讯企业为例)
gitlab配置通过smtp发送邮件(QQ exmail腾讯企业为例) 首先祭出官网文档链接:https://docs.gitlab.com/omnibus/settings/smtp.html 其实 ...
随机推荐
- UVa 11889 (GCD) Benefit
好吧,被大白书上的入门题给卡了.=_=|| 已知LCM(A, B) = C,已知A和C,求最小的B 一开始我想当然地以为B = C / A,后来发现这时候的B不一定满足gcd(A, B) = 1 A要 ...
- 【转】JAVA之网络编程
转自:火之光 网络编程 网络编程对于很多的初学者来说,都是很向往的一种编程技能,但是很多的初学者却因为很长一段时间无法进入网络编程的大门而放弃了对于该部分技术的学习. 在 学习网络编程以前,很多初学者 ...
- apache开源项目 -- tez
为了更高效地运行存在依赖关系的作业(比如Pig和Hive产生的MapReduce作业),减少磁盘和网络IO,Hortonworks开发了DAG计 算框架Tez.Tez是从MapReduce计算框架演化 ...
- C# 一次查询多表,填充DataSet并指定表名
lhrhi 原文 NET 一次查询多表,填充DataSet并指定表名(DataSet指定DataTable名称的技巧) 现实中的场景,有时可能需要一次查询数据库中表张.在使用SqlDataAdapte ...
- java jvm学习笔记十一(访问控制器)
欢迎装载请说明出处: http://blog.csdn.net/yfqnihao/article/details/8271665 这一节,我们要学习的是访问控制器,在阅读本节之前,如果没有前面几节的 ...
- 求职基础复习之快速排序c++版
#include<iostream> using namespace std; int partition(int a[],int p,int q){ int x = a[q]; ; fo ...
- G-sensor驱动分析
重力传感器代码分析 重力传感器驱动的功能,主要是向HAL层提供IOCTRL接口,并通过input设备上报数据.芯片实际数据的读取是采用i2c协议读取原始数据,并且作为i2c设备挂载在系统上工作的. 1 ...
- ACM1996
/* 汉诺塔VI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- [Hive - LanguageManual] Create/Drop/Alter Database Create/Drop/Truncate Table
Hive Data Definition Language Hive Data Definition Language Overview Create/Drop/Alter Database Crea ...
- Apache Rewrite常用设置说明
例子: RewriteEngine on 打开引擎 RewriteRule test.html /test.php [L] RewriteRule test.html?$ /tianqi.php?s1 ...