详解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中字典创建、遍历、添加等实用操作技巧合集
Jun 02 Python
用Python的Django框架来制作一个RSS阅读器
Jul 22 Python
Python实现将sqlite数据库导出转成Excel(xls)表的方法
Jul 17 Python
Python基于scipy实现信号滤波功能
May 08 Python
使用Python进行体育竞技分析(预测球队成绩)
May 16 Python
详解Python字符串切片
May 20 Python
使用pyhon绘图比较两个手机屏幕大小(实例代码)
Jan 03 Python
Python 窗体(tkinter)下拉列表框(Combobox)实例
Mar 04 Python
解决jupyter notebook打不开无反应 浏览器未启动的问题
Apr 10 Python
Python列表嵌套常见坑点及解决方案
Sep 30 Python
用 Python 元类的特性实现 ORM 框架
May 19 Python
FP-growth算法发现频繁项集——构建FP树
Jun 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如何得到当前页和上一页的地址?
2006/11/27 PHP
PHP冒泡排序算法代码详细解读
2011/07/17 PHP
php图片上传存储源码并且可以预览
2011/08/26 PHP
PHP简单实现防止SQL注入的方法
2018/03/13 PHP
兼容FireFox 的 js 日历 支持时间的获取
2009/03/04 Javascript
IE与Firefox在JavaScript上的7个不同写法小结
2009/09/14 Javascript
从jQuery.camelCase()学习string.replace() 函数学习
2011/09/13 Javascript
js实现回放拖拽轨迹从过程上进行分析
2014/06/26 Javascript
如何用JavaScript定义一个类
2014/09/12 Javascript
浅谈javascript中字符串String与数组Array
2014/12/31 Javascript
jQuery应用之jQuery链用法实例
2015/01/19 Javascript
JS烟花背景效果实现方法
2015/03/03 Javascript
TinyMCE提交AjaxForm获取不到数据的解决方法
2015/03/05 Javascript
JS模仿编辑器实时改变文本框宽度和高度大小的方法
2015/08/17 Javascript
jquery插件方式实现table查询功能的简单实例
2016/06/06 Javascript
深入浅析jQuery对象$.html
2016/08/22 Javascript
微信小程序实现实时圆形进度条的方法示例
2017/02/24 Javascript
实例详解BootStrap的动态模态框及静态模态框
2018/08/13 Javascript
vue.js实现的全选与全不选功能示例【基于elementui】
2018/12/03 Javascript
angular4应用中输入的最小值和最大值的方法
2019/05/17 Javascript
JavaScript实现点击切换验证码及校验
2021/01/10 Javascript
JavaScript实现打字游戏
2021/02/19 Javascript
利用Python绘制数据的瀑布图的教程
2015/04/07 Python
Python的GUI框架PySide的安装配置教程
2016/02/16 Python
python的paramiko模块实现远程控制和传输示例
2017/10/13 Python
sublime python3 输入换行不结束的方法
2018/04/19 Python
python简易远程控制单线程版
2018/06/20 Python
关于keras.layers.Conv1D的kernel_size参数使用介绍
2020/05/22 Python
pytorch随机采样操作SubsetRandomSampler()
2020/07/07 Python
python实现计算图形面积
2021/02/22 Python
html5仿支付宝密码框的实现代码
2017/09/06 HTML / CSS
HTML5实现移动端弹幕动画效果
2019/08/01 HTML / CSS
德国户外商店:eXXpozed
2020/07/25 全球购物
军训心得体会
2013/12/31 职场文书
委托公证书
2014/04/08 职场文书
Python何绘制带有背景色块的折线图
2022/04/23 Python