详解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三元运算符实现方法
Dec 17 Python
Python中使用md5sum检查目录中相同文件代码分享
Feb 02 Python
Python 查看文件的编码格式方法
Dec 21 Python
Python中的二维数组实例(list与numpy.array)
Apr 13 Python
Python 支付整合开发包的实现
Jan 23 Python
Mac 使用python3的matplot画图不显示的解决
Nov 23 Python
selenium+python实现自动登陆QQ邮箱并发送邮件功能
Dec 13 Python
python dumps和loads区别详解
Feb 04 Python
python3+opencv 使用灰度直方图来判断图片的亮暗操作
Jun 02 Python
Matlab中plot基本用法的具体使用
Jul 17 Python
手把手教你用Django执行原生SQL的方法
Feb 18 Python
python编程学习使用管道Pipe编写优化代码
Nov 20 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
PHP获取当前日期和时间及格式化方法参数
2015/05/11 PHP
php中关于socket的系列函数总结
2015/05/18 PHP
一个简单的php MVC留言本实例代码(必看篇)
2016/09/22 PHP
PHP中常见的密码处理方式和建议总结
2018/10/14 PHP
js获取当前日期代码适用于网页头部
2013/06/27 Javascript
JavaScript全排列的六种算法 具体实现
2013/06/29 Javascript
用Javascript来生成ftp脚本的小例子
2013/07/03 Javascript
JS和Jquery获取和修改label的值的示例代码
2014/01/15 Javascript
js实现精确到秒的日期选择器完整实例
2016/04/30 Javascript
js返回顶部实例分享
2016/12/21 Javascript
简单实现Vue的observer和watcher
2016/12/21 Javascript
jquery uploadify隐藏上传进度的实现方法
2017/02/06 Javascript
Vue响应式原理深入解析及注意事项
2017/12/11 Javascript
Vue实现表格中对数据进行转换、处理的方法
2018/09/06 Javascript
浅谈HTTP 缓存的那些事儿
2018/10/17 Javascript
vue组件实践之可搜索下拉框功能
2018/11/25 Javascript
详解Vue的ref特性的使用
2020/01/24 Javascript
vue实现登录拦截
2020/06/29 Javascript
Javascript如何递归遍历本地文件夹
2020/08/06 Javascript
vue 递归组件的简单使用示例
2021/01/14 Vue.js
python查询sqlite数据表的方法
2015/05/08 Python
Python实现的概率分布运算操作示例
2017/08/14 Python
python+opencv实现的简单人脸识别代码示例
2017/11/14 Python
Python基于递归算法实现的汉诺塔与Fibonacci数列示例
2018/04/18 Python
Python对HTML转义字符进行反转义的实现方法
2019/04/28 Python
Python搭建代理IP池实现接口设置与整体调度
2019/10/27 Python
html5实现微信打飞机游戏
2014/03/27 HTML / CSS
世界第一曲奇连锁店:Mrs. Fields Cookies
2017/02/04 全球购物
越南电子产品购物网站:FPT Shop
2017/12/02 全球购物
西班牙手机之家:Phone House
2018/10/18 全球购物
中学生个人自我评价
2014/02/06 职场文书
餐厅周年庆活动方案
2014/08/25 职场文书
不服从上级领导安排的检讨书
2014/09/14 职场文书
出国留学单位推荐信
2015/03/26 职场文书
大学体育课感想
2015/08/10 职场文书
Python基础教程,Python入门教程(超详细)
2021/06/24 Python