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打包方法Pyinstaller的使用
Oct 09 Python
python调用c++ ctype list传数组或者返回数组的方法
Feb 13 Python
浅谈pyqt5在QMainWindow中布局的问题
Jun 21 Python
Django框架orM与自定义SQL语句混合事务控制操作
Jun 27 Python
Python箱型图处理离群点的例子
Dec 09 Python
Python实现中值滤波去噪方式
Dec 18 Python
Python 实现Serial 与STM32J进行串口通讯
Dec 18 Python
python基于三阶贝塞尔曲线的数据平滑算法
Dec 27 Python
django在保存图像的同时压缩图像示例代码详解
Feb 11 Python
TensorFlow打印输出tensor的值
Apr 19 Python
python实现数字炸弹游戏
Jul 17 Python
Python将CSV文件转化为HTML文件的操作方法
Jun 30 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
apache和php之间协同工作的配置经验分享
2013/04/08 PHP
PHP中使用数组指针函数操作数组示例
2014/11/19 PHP
php使用iconv中文截断问题的解决方法
2015/02/11 PHP
PHP的RSA加密解密方法以及开发接口使用
2018/02/11 PHP
php查询内存信息操作示例
2019/05/09 PHP
laravel5.1框架基础之Blade模板继承简单使用方法分析
2019/09/05 PHP
比较详细的关于javascript 解析json的代码
2009/12/16 Javascript
Javascript Function对象扩展之延时执行函数
2010/07/06 Javascript
jquery获取一组checkbox的值(实例代码)
2013/11/04 Javascript
jquery中change()用法实例分析
2015/02/06 Javascript
JS实时弹出新消息提示框并有提示音响起的实现代码
2016/04/20 Javascript
浅谈Angularjs link和compile的使用区别
2016/10/21 Javascript
Vue.js弹出模态框组件开发的示例代码
2017/07/26 Javascript
angular中不同的组件间传值与通信的方法
2017/11/04 Javascript
Vant的安装和配合引入Vue.js项目里的方法步骤
2018/12/05 Javascript
微信小程序实现元素渐入渐出动画效果封装方法
2019/05/18 Javascript
vue-cli3 取消eslint校验代码的解决办法
2020/01/16 Javascript
BootStrap前端框架使用方法详解
2020/02/26 Javascript
[00:13]天涯墨客二技能展示
2018/08/25 DOTA
使用python读取txt文件的内容,并删除重复的行数方法
2018/04/18 Python
Python创建普通菜单示例【基于win32ui模块】
2018/05/09 Python
使用Python处理Excel表格的简单方法
2018/06/07 Python
Python unittest 简单实现参数化的方法
2018/11/30 Python
python模拟登陆,用session维持回话的实例
2018/12/27 Python
Python基础之字符串常见操作经典实例详解
2020/02/26 Python
python实现电子词典
2020/03/03 Python
PyCharm中如何直接使用Anaconda已安装的库
2020/05/28 Python
Python用Jira库来操作Jira
2020/12/28 Python
日本无添加化妆品:HABA
2016/08/18 全球购物
公积金转移接收函
2014/01/11 职场文书
学校节能减排倡议书
2014/05/16 职场文书
教师批评与自我批评材料
2014/10/16 职场文书
2014年社团工作总结范文
2014/11/27 职场文书
教研活动主持词
2015/07/03 职场文书
Redis集群的关闭与重启操作
2021/07/07 Redis
Mysql将字符串按照指定字符分割的正确方法
2022/05/30 MySQL