Python使用SMTP模块、email模块发送邮件
一、smtplib模块:
主要通过SMTP类与邮件系统进行交互。使用方法如下:
1.实例化一个SMTP对象:
s = smtplib.SMTP(邮件服务地址,端口号)
s = smtplib.SMTP_SSL(邮件服务地址,端口号)
2.登陆邮件,权限验证:
s.login(用户名,密码)
3.发送邮件:
s.sendmail(发件人邮箱,收件人邮箱,发送内容)
4.断开连接:
s.close()
二、email模块:
email模块:支持发送的邮件内容为纯文本、HTML内容、图片、附件。email模块中有几大类来针对不同的邮件内容形式,常用如下:
MIMEText:(MIME媒体类型)内容形式为纯文本、HTML页面。
MIMEImage:内容形式为图片。
MIMEMultupart:多形式组合,可包含文本和附件。
每一类对应的导入方式:
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
三、MIMEText:
MIMEText(msg,type,chartset)
msg:文本内容
type:文本类型默认为plain(纯文本)
发送HTML格式的时候,修改为html,但同时要求msg的内容也是html的格式。
chartset:文本编码,中文为“utf-8”
# 构造TEXT格式的消息
msg = MIMEText("hello.text","plain","utf-8")
msg["Subject"] = "xxxxx"
msg["From"] = "xxxx"
msg["To"] = "xxxx"
#发送以上构造的邮件内容要使用as_string将构造的邮件内容转换为string形式。
s.sendmail("xxx","xxx",msg.as_string)
四、MIMEImage、MIMEMultipart:
msg = MIMEMultipart()
#实例化一个文本对象
msg_sub = MIMEText("hello.text","plain","utf-8")
#将text消息添加到MIMEMultipart中,作为邮件正文。
msg.attach(msg_sub)
#图片作为附件
import os
img_datas = open(os.getcwd()+ "/reports/xxxx.png","rb").read()
msg_img = MIMEImage(img_data)
msg_img.add_header('Content-Disposition','attachment', filename = "xxxx.png" )
msg_img.add_header('Content-ID','<0>')
#将图片添加到MIMEMultiplart中,作为附件发送。
msg.attach(mag_img)
源代码如下:
发送文本邮件:
import smtplib
from email.mime.text import MIMEText sender = 'xxxx@qq.com' #发送人邮箱
passwd = 'lkugmgywydhfff' #发送人邮箱授权码
receivers = 'xxxx@qq.com' #收件人邮箱 subject = 'python发邮件测试' #主题
content = '这是我使用python smtplib模块和email模块自动发送的邮件' #正文 msg = MIMEText(content,'plain','utf-8')
msg['Subject'] = subject
msg['From'] = sender
msg['TO'] = receivers try:
s = smtplib.SMTP_SSL('smtp.qq.com',465)
s.login(sender,passwd)
s.sendmail(sender,receivers,msg.as_string())
print('发送成功') except Exception:
print('发送失败')
发送HTML邮件:
import smtplib
from email.mime.text import MIMEText
from email.header import Header sender = 'xxxx@qq.com' #发件邮箱
passwd = 'lkugmgywydhfff' #发送人邮箱授权码
receivers = 'xxxx@qq.com' #收件邮箱 subject = 'python发邮Html邮件测试' #主题 content = """ #内容,HTML格式
<p>Python 邮件发送测试...</p>
<p><a href="http://www.baidu.com">这是一个链接</a></p>
""" msg = MIMEText(content,'html','utf-8')
# msg['Subject'] = subject
msg['Subject'] = Header(subject,'utf-8')
# msg['From'] = sender
msg['From'] = Header('大傻子','utf-8')
# msg['To'] = receivers
msg['To'] = Header('二愣子','utf-8')
try:
s = smtplib.SMTP_SSL('smtp.qq.com',465)
s.login(sender,passwd)
s.sendmail(sender,receivers,msg.as_string())
print('Send Success') except:
print('Send Failure')
发送图片邮件:
import smtplib
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart sender = 'xxxx@qq.com'
passwd = 'lkugmgywydhfff'
receivers = 'xxxx@qq.com'
subject = 'python发邮带img的邮件测试' #主题 # 创建一个带附件的实例
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receivers # 创建正文
msg.attach(MIMEText('使用python smtplib模块和email模块自动发送邮件测试','plain','utf-8')) # 创建图片附件
import os
img_file = open(os.getcwd()+"/a4.jpg",'rb').read()
msg_img = MIMEImage(img_file)
msg_img.add_header('Content-Disposition','attachment', filename = "a4.jpg")
msg_img.add_header('Content-ID', '<0>')
msg.attach(msg_img) try:
s = smtplib.SMTP_SSL('smtp.qq.com',465)
s.set_debuglevel(1) #输出发送邮件详细过程
s.login(sender,passwd)
s.sendmail(sender,receivers,msg.as_string())
print('Send Succese') except:
print('Send Failure')
发送带附件的邮件:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header sender = 'xxxxxx@qq.com' #发件邮箱
passwd = 'lkugmgywydhfff' # 邮箱授权码
receivers = 'xxxxxx@qq.com' #收件邮箱 subject = 'python发带附件的邮件测试' #主题
# 创建一个带附件的实例
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receivers #创建正文,将文本文件添加到MIMEMultipart中
msg.attach(MIMEText('使用python smtplib模块和email模块自动发送邮件测试','plain','utf-8')) #构造附件1,传送当前目录下 文件
att1 = MIMEText(open('testdata.xlsx','rb').read(),'base64','utf-8') # rb以二进制方式读取
# att1["Content-Type"] = 'application/octet-stream'
# filename为附件名称,可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename = "testdata.xlsx" '
#将附件添加到MIMEMultipart中
msg.attach(att1) #构造附件2
att2 = MIMEText(open('db.cfg','rb').read(),'base64','utf-8')
# att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename = "db.cfg" '
#将附件添加到MIMEMultipart中
msg.attach(att2) try:
s = smtplib.SMTP_SSL('smtp.qq.com',465)
s.set_debuglevel(1) #输出发送邮件详细过程
s.login(sender,passwd)
s.sendmail(sender,receivers,msg.as_string())
print('Send Succese') except:
print('Send Failure')
Python使用SMTP模块、email模块发送邮件的更多相关文章
- python使用smtplib和email库发送邮件
国内很多服务器提供商都默认禁止了smtp默认的25端口服务,而启用465端口发送邮件 在smtplib库中直接调用SMTP_SSL就是默认使用465端口 示例代码如下: def send_eamil( ...
- 利用Python的smtplib和email发送邮件
原理 网上已经有了很多的教程讲解相关的发送邮件的原理,在这里还是推荐一下廖雪峰老师的Python教程,讲解通俗易懂.简要来说,SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本 ...
- python email ==> send 发送邮件 :) [smtplib, email 模块]
关于Email的预备知识: 原贴地址:http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2343480.html ############ ...
- 使用python调用email模块发送邮件附件
使用python调用email模块实现附件发送 需要模块: import datetime import time import sys import mimetypes import smtplib ...
- Python_使用smtplib和email模块发送邮件
[http://blog.csdn.net/menglei8625/article/details/7721746] SMTP (Simple Mail Transfer Protocol) 邮件传送 ...
- python email模块
python email模块 官方文档 email模块 电子邮件包是一个用于管理电子邮件消息的库.它的特殊设计不用于向SMTP (RFC 2821).NNTP或其他服务器发送任何电子邮件消息;这些是模 ...
- Python 通过 SMTP 发送邮件
Python版本:Python3.5.2 简介 SMTP是发送邮件的协议,Python 内置对 SMTP 的支持,可以发送纯文本邮件.HTML 邮件以及带附件的邮件. Python 对 SMTP 支持 ...
- python学习笔记8-邮件模块
我们在开发程序的时候,有时候需要开发一些自动化的任务,执行完之后,将结果自动的发送一份邮件,python发送邮件使用smtplib模块,是一个标准包,直接import导入使用即可,代码如下: impo ...
- python实现邮件接口——smtplib模块
1. 思路 使用脚本发送邮件的思路其实和客户端发送邮件一样,过程都是: 登录 —> 写邮件 —> 发送 只不过通过脚本发送时我们需要考虑到整个过程的方方面面.以下为思路导图: 2. Pyt ...
随机推荐
- pache—DBUtils框架简介、DbUtils类、QueryRunner类 、ResultSetHandler接口
Apache—DBUtils框架简介.DbUtils类.QueryRunner类 .ResultSetHandler接口 commons-dbutils 是 Apache 组织提供的一个开源 JDBC ...
- eclipse 中英文切换
第一种方法: 在Eclipse安装目录下找到它的配置文件"eclipse.ini",用UE或者EditPlus等工具打开该配置文件,截图显示如下: 然后在最后一行添加如下相应命令: ...
- Avoid nesting too deeply and return early避免嵌套太深应尽早返回
当某个变量与多个值进行比较的时候 不要用多个if else 判断是否相等 将多个值放在数组里,然后用PHP函数in_array(mixed $needle,array $haystack),检查数组$ ...
- Mac的搜狗输入法和QQ输入法加入⌘⌥⌃⇧自定义短语
搜狗输入法(Mac):http://pinyin.sogou.com/mac/ 创建名为『搜狗输入法自定义短语.ini』的文本文件(建议用Sublime Text),内容如下,然后偏好设置的自定义短语 ...
- atom总结
window 系统 //查找 apm search emmet //安装 apm install emmet //删除 apm remove emmet
- CATransform3D 矩阵变换之立方体旋转实现细节 (转)
原文地址 http://blog.csdn.net/ch_soft/article/details/7351896 第一部分.前几天做动画,使用到了CATransform3D ,由于没有学过计算机图形 ...
- http请求 详解
- [BZOJ5249][多省联测2018]IIIDX
bzoj luogu sol 首先可以把依赖关系转成一个森林.自下而上维护出每个点的\(size\),表示这关解锁以后一共有多少关. 考虑没有重复数字的情况. 直接从小往大贪心把每个数赋给当前已解锁的 ...
- P1230 智力大冲浪(洛谷)
题目描述 小伟报名参加中央电视台的智力大冲浪节目.本次挑战赛吸引了众多参赛者,主持人为了表彰大家的勇气,先奖励每个参赛者m元.先不要太高兴!因为这些钱还不一定都是你的?!接下来主持人宣布了比赛规则: ...
- Thinkphp 自定义404页面
一. 手册->调试->异常处理 在公共config.php 中加入: 'TMPL_EXCEPTION_FILE' => '/Public/404.html', //访问不存在的跳转 ...