python邮件发送smtplib使用详解


Posted in Python onJune 16, 2020

本文实例为大家分享了python邮件发送smtplib使用具体代码,供大家参考,具体内容如下

文件形式的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
from email.header import Header 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要 
msg['Subject'] = Header(subject, 'utf-8') 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit()

HTML形式的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8') 
 
msg['Subject'] = subject 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit()

带图片的HTML邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
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') 
msgRoot.attach(msgText) 
 
fp = open('h:\\python\\1.jpg', 'rb') 
msgImage = MIMEImage(fp.read()) 
fp.close() 
 
msgImage.add_header('Content-ID', '<image1>') 
msgRoot.attach(msgImage) 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msgRoot.as_string()) 
smtp.quit()

带附件的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msgRoot = MIMEMultipart('related') 
msgRoot['Subject'] = 'test message' 
 
#构造附件 
att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att["Content-Disposition"] = 'attachment; filename="1.jpg"' 
msgRoot.attach(att) 
  
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msgRoot.as_string()) 
smtp.quit()

群发邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
 
sender = '***' 
receiver = ['***','****',……] 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8') 
 
msg['Subject'] = subject 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit()

各种元素都包含的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
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://www.python.org" 
html = """\ 
<html> 
 <head></head> 
 <body> 
 <p>Hi!<br> 
 How are you?<br> 
 Here is the <a href="http://www.python.org" rel="external nofollow" >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. 
msg.attach(part1) 
msg.attach(part2) 
#构造附件 
att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att["Content-Disposition"] = 'attachment; filename="1.jpg"' 
msg.attach(att) 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit()

基于SSL的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
from email.header import Header 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要 
msg['Subject'] = Header(subject, 'utf-8') 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.ehlo() 
smtp.starttls() 
smtp.ehlo() 
smtp.set_debuglevel(1) 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python列出一个文件夹及其子目录的所有文件
Jun 30 Python
Python中的复制操作及copy模块中的浅拷贝与深拷贝方法
Jul 02 Python
Python利用turtle库绘制彩虹代码示例
Dec 20 Python
详解用python实现简单的遗传算法
Jan 02 Python
python控制windows剪贴板,向剪贴板中写入图片的实例
May 31 Python
mac下给python3安装requests库和scrapy库的实例
Jun 13 Python
python实现汽车管理系统
Nov 30 Python
对python字典过滤条件的实例详解
Jan 22 Python
Django之使用celery和NGINX生成静态页面实现性能优化
Oct 08 Python
python 获取字典键值对的实现
Nov 12 Python
Python爬虫之Selenium鼠标事件的实现
Dec 04 Python
python实战之用emoji表情生成文字
May 08 Python
Python3多线程操作简单示例
May 22 #Python
Python3基于sax解析xml操作示例
May 22 #Python
Python smtplib实现发送邮件功能
May 22 #Python
linux下python使用sendmail发送邮件
May 22 #Python
Python实现的文本对比报告生成工具示例
May 22 #Python
python smtplib模块实现发送邮件带附件sendmail
May 22 #Python
点球小游戏python脚本
May 22 #Python
You might like
ThinkPHP模板范围判断输出In标签与Range标签用法详解
2014/06/30 PHP
PHP面向对象程序设计实例分析
2016/01/26 PHP
浅析PHP中的i++与++i的区别及效率
2016/06/15 PHP
yum命令安装php7和相关扩展
2016/07/04 PHP
PHP中spl_autoload_register()函数用法实例详解
2016/07/18 PHP
PHP Cookie学习笔记
2016/08/23 PHP
模拟用户操作Input元素,不会触发相应事件
2007/05/11 Javascript
比较简单实用的使用正则三种版本的js去空格处理方法
2007/11/18 Javascript
jQuery中live方法的重复绑定说明
2011/10/21 Javascript
js+css实现增加表单可用性之提示文字
2013/06/03 Javascript
jquery点击页面任何区域实现鼠标焦点十字效果
2013/06/21 Javascript
js语法学习之判断一个对象是否为数组
2014/05/13 Javascript
为什么Node.js会这么火呢?Node.js流行的原因
2014/12/01 Javascript
jQuery中选择器的基础使用教程
2016/05/23 Javascript
javascript解析ajax返回的xml和json格式数据实例详解
2017/01/05 Javascript
vue元素实现动画过渡效果
2017/07/01 Javascript
js实现一个简单的MVVM框架示例
2018/01/15 Javascript
js实现简单模态框实例
2018/11/16 Javascript
使用vue-cli3新建一个项目并写好基本配置(推荐)
2019/04/24 Javascript
JavaScript数组类型Array相关的属性与方法详解
2020/09/08 Javascript
javascript实现时钟动画
2020/12/03 Javascript
python xml.etree.ElementTree遍历xml所有节点实例详解
2016/12/04 Python
CentOS下使用yum安装python-pip失败的完美解决方法
2017/08/16 Python
python实现归并排序算法
2018/11/22 Python
selenium设置proxy、headers的方法(phantomjs、Chrome、Firefox)
2018/11/29 Python
django-orm F对象的使用 按照两个字段的和,乘积排序实例
2020/05/18 Python
Python程序慢的重要原因
2020/09/04 Python
详解CSS3新增的背景属性
2019/12/25 HTML / CSS
AmazeUI 手机版页面的顶部导航条Header与侧边导航栏offCanvas的示例代码
2020/08/19 HTML / CSS
水芝澳美国官网:H2O Plus
2016/10/15 全球购物
澳大利亚拥有最佳跳伞降落点和最好服务的跳伞项目运营商:Skydive Australia
2018/03/05 全球购物
SQL数据库笔试题
2016/03/08 面试题
Static Nested Class 和 Inner Class的不同
2013/11/28 面试题
人事行政专员岗位职责
2014/07/23 职场文书
还款承诺书范本
2015/01/20 职场文书
政协委员个人总结
2015/03/03 职场文书