详解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标准库os.path包、glob包使用实例
Nov 25 Python
Python 包含汉字的文件读写之每行末尾加上特定字符
Dec 12 Python
Python实现利用163邮箱远程关电脑脚本
Feb 22 Python
简单谈谈Python的pycurl模块
Apr 07 Python
python爱心表白 每天都是浪漫七夕!
Aug 18 Python
Python英文文本分词(无空格)模块wordninja的使用实例
Feb 20 Python
python binascii 进制转换实例
Jun 12 Python
Python 迭代,for...in遍历,迭代原理与应用示例
Oct 12 Python
python列表推导式操作解析
Nov 26 Python
pycharm快捷键汇总
Feb 14 Python
Matlab使用Plot函数实现数据动态显示方法总结
Feb 25 Python
Python Django / Flask如何使用Elasticsearch
Apr 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
PHP学习笔记 (1) 环境配置与代码调试
2011/06/19 PHP
PHP session会话的安全性分析
2011/09/08 PHP
PHP简单实现解析xml为数组的方法
2018/05/02 PHP
深入解析PHP底层机制及相关原理
2020/12/11 PHP
更正确的asp冒泡排序
2007/05/24 Javascript
JavaScript中使用构造函数实现继承的代码
2010/08/12 Javascript
jQuery Tools Dateinput使用介绍
2012/07/14 Javascript
js判断undefined类型示例代码
2014/02/10 Javascript
js,jquery滚动/跳转页面到指定位置的实现思路
2014/06/03 Javascript
node.js中RPC(远程过程调用)的实现原理介绍
2014/12/05 Javascript
jquery单击事件和双击事件冲突解决方案
2016/03/02 Javascript
基于bootstrap实现广告轮播带图片和文字效果
2016/07/22 Javascript
jQuery基本过滤选择器用法示例
2016/09/09 Javascript
基于Vue中点击组件外关闭组件的实现方法
2018/03/06 Javascript
解决Nodejs全局安装模块后找不到命令的问题
2018/05/15 NodeJs
小谈angular ng deploy的实现
2020/04/07 Javascript
vue-router为激活的路由设置样式操作
2020/07/18 Javascript
Python 26进制计算实现方法
2015/05/28 Python
python实现从wind导入数据
2019/12/03 Python
django 模型字段设置默认值代码
2020/07/15 Python
Python爬虫爬取糗事百科段子实例分享
2020/07/31 Python
英国领先的亚洲旅游专家:Wendy Wu Tours
2018/01/21 全球购物
在weblogic中发布ejb需涉及到哪些配置文件
2012/01/17 面试题
业务经理岗位职责
2013/11/11 职场文书
班级年度安全计划书
2014/05/01 职场文书
治庸问责心得体会
2014/09/12 职场文书
大学生实习证明范文(5篇)
2014/09/18 职场文书
个人批评与自我批评范文
2014/10/17 职场文书
社区党的群众路线教育实践活动总结材料
2014/10/31 职场文书
毕业生对母校寄语
2015/02/26 职场文书
初中生思想道德自我评价
2015/03/09 职场文书
面试通知短信
2015/04/20 职场文书
黑白记忆观后感
2015/06/18 职场文书
职工宿舍管理制度
2015/08/05 职场文书
假期读书倡议书3篇
2019/08/19 职场文书
instantclient客户端 连接oracle数据库
2022/04/26 Oracle