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实现socket端口重定向示例
Feb 10 Python
python实现忽略大小写对字符串列表排序的方法
Sep 25 Python
浅析Python中的多重继承
Apr 28 Python
Python 'takes exactly 1 argument (2 given)' Python error
Dec 13 Python
使用python telnetlib批量备份交换机配置的方法
Jul 25 Python
处理python中多线程与多进程中的数据共享问题
Jul 28 Python
Django对models里的objects的使用详解
Aug 17 Python
python Popen 获取输出,等待运行完成示例
Dec 30 Python
Python分析最近大火的网剧《隐秘的角落》
Jul 02 Python
python求解汉诺塔游戏
Jul 09 Python
python中使用.py配置文件的方法详解
Nov 23 Python
使用Django框架创建项目
Jun 10 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中PDO的错误处理
2011/09/04 PHP
开源php中文分词系统SCWS安装和使用实例
2014/04/11 PHP
php in_array() 检查数组中是否存在某个值详解
2016/11/23 PHP
简短几句jquery代码的实现一个图片向上滚动切换
2011/09/02 Javascript
点评js异步加载的4种方式
2015/12/22 Javascript
JS中微信小程序自定义底部弹出框
2016/12/22 Javascript
JavaScript实现焦点进入文本框内关闭输入法的核心代码
2017/09/20 Javascript
微信小程序功能之全屏滚动效果的实现代码
2018/11/22 Javascript
JS无限级导航菜单实现方法
2019/01/05 Javascript
redux.js详解及基本使用
2019/05/24 Javascript
JS去除字符串最后的逗号实例分析【四种方法】
2019/06/20 Javascript
超详细小程序定位地图模块全系列开发教学
2020/11/24 Javascript
vue3 watch和watchEffect的使用以及有哪些区别
2021/01/26 Vue.js
Python实现字典的遍历与排序功能示例
2017/12/23 Python
Python入门必须知道的11个知识点
2018/03/21 Python
Pandas:DataFrame对象的基础操作方法
2018/06/07 Python
python实现两张图片的像素融合
2019/02/23 Python
python实现坦克大战游戏 附详细注释
2020/03/27 Python
Python 实现训练集、测试集随机划分
2020/01/08 Python
Python关键字及可变参数*args,**kw原理解析
2020/04/04 Python
Python matplotlib可视化实例解析
2020/06/01 Python
详解CSS3中常用的样式【基本文本和字体样式】
2020/10/20 HTML / CSS
英国内衣连锁店:Boux Avenue
2018/01/24 全球购物
法国一家芭蕾舞鞋公司:Repetto
2018/11/12 全球购物
购买正版游戏和游戏激活码:Green Man Gaming
2019/11/06 全球购物
给幼儿园老师的表扬信
2014/01/19 职场文书
员工培训邀请函
2014/02/02 职场文书
中专自我鉴定
2014/02/05 职场文书
2014年父亲节活动方案
2014/03/06 职场文书
《红军不怕远征难》教学反思
2014/04/14 职场文书
食品安全宣传标语
2014/06/07 职场文书
2016春节家属慰问信
2015/03/25 职场文书
2015年社区文体活动总结
2015/03/25 职场文书
孔子观后感
2015/06/08 职场文书
Django Paginator分页器的使用示例
2021/06/23 Python
简单聊聊TypeScript只读修饰符
2022/04/06 Javascript