详解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中stdout输出不缓存的设置方法
May 29 Python
以Python的Pyspider为例剖析搜索引擎的网络爬虫实现方法
Mar 30 Python
举例讲解Python中is和id的用法
Apr 03 Python
简单介绍Python中的struct模块
Apr 28 Python
Python中字符串的常见操作技巧总结
Jul 28 Python
Python如何快速实现分布式任务
Jul 06 Python
Opencv-Python图像透视变换cv2.warpPerspective的示例
Apr 11 Python
利用Python检测URL状态
Jul 31 Python
PyQt5结合matplotlib绘图的实现示例
Sep 15 Python
python 多进程和协程配合使用写入数据
Oct 30 Python
Python实现视频中添加音频工具详解
Dec 06 Python
5个pandas调用函数的方法让数据处理更加灵活自如
Apr 24 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生成静态HTML速度快类库
2007/03/18 PHP
php结合飞信 免费天气预报短信
2009/05/07 PHP
一个JavaScript继承的实现
2006/10/24 Javascript
jQuery学习笔记之jQuery.extend(),jQuery.fn.extend()分析
2014/06/09 Javascript
JavaScript比较两个对象是否相等的方法
2015/02/06 Javascript
JavaScript实现的选择排序算法实例分析
2017/04/14 Javascript
微信页面弹出键盘后iframe内容变空白的解决方案
2017/09/20 Javascript
jQuery读取本地的json文件(实例讲解)
2017/10/31 jQuery
three.js中文文档学习之如何本地运行详解
2017/11/20 Javascript
完美解决axios在ie下的兼容性问题
2018/03/05 Javascript
原生js实现淘宝放大镜效果
2020/10/28 Javascript
深入浅析nuxt.js基于ssh的vue通用框架
2019/05/21 Javascript
用 js 写一个 js 解释器过程详解
2019/08/02 Javascript
nodemon实现Typescript项目热更新的示例代码
2019/11/19 Javascript
js实现特别简单的钟表效果
2020/09/14 Javascript
[00:52]黑暗之门更新 新英雄孽主驾临DOTA2
2016/08/24 DOTA
[01:00:53]OG vs IG 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
python3使用urllib模块制作网络爬虫
2016/04/08 Python
Python 编码处理-str与Unicode的区别
2016/09/06 Python
Python多进程并发与多线程并发编程实例总结
2018/02/08 Python
python语言中with as的用法使用详解
2018/02/23 Python
flask框架视图函数用法示例
2018/07/19 Python
python调试神器PySnooper的使用
2019/07/03 Python
win10子系统python开发环境准备及kenlm和nltk的使用教程
2019/10/14 Python
Python搭建代理IP池实现接口设置与整体调度
2019/10/27 Python
Python中itertools的用法详解
2020/02/07 Python
Python类及获取对象属性方法解析
2020/06/15 Python
高分子材料与工程专业推荐信
2013/12/01 职场文书
四个太阳教学反思
2014/02/01 职场文书
留学生求职信
2014/06/03 职场文书
《叶问2》观后感
2015/06/15 职场文书
2015秋季开学典礼致辞
2015/07/16 职场文书
浅谈react useEffect闭包的坑
2021/06/08 Javascript
Apache Linkis 中间件架构及快速安装步骤
2022/03/16 Servers
《极主夫道》真人电影正式预告 定档6月3日上映
2022/04/05 日漫
ECharts transform数据转换和dataZoom在项目中使用
2022/12/24 Javascript