详解python 发送邮件实例代码


Posted in Python onDecember 22, 2016

python 发送邮件实例

文件形式的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimetext import MIMEText 
from emailheader import Header 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要 
msg['Subject'] = Header(subject, 'utf-8') 
 
smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgas_string()) 
smtpquit()

HTML形式的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimetext import MIMEText 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 
 
msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8') 
 
msg['Subject'] = subject 
 
smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgas_string()) 
smtpquit()

带图片的HTML邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimemultipart import MIMEMultipart 
from emailmimetext import MIMEText 
from emailmimeimage import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 
 
msgRoot = MIMEMultipart('related') 
msgRoot['Subject'] = 'test message' 
 
msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image<br><img src="cid:image1"><br>good!','html','utf-8') 
msgRootattach(msgText) 
 
fp = open('h:\\python\\jpg', 'rb') 
msgImage = MIMEImage(fpread()) 
fpclose() 
 
msgImageadd_header('Content-ID', '<image1>') 
msgRootattach(msgImage) 
 
smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgRootas_string()) 
smtpquit()

带附件的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimemultipart import MIMEMultipart 
from emailmimetext import MIMEText 
from emailmimeimage import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 
 
msgRoot = MIMEMultipart('related') 
msgRoot['Subject'] = 'test message' 
 
#构造附件 
att = MIMEText(open('h:\\python\\jpg', 'rb')read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att["Content-Disposition"] = 'attachment; filename="jpg"' 
msgRootattach(att) 
     
smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgRootas_string()) 
smtpquit()

群邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimetext import MIMEText 
 
sender = '***' 
receiver = ['***','****',……] 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8') 
 
msg['Subject'] = subject 
 
smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgas_string()) 
smtpquit()

各种元素都包含的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimemultipart import MIMEMultipart 
from emailmimetext import MIMEText 
from emailmimeimage import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 
 
# Create message container - the correct MIME type is multipart/alternative 
msg = MIMEMultipart('alternative') 
msg['Subject'] = "Link" 
 
# Create the body of the message (a plain-text and an HTML version) 
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://wwwpythonorg" 
html = """\ 
<html> 
 <head></head> 
 <body> 
  <p>Hi!<br> 
    How are you?<br> 
    Here is the <a href="http://wwwpythonorg">link</a> you wanted 
  </p> 
 </body> 
</html> 
""" 
 
# Record the MIME types of both parts - text/plain and text/html 
part1 = MIMEText(text, 'plain') 
part2 = MIMEText(html, 'html') 
 
# Attach parts into message container 
# According to RFC 2046, the last part of a multipart message, in this case 
# the HTML message, is best and preferred 
msgattach(part1) 
msgattach(part2) 
#构造附件 
att = MIMEText(open('h:\\python\\jpg', 'rb')read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att["Content-Disposition"] = 'attachment; filename="jpg"' 
msgattach(att) 
   
smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgas_string()) 
smtpquit()

基于SSL的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimetext import MIMEText 
from emailheader import Header 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要 
msg['Subject'] = Header(subject, 'utf-8') 
 
smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtpehlo() 
smtpstarttls() 
smtpehlo() 
smtpset_debuglevel(1) 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgas_string()) 
smtpquit()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python的Twisted框架中使用Deferred对象来管理回调函数
May 25 Python
python目录与文件名操作例子
Aug 28 Python
Python实现曲线点抽稀算法的示例
Oct 12 Python
Python实现图片转字符画的代码实例
Feb 22 Python
Python二叉树的镜像转换实现方法示例
Mar 06 Python
使用 Supervisor 监控 Python3 进程方式
Dec 05 Python
使用Pandas将inf, nan转化成特定的值
Dec 19 Python
Python字典深浅拷贝与循环方式方法详解
Feb 09 Python
解决pycharm中opencv-python导入cv2后无法自动补全的问题(不用作任何文件上的修改)
Mar 05 Python
Python爬虫实现vip电影下载的示例代码
Apr 20 Python
tensorflow转换ckpt为savermodel模型的实现
May 25 Python
在tensorflow以及keras安装目录查询操作(windows下)
Jun 19 Python
使用Python3 编写简单信用卡管理程序
Dec 21 #Python
Python 遍历子文件和所有子文件夹的代码实例
Dec 21 #Python
详解python中的json的基本使用方法
Dec 21 #Python
win系统下为Python3.5安装flask-mongoengine 库
Dec 20 #Python
python查看微信好友是否删除自己
Dec 19 #Python
python用reduce和map把字符串转为数字的方法
Dec 19 #Python
python虚拟环境virualenv的安装与使用
Dec 18 #Python
You might like
fgetcvs在linux的问题
2012/01/15 PHP
PHP生成自适应大小的缩略图类及使用方法分享
2014/05/06 PHP
Laravel 实现Controller向blade前台模板赋值的四种方式小结
2019/10/22 PHP
Jquery 获得服务器控件值的方法小结
2010/05/11 Javascript
js定义对象或数组直接量时各浏览器对多余逗号的处理(json)
2011/03/05 Javascript
EXTjs4.0的store的findRecord的BUG演示代码
2013/06/08 Javascript
js实现格式化金额,字符,时间的方法
2015/02/26 Javascript
Node.js 学习笔记之简介、安装及配置
2015/03/03 Javascript
jQuery使用empty()方法删除元素及其所有子元素的方法
2015/03/26 Javascript
js实现同一页面多个运动效果的方法
2015/04/10 Javascript
jquery+json实现数据二级联动的方法
2015/11/28 Javascript
Jquery插件easyUi实现表单验证示例
2015/12/15 Javascript
JS给swf传参数的实现方法
2016/09/13 Javascript
原生js实现类似fullpage的单页/全屏滚动
2017/01/22 Javascript
react-router实现按需加载
2017/05/09 Javascript
vue 全选与反选的实现方法(无Bug 新手看过来)
2018/02/09 Javascript
详解如何webpack使用DllPlugin
2018/09/30 Javascript
JavaScript实现动态添加、移除元素或属性的方法分析
2019/01/03 Javascript
vue+iview 兼容IE11浏览器的实现方法
2019/01/07 Javascript
浅谈javascript中的prototype和__proto__的理解
2019/04/07 Javascript
JavaScript中工厂函数与构造函数示例详解
2019/05/06 Javascript
javascript-hashchange事件和历史状态管理实例分析
2020/04/18 Javascript
详解vite2.0配置学习(typescript版本)
2021/02/25 Javascript
[51:11]2014 DOTA2国际邀请赛中国区预选赛5.21 LGD-CDEC VS DT
2014/05/22 DOTA
[40:19]2018完美盛典CS.GO表演赛
2018/12/17 DOTA
Python通过解析网页实现看报程序的方法
2014/08/04 Python
在Linux下调试Python代码的各种方法
2015/04/17 Python
对python xlrd读取datetime类型数据的方法详解
2018/12/26 Python
详解用Python为直方图绘制拟合曲线的两种方法
2019/08/21 Python
高中毕业自我鉴定
2013/12/22 职场文书
美术专业个人自我评价
2014/01/18 职场文书
会计系毕业生求职信
2014/05/28 职场文书
励志语录:时光飞逝,请学会珍惜所有的人和事
2020/01/16 职场文书
Oracle 区块链表创建过程详解
2021/05/15 Oracle
go语言使用Casbin实现角色的权限控制
2021/06/26 Golang
JavaScript正则表达式实现注册信息校验功能
2022/05/30 Java/Android