python使用smtplib模块发送电子邮件

模式示例:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import smtplib
import string

HOST="smtp.gmail.com"
SUBJECT="this is a test"
TO="[email protected]"
FROM="[email protected]"
text="let us try to get python mail"
BODY=string.join((
	"From:%s" % FROM,
	"To:%s" % TO,
	"Subject:%s" % SUBJECT,
	"-------------------------------------------",
	"-------------------------------------------",
	text
	),"\r\n")
server=smtplib.SMTP()
server.connect(HOST,"25")
server.starttls()
server.login(FROM,"mypasswd")
server.sendmail(FROM,[TO],BODY)
server.quit()

示例1:带有html格式的报表邮件

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import smtplib
from email.mime.text import MIMEText

HOST="smtp.163.com"
FROM="[email protected]"
TO="[email protected]"
SUBJECT="数据报表详情"
msg=MIMEText("""
html 内容

"""
)

msg['Subject']=SUBJECT
msg['From']=FROM
msg['To']=TO
try:
	server=smtplib.SMTP()
	server.connect(HOST,"25")
	server.starttls()
	server.login(FROM,"password")
	server.sendmail(FROM,TO,msg.as_string())
	server.quit()
	print "邮件已发送"
except Exception,e:
	print "失败:"+str(e)

-------------------------------------------------

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

HOST="smtp.qq.com"
FROM="[email protected]"
TO="[email protected]"
SUBJECT="服务器数据报表"

def adding(src,imgid):
	fp=open(src,'rb')
	msgImage=MIMEImage(fp.read())
	fp.close()
	msgImage.add_header('Content-ID',imgid)
	return msgImage

msg=MIMEMultipart('related')
msgtext=MIMEText("""

""")
msg.attach(msgtext)
msg.attach(adding("/data/image/test.png","io"))
msg.attach(adding("/data/image/good.png","disk"))
msg.attach(adding("/data/image/try.png","cpu"))
msg.attach(adding("/data/image/mem.png","mem"))
msg['Subject']=SUBJECT
msg['From']=FROM
msg['To']=TO

try:
	server=smtplib.SMTP()
	server.connect(HOST,"25")
	server.starttls()
	server.login(FROM,"password")
	server.sendmail(FROM,TO,msg.as_string())
	server.quit()
	print "邮件已发送"
except Exception,e:
	print "失败:"+str(e)

--------------------------------------------------

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

HOST="smtp.qq.com"
FROM="[email protected]"
TO="[email protected]"
SUBJECT="业务月报信息"

def adding(src,imgid):
	fp=open(src,'rb')
	msgImage=MIMEImage(fp.read())
	fp.close()
	msgImage.add_header('Content-ID',imgid)
	return msgImage

msg=MIMEMultipart('related')
msgtext=MIMEText("<font color=red> 业务报表:<br><img src=\"cid:yuebao\"border=\"1\"></br>详见附件  </font>","html","utf-8")
msg.attach(msgtext)
msg.attach(adding("/data/image/yuebao.png","yuebao"))

attach=MIMEText(open("/data/yuebao.xlsx","rb").read(),"base64","utf-8")
attach["Content-Type"]="application/octet-stream"
attach["Content-Disposition"]="attachment;filename=\"月报.xlsx\"
#attach["Content-Disposition"]="attachment;filename=\"月报.xlsx\"".decode("utf-8").encode("gb18030")
msg.attach(attach)
msg['Subject']=SUBJECT
msg['From']=FROM
msg['To']=TO

try:
	server=smtplib.SMTP()
	server.connect(HOST,"25")
	server.starttls()
	server.login(FROM,"password")
	server.sendmail(FROM,TO,msg.as_string())
	server.quit()
	print "邮件已发送"
except Exception,e:
	print "失败:"+str(e)

注意:采用SMTP协议,那么需要确保自己的发送邮箱地址开启了SMTP服务

点击开启后,生产授权码,上面示例代码中的password 就是授权码,不是自己的密码

原文地址:http://blog.51cto.com/superleedo/2116645

时间: 2024-08-28 12:40:53

python使用smtplib模块发送电子邮件的相关文章

在Python中使用SMTP发送电子邮件

Python中有内置的smtplib模块,完成一封邮件的发送,需要做很多准备工作.第三方的py_smtp这个包也是基于smtplib的,在Python中使用py_smtp发送电子邮件非常方便,只要填写发送邮件的相关信息即可. 一.安装模块 pip install py-smtp 二.发送邮件 from py_smtp import send send('smtp服务器', 465,['发送人昵称', '发送人邮箱地址'], '密码', ['收件人1','收件人2','收件人3'], ['抄送人1

python爬虫-smtplib模块发送邮件

1.代码如下: import smtplib from email.message from EmailMessage # smtplib模块负责发送邮件服务 # email.message模块负责构建邮件,然后交给smtplib发送 # 定义SMTP服务器地址 smtp_server = 'smtp.163.com' # 定义发件人地址 from_addr = "***********@163.com" # 定义登录密码 password = '**********' # 定义收件人

python之smtplib模块 发送邮件

# -*- coding: utf-8 -*- #python 27 #xiaodeng #smtplib模块 发送邮件 import smtplib from email.mime.text import MIMEText ''' http://www.cnblogs.com/xiaowuyi/archive/2012/03/17/2404015.html #基本思路: 1.构造发送邮件的主程序,创建发邮件的对象,链接服务器.登录服务器.发送邮件命令行.关闭服务器 2.在主程序中为了便于错误分

python学习-smtplib模块

python的stmplib模块可以实现邮件的发送功能,可以模拟一个smtp客户端.在python2.3或者更高版本默认自带smtplib模块,无需额外安装. 一.smtplibi模块的常用类与方法 smtp类定义:smtplib([host[,port[,local_hostname[,timeout]]]),作为smtp的构造函数,功能是与smtp服务器建立连接,在连接成功后,就可以向服务器发送相关请求,比如登录.校验.发送.退出等.host参数为远程smtp主机地址,比如smtp.163.

Python通过smtp服务发送电子邮件给指定用户(适用于Zabbix邮件报警)

当下免费的邮件服务很多,例如163企业邮箱.QQ企业邮箱等.不需要自己搭建邮件服务器发送邮件给指 定用户,只需要注册任何一个支持smtp协议的邮箱就可以实现发送邮件.发送邮件可以通过Linux命令.自己编写的Shell脚本,也可以通过Python写的Python脚本. 如下代码是一个简单却实用的示例.默认无参数执行时,发送预设的邮件主题和邮件内容到预设的用户.带参数执行时将指定的主题和邮件内容发送到指定的用户.带参数执行可用于Zabbix邮件报警脚本. 对于Zabbix2.x可以直接填写脚本名字

python用httplib模块发送get和post请求

在python中,模拟http客户端发送get和post请求,主要用httplib模块的功能. 1.python发送GET请求 我在本地建立一个测试环境,test.php的内容就是输出一句话: 1 echo 'Old friends and old wines are best.'; python发送get请求代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #!/usr/bin/env python #coding=utf8 i

python调用smtplib模块发送邮件

#!/usr/bin/env python #coding: utf-8 import smtplib from email.mime.text import MIMEText from email.header import Header sender = '[email protected]' #receiver = '[email protected]' receiver = '[email protected]' subject = 'python email test' smtpser

python发送电子邮件模块smtplib

一.简介: 电子邮件是最流行的互联网应用之一,在系统管理中,经常需要使用邮件来告警信息,业务质量报告等.方便运维人员在第一时间了解业务的服务状态,将通过使用python的smtplib模块来实现邮件的发送功能,能模拟一个smtp的客户端,通过与smtp服务器交互来实现邮件的发送功能,可以理解成foxmail的发邮件功能,在第一次使用的时候需要诶只smtp的主机地址,邮箱帐号密码等信息.python 2.4以上的版本默认就自带了smtplib模块,无需额外安装. 二.smtplib模块的常用类与方

Python接口测试-使用requests模块发送post请求

本篇主要记录下使用python的requests模块发送post请求的实现代码. #coding=utf-8 import unittest import requests class PostTest(unittest.TestCase): def setUp(self): host = 'https://httpbin.org/' endpoint = 'post' self.url = ''.join([host, endpoint]) def testPost(self): params