详解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 win32 简单操作方法
May 25 Python
Python网络爬虫中的同步与异步示例详解
Feb 03 Python
python实现五子棋小游戏
Mar 25 Python
Python使用while循环花式打印乘法表
Jan 28 Python
Python实现二叉树的常见遍历操作总结【7种方法】
Mar 06 Python
python网络爬虫 Scrapy中selenium用法详解
Sep 28 Python
利用Python代码实现一键抠背景功能
Dec 29 Python
使用Numpy对特征中的异常值进行替换及条件替换方式
Jun 08 Python
python怎么删除缓存文件
Jul 19 Python
用python对oracle进行简单性能测试
Dec 05 Python
关于探究python中sys.argv时遇到的问题详解
Feb 23 Python
超级详细实用的pycharm常用快捷键
May 12 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自动跳转中英文页面
2008/07/29 PHP
AMFPHP php远程调用(RPC, Remote Procedure Call)工具 快速入门教程
2010/05/10 PHP
sql注入与转义的php函数代码
2013/06/17 PHP
PHP数据库操作之基于Mysqli的数据库操作类库
2014/04/19 PHP
PHP启动windows应用程序、执行bat批处理、执行cmd命令的方法(exec、system函数详解)
2014/10/20 PHP
Joomla实现组件中弹出一个模式(modal)窗口的方法
2016/05/04 PHP
PHP编程实现阳历转换为阴历的方法实例
2017/08/08 PHP
JavaScript中的Window窗口对象
2008/01/16 Javascript
基于jquery实现的图片在各种分辨率下未知的容器内上下左右居中
2014/05/11 Javascript
JavaScript实现在数组中查找不同顺序排列的字符串
2014/09/26 Javascript
JavaScript生成随机字符串的方法
2015/03/19 Javascript
jQuery插件slick实现响应式移动端幻灯片图片切换特效
2015/04/12 Javascript
jquery淡入淡出效果简单实例
2016/01/14 Javascript
jquery validate表单验证插件
2016/09/06 Javascript
JavaScript使用链式方法封装jQuery中CSS()方法示例
2017/04/07 jQuery
Vue.js实现价格计算器功能
2020/03/30 Javascript
Django中使用jquery的ajax进行数据交互的实例代码
2017/10/15 jQuery
vue中实现图片和文件上传的示例代码
2018/03/16 Javascript
使用vue根据状态添加列表数据和删除列表数据的实例
2018/09/29 Javascript
详解ES6系列之私有变量的实现
2018/11/21 Javascript
实例讲解JavaScript 计时事件
2020/07/04 Javascript
[01:04:06]DOTA2上海特级锦标赛A组资格赛#2 Secret VS EHOME第一局
2016/02/26 DOTA
[01:36]极致酷炫!TI9典藏宝瓶+撼地者至宝展示
2019/06/11 DOTA
Python set集合类型操作总结
2014/11/07 Python
python实现单向链表详解
2018/02/08 Python
python实现文本界面网络聊天室
2018/12/12 Python
解决新django中的path不能使用正则表达式的问题
2018/12/18 Python
对python读取CT医学图像的实例详解
2019/01/24 Python
使用Python进行中文繁简转换的实现代码
2019/10/18 Python
龟牌英国商店:Turtle Wax Brand Store UK
2019/07/02 全球购物
劳动模范事迹材料
2014/01/19 职场文书
社区领导班子四风问题原因分析及整改措施
2014/09/28 职场文书
乱丢垃圾袋检讨书
2014/10/08 职场文书
详解在OpenCV中如何使用图像像素
2022/03/03 Python
解决vue-router的beforeRouteUpdate不能触发
2022/04/14 Vue.js
MySQL的prepare使用以及遇到的bug
2022/05/11 MySQL