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 ...
随机推荐
- Windows Server 2008 IIS的备份和还原
Windows Server 2008 IIS的备份和还原 当我们服务器系统有大量的站点和虚拟目录的时候,因为种种原因需要重做系统,那么重装系统后这些站点我们是否只能一个一个的添加,如果有成百上千个站 ...
- 异常:没有找到本地方法库,java.lang.UnsatisfiedLinkError: no trsbean in java.library.path
1.问题描述 迁移环境中遇到这个问题 : Fri Apr 20 15:22:31 CST 2018, Exception:500004___-500004,没有找到本地方法库,java.lang.Un ...
- js替换字符串中的数字或非数字
替换字符串中的数字 var text = "abc123"; text=text.replace(/[0-9]/ig,""); 此时得到的text为" ...
- myEclipse 导入maven项目时报错
1.先右击项目,build Path 查看是否是某个JAR出了错.去掉无用的JAR 以及修改对应的JDK版本 2.检查src源文件,查看是否是源文件出了错.发现,还真的是 导入的包上面,报错:The ...
- PowerDesigner生成数据库表和逆向生成表结构(MySQL数据库)
一.Download Connector/ODBC下载ODBC驱动,地址:https://dev.mysql.com/downloads/connector/odbc/, 需要注意:PowerDesi ...
- Python 正则表达式匹配小数
不废话,直接上表达式 (\d+(\.\d+)?) 如: import re find_float = lambda x: re.search("\d+(\.\d+)?",x) .g ...
- iphone动态下拉菜单
介绍:实现带动画效果的下拉菜单.用户按下菜单按钮,出现下拉按钮,用户松开菜单按钮,下拉按钮收回. 测试环境:Xcode 4.3, iOS 5.0. 效果图: jQuery特效:http://www.h ...
- tensorflow笔记:多层CNN代码分析
tensorflow笔记系列: (一) tensorflow笔记:流程,概念和简单代码注释 (二) tensorflow笔记:多层CNN代码分析 (三) tensorflow笔记:多层LSTM代码分析 ...
- 关闭 Windows Defender
关闭 Windows Defender Win+R,输入 gpedit.msc 回车,打开组策略编辑器 展开[计算机设置]-[管理模板]-[Windows 组件]-[Windows Defender] ...
- 慕课网python分布式爬虫打造搜索引擎视频中爬取伯乐网文章
代码:https://github.com/longbigbeard/scrapy_demo