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 相关文章推荐
利用matplotlib+numpy绘制多种绘图的方法实例
May 03 Python
Python中异常重试的解决方案详解
May 05 Python
浅谈django开发者模式中的autoreload是如何实现的
Aug 18 Python
Django ORM框架的定时任务如何使用详解
Oct 19 Python
python numpy格式化打印的实例
May 14 Python
Python3利用Dlib实现摄像头实时人脸检测和平铺显示示例
Feb 21 Python
python SVM 线性分类模型的实现
Jul 19 Python
对Django url的几种使用方式详解
Aug 06 Python
Python基于QQ邮箱实现SSL发送
Apr 26 Python
浅谈keras中的keras.utils.to_categorical用法
Jul 02 Python
如何在vscode中安装python库的方法步骤
Jan 06 Python
详解如何修改jupyter notebook的默认目录和默认浏览器
Jan 24 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
PHP中文汉字验证码
2007/04/08 PHP
php include类文件超时问题处理
2015/02/06 PHP
PHP中curl_setopt函数用法实例分析
2015/04/16 PHP
php简单随机字符串生成方法示例
2017/04/19 PHP
如何用javascript判断录入的日期是否合法
2007/01/08 Javascript
jqGrid jQuery 表格插件测试代码
2011/08/23 Javascript
node.js中的fs.createReadStream方法使用说明
2014/12/17 Javascript
angular.element方法汇总
2015/01/07 Javascript
基于JavaScript实现手机短信按钮倒计时(超简单)
2015/12/30 Javascript
学习JavaScript设计模式之代理模式
2016/01/12 Javascript
Vue CLI3 如何支持less的方法示例
2018/08/29 Javascript
layer.alert回调函数执行关闭弹窗的实例
2019/09/11 Javascript
vuex存储复杂参数(如对象数组等)刷新数据丢失的解决方法
2019/11/05 Javascript
vue 实现锚点功能操作
2020/08/10 Javascript
基于elementUI竖向表格、和并列的案例
2020/10/26 Javascript
Python实现Const详解
2015/01/27 Python
Python导入txt数据到mysql的方法
2015/04/08 Python
python创建和删除目录的方法
2015/04/29 Python
Python实现爬取逐浪小说的方法
2015/07/07 Python
python urllib爬取百度云连接的实例代码
2017/06/19 Python
Python中标准库OS的常用方法总结大全
2017/07/19 Python
python中 logging的使用详解
2017/10/25 Python
对python requests发送json格式数据的实例详解
2018/12/19 Python
教你一步步利用python实现贪吃蛇游戏
2019/06/27 Python
python实现日志按天分割
2019/07/22 Python
python 利用已有Ner模型进行数据清洗合并代码
2019/12/24 Python
解决python -m pip install --upgrade pip 升级不成功问题
2020/03/05 Python
Python Selenium截图功能实现代码
2020/04/26 Python
彻底解决pip下载pytorch慢的问题方法
2021/03/01 Python
CSS3弹性布局内容对齐(justify-content)属性使用详解
2017/07/31 HTML / CSS
新西兰领先的鞋类和靴子网上商城:Merchant 1948
2017/09/08 全球购物
澳洲女装时尚在线:Blue Bungalow
2018/05/05 全球购物
酒店管理专业学生求职信
2013/09/27 职场文书
先进事迹材料怎么写
2014/12/30 职场文书
2015年母亲节活动总结
2015/02/10 职场文书
公司门卫岗位职责
2015/04/13 职场文书