详解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 相关文章推荐
linux系统使用python监控apache服务器进程脚本分享
Jan 15 Python
python网络编程学习笔记(一)
Jun 09 Python
python 实现网上商城,转账,存取款等功能的信用卡系统
Jul 15 Python
Python常用算法学习基础教程
Apr 13 Python
Python实现字符串与数组相互转换功能示例
Sep 22 Python
Django使用Mysql数据库已经存在的数据表方法
May 27 Python
selenium python 实现基本自动化测试的示例代码
Feb 25 Python
Python3利用print输出带颜色的彩色字体示例代码
Apr 08 Python
简单易懂Pytorch实战实例VGG深度网络
Aug 27 Python
Python socket模块方法实现详解
Nov 05 Python
Python测试线程应用程序过程解析
Dec 31 Python
pytorch 修改预训练model实例
Jan 18 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/03/16 PHP
PHP连接access数据库
2015/03/27 PHP
Yii获取当前url和域名的方法
2015/06/08 PHP
Smarty简单生成表单元素的方法示例
2016/05/23 PHP
PHP实现的DES加密解密封装类完整实例
2017/04/29 PHP
javascript分页代码(当前页码居中)
2012/09/20 Javascript
js实现通用的微信分享组件示例
2014/03/10 Javascript
详解JavaScript的变量和数据类型
2015/11/27 Javascript
JS组件Bootstrap Table使用方法详解
2016/02/02 Javascript
Angularjs中如何使用filterFilter函数过滤
2016/02/06 Javascript
浅谈jquery中next与siblings的区别
2016/10/27 Javascript
js实现选项卡内容切换以及折叠和展开效果【推荐】
2017/01/08 Javascript
微信小程序 下拉菜单的实现
2017/04/06 Javascript
JavaScript的setter与getter方法
2017/11/29 Javascript
详解vue添加删除元素的方法
2018/06/30 Javascript
nodejs遍历文件夹下并操作HTML/CSS/JS/PNG/JPG的方法
2018/11/01 NodeJs
简单使用webpack打包文件的实现
2019/10/29 Javascript
jquery选择器和属性对象的操作实例分析
2020/01/10 jQuery
基于element-ui封装可搜索的懒加载tree组件的实现
2020/05/22 Javascript
python自动化测试之连接几组测试包实例
2014/09/28 Python
Python脚本在Appium库上对移动应用实现自动化测试
2015/04/17 Python
Python检测字符串中是否包含某字符集合中的字符
2015/05/21 Python
Python实现在线暴力破解邮箱账号密码功能示例【测试可用】
2017/09/06 Python
5个很好的Python面试题问题答案及分析
2018/01/19 Python
在python中,使用scatter绘制散点图的实例
2019/07/03 Python
Python数据可视化:泊松分布详解
2019/12/07 Python
python def 定义函数,调用函数方式
2020/06/02 Python
CSS3中引入多种自定义字体font-face
2020/06/12 HTML / CSS
css3中flex布局宽度不生效的解决
2020/12/09 HTML / CSS
布鲁明戴尔百货店:Bloomingdale’s
2016/12/21 全球购物
财务副总经理工作职责
2013/11/25 职场文书
大学生职业规划范文:象牙塔生活的四年计划
2014/01/14 职场文书
党员公开承诺践诺书
2014/03/25 职场文书
小学庆六一主持词
2015/06/30 职场文书
辩论赛新闻稿
2015/07/17 职场文书
2015年统计员个人工作总结
2015/07/23 职场文书