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 04 Python
python实现发送邮件及附件功能
Mar 02 Python
python爬虫_微信公众号推送信息爬取的实例
Oct 23 Python
python如何统计序列中元素
Jul 31 Python
Python生成器的使用方法和示例代码
Mar 04 Python
django多个APP的urls设置方法(views重复问题解决)
Jul 19 Python
使用 Django Highcharts 实现数据可视化过程解析
Jul 31 Python
在Python中使用MySQL--PyMySQL的基本使用方法
Nov 19 Python
解决Python使用列表副本的问题
Dec 19 Python
Python3 操作 MySQL 插入一条数据并返回主键 id的实例
Mar 02 Python
django haystack实现全文检索的示例代码
Jun 24 Python
Django中的DateTimeField和DateField实现
Feb 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
yii框架源码分析之创建controller代码
2011/06/28 PHP
PHP的一个基础知识 表单提交
2011/07/04 PHP
phpadmin如何导入导出大数据文件及php.ini参数修改
2013/02/18 PHP
一致性哈希算法以及其PHP实现详细解析
2013/08/24 PHP
使用CodeIgniter的类库做图片上传
2014/06/12 PHP
PHP合并静态文件详解
2014/11/14 PHP
Yii2实现UploadedFile上传文件示例
2017/02/15 PHP
PHP编程快速实现数组去重的方法详解
2017/07/22 PHP
动态加载外部javascript文件的函数代码分享
2011/07/28 Javascript
NodeJs中的非阻塞方法介绍
2012/06/05 NodeJs
javascript中的Base64、UTF8编码与解码详解
2015/03/18 Javascript
canvas 绘制圆形时钟
2017/02/22 Javascript
Angular中$state.go页面跳转并传递参数的方法
2017/05/09 Javascript
Vuejs 单文件组件实例详解
2018/02/09 Javascript
JS实现为动态创建的元素添加事件操作示例
2018/03/17 Javascript
element-ui 实现响应式导航栏的示例代码
2020/05/08 Javascript
django多文件上传,form提交,多对多外键保存的实例
2019/08/06 Python
pytorch中的embedding词向量的使用方法
2019/08/18 Python
pygame用blit()实现动画效果的示例代码
2020/05/28 Python
基于keras中的回调函数用法说明
2020/06/17 Python
Biblibili视频投稿接口分析并以Python实现自动投稿功能
2021/02/05 Python
法国时尚童装网站:Melijoe
2016/08/10 全球购物
爱淘宝:淘宝网购物分享平台
2017/04/28 全球购物
Coggles美国/加拿大:高级国际时装零售商
2018/10/23 全球购物
波兰在线香水店:Perfumy.pl
2019/08/12 全球购物
土木工程实习生自我鉴定
2013/09/19 职场文书
一年级语文教学反思
2014/02/13 职场文书
超越自我演讲稿
2014/05/21 职场文书
基层党建工作汇报材料
2014/08/15 职场文书
党员批评与自我批评思想汇报(集锦)
2014/09/14 职场文书
四风问题自我剖析材料
2014/10/07 职场文书
物流业务员岗位职责
2015/04/03 职场文书
廉洁自律心得体会2016
2016/01/13 职场文书
Nginx工作原理和优化总结。
2021/04/02 Servers
CSS中em的正确打开方式详解
2021/04/08 HTML / CSS
Win10服务全部禁用了怎么启动?Win10服务全部禁用解决方法
2022/09/23 数码科技