python使用smtp库发送邮件
python smtp发邮件
开发一个日志扫描工具,用于扫描报错日志,并打包成邮件,发送到邮箱。
例子:
#!/usr/bin/env python # coding=utf-8 from smtplib import SMTP from email import MIMEText from email import Header from os.path import getsize from sys import exit from re import compile, IGNORECASE #定义主机 帐号 密码 收件人 邮件主题 smtpserver = '10.0.10.191' user = 'cxyblog' password = '*******' sender = 'cxyblog@163.com' receiver = ('xiaozi@163.com','收件人',) subject = u'Web服务器Tomcat日志错误信息' From = u'xxx Web服务器' To = u'服务器管理员' #发送邮件函数 def send_mail(error): #定义邮件的头部信息 header = Header.Header msg = MIMEText.MIMEText(error,'plain','utf-8') msg['From'] = header(From) msg['To'] = header(To) msg['Subject'] = header(subject+' ') #连接SMTP服务器,然后发送信息 smtp = SMTP(smtpserver) smtp.login(user, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.close() send_mail("hi hello")
代码使用说明:
1,user和password在调用163等邮箱时,user是自己的邮箱,senser也是自己的邮箱。
2,在公司内部邮件系统中,这个login的user,可能是其他的账户。sender,才是你自己的邮箱。
这段程序测试是通不过的,不过下面这个程序貌似更好.
- #!/usr/bin/env python
- # coding:utf-8
- import email
- import smtplib
- class Mailer:
- def __init__(self, smtp_host, smtp_user, smtp_passwd, smtp_port = 25) :
- self.smtp_host = smtp_host
- self.smtp_user = smtp_user
- self.smtp_passwd = smtp_passwd
- self.smtp_port = smtp_port
- self.mail = email.MIMEMultipart.MIMEMultipart('related')
- self.alter = email.MIMEMultipart.MIMEMultipart('alternative')
- self.mail.attach(self.alter)
- self.attachments = []
- def mailfrom(self, mail_from) :
- self._from = mail_from
- self.mail['from'] = mail_from
- def mailto(self, mail_to) :
- """
- mail_to : comma separated emails
- """
- self._to = mail_to
- if type(mail_to) == list:
- self.mail['to'] = ','.join(mail_to)
- elif type(mail_to) == str :
- self.mail['to'] = mail_to
- else :
- raise Exception('invalid mail to')
- def mailsubject(self, mail_subject) :
- self.mail['subject'] = mail_subject
- def text_body(self, body, encoding = 'utf-8') :
- self.alter.attach(email.MIMEText.MIMEText(body, 'plain', encoding))
- def html_body(self, body, encoding = 'utf-8') :
- self.alter.attach(email.MIMEText.MIMEText(body, 'html', encoding))
- def addattach(self, filepath, mime_type = 'octect-stream', rename = None) :
- import os
- f = open(filepath, 'rb')
- filecontent = f.read()
- f.close()
- mb = email.MIMEBase.MIMEBase('application', mime_type)
- mb.set_payload(filecontent)
- email.Encoders.encode_base64(mb)
- fn = os.path.basename(filepath)
- mb.add_header('Content-Disposition', 'attachment', filename = rename or fn)
- self.mail.attach(mb)
- def send(self):
- self.mail['Date'] = email.Utils.formatdate( )
- smtp = False
- try:
- smtp = smtplib.SMTP()
- smtp.set_debuglevel(1)
- smtp.connect(self.smtp_host, self.smtp_port)
- smtp.login(self.smtp_user, self.smtp_passwd)
- smtp.sendmail(self._from, self._to, self.mail.as_string())
- return True
- except Exception, e:
- import traceback
- print traceback.format_exc()
- return False
- #finally :
- smtp and smtp.quit()
- def get_mailer() :
- mailer = Mailer(, , )#smtp,user,pwd
- mailer.mailfrom('')#from email
- return mailer
- if __name__ == '__main__':
- mailer = get_mailer()
- import sys
- #mailto = sys.argv[1]
- #subject = sys.argv[2]
- #body = sys.stdin.read()
- #mailer.mailto(mailto)
- #mailer.mailsubject(subject)
- #mailer.html_body(body)
- #if len(sys.argv) == 4 :
- # mailer.addattach(sys.argv[3])
- #mailer.send()
- mailto = 'so@gmail.com'
- subject = 'Daily Report'
- body = 'Hello'
- mailer.mailto(mailto)
- mailer.mailsubject(subject)
- mailer.html_body(body)
- #if len(sys.argv) == 4 :
- mailer.addattach('318110214911a041c0ed077f0.xls')
- mailer.send()
#!/usr/bin/env python
# coding:utf-8
import email
import smtplib
class Mailer:
def __init__(self, smtp_host, smtp_user, smtp_passwd, smtp_port = 25) :
self.smtp_host = smtp_host
self.smtp_user = smtp_user
self.smtp_passwd = smtp_passwd
self.smtp_port = smtp_port
self.mail = email.MIMEMultipart.MIMEMultipart('related')
self.alter = email.MIMEMultipart.MIMEMultipart('alternative')
self.mail.attach(self.alter)
self.attachments = []
def mailfrom(self, mail_from) :
self._from = mail_from
self.mail['from'] = mail_from
def mailto(self, mail_to) :
"""
mail_to : comma separated emails
"""
self._to = mail_to
if type(mail_to) == list:
self.mail['to'] = ','.join(mail_to)
elif type(mail_to) == str :
self.mail['to'] = mail_to
else :
raise Exception('invalid mail to')
def mailsubject(self, mail_subject) :
self.mail['subject'] = mail_subject
def text_body(self, body, encoding = 'utf-8') :
self.alter.attach(email.MIMEText.MIMEText(body, 'plain', encoding))
def html_body(self, body, encoding = 'utf-8') :
self.alter.attach(email.MIMEText.MIMEText(body, 'html', encoding))
def addattach(self, filepath, mime_type = 'octect-stream', rename = None) :
import os
f = open(filepath, 'rb')
filecontent = f.read()
f.close()
mb = email.MIMEBase.MIMEBase('application', mime_type)
mb.set_payload(filecontent)
email.Encoders.encode_base64(mb)
fn = os.path.basename(filepath)
mb.add_header('Content-Disposition', 'attachment', filename = rename or fn)
self.mail.attach(mb)
def send(self):
self.mail['Date'] = email.Utils.formatdate( )
smtp = False
try:
smtp = smtplib.SMTP()
smtp.set_debuglevel(1)
smtp.connect(self.smtp_host, self.smtp_port)
smtp.login(self.smtp_user, self.smtp_passwd)
smtp.sendmail(self._from, self._to, self.mail.as_string())
return True
except Exception, e:
import traceback
print traceback.format_exc()
return False
#finally :
smtp and smtp.quit()
def get_mailer() :
mailer = Mailer(, , )#smtp,user,pwd
mailer.mailfrom('')#from email
return mailer
if __name__ == '__main__':
mailer = get_mailer()
import sys
#mailto = sys.argv[1]
#subject = sys.argv[2]
#body = sys.stdin.read()
#mailer.mailto(mailto)
#mailer.mailsubject(subject)
#mailer.html_body(body)
#if len(sys.argv) == 4 :
# mailer.addattach(sys.argv[3])
#mailer.send()
mailto = 'so@gmail.com'
subject = 'Daily Report'
body = 'Hello'
mailer.mailto(mailto)
mailer.mailsubject(subject)
mailer.html_body(body)
#if len(sys.argv) == 4 :
mailer.addattach('318110214911a041c0ed077f0.xls')
mailer.send()
这部分程序在发送向126,gmail的时候是成功的。但是公司连接的是国外的VPN,再连接的smtp
一下是debug的内容
connect: (************, 25)-----hostname
connect: (***********, 25)------ip
reply: '220 *********************************************************\r\n'
reply: retcode (220); Msg: *********************************************************
connect: *********************************************************
send: 'ehlo [**********]\r\n'
reply: '250-************ Hello [*********]\r\n'
reply: '250-SIZE 29360128\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-DSN\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-XXXXXXXA\r\n'
reply: '250-AUTH\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-BINARYMIME\r\n'
reply: '250 XXXXXXXB\r\n'
reply: retcode (250); Msg: ************************
SIZE 29360128
PIPELINING
DSN
ENHANCEDSTATUSCODES
XXXXXXXA
AUTH
8BITMIME
BINARYMIME
XXXXXXXB
Traceback (most recent call last):
File "C:\Users\Desktop\Send_6lisa.py", line 53, in send
smtp.login(self.smtp_user, self.smtp_passwd)
File "F:\Python25\lib\smtplib.py", line 587, in login
if code == 334:
SMTPException: No suitable authentication method found.
参考网址:http://u.cxyblog.com/14/article-aid-304.html
按照网上说的方法对smtplib.py进行了修改,如下
- AUTH_CRAM_MD5 = "CRAM-MD5"
- AUTH_LOGIN = "LOGIN"
- if self.helo_resp is None and self.ehlo_resp is None:
- if not (200 <= self.ehlo()[0] <= 299):
- (code, resp) = self.helo()
- if not (200 <= code <= 299):
- raise SMTPHeloError(code, resp)
- if not self.has_extn("auth"):
- raise SMTPException("SMTP AUTH extension not supported by server.")
- # Authentication methods the server supports:
- authlist = self.esmtp_features["auth"].split()
- # List of authentication methods we support: from preferred to
- # less preferred methods. Except for the purpose of testing the weaker
- # ones, we prefer stronger methods like CRAM-MD5:
- #preferred_auths = [AUTH_CRAM_MD5, AUTH_PLAIN]
- preferred_auths = [AUTH_CRAM_MD5, AUTH_PLAIN, AUTH_LOGIN]# add LOGIN
- #preferred_auths = [AUTH_PLAIN, AUTH_CRAM_MD5]
- # Determine the authentication method we'll use
- authmethod = None
- for method in preferred_auths:
- if method in authlist:
- authmethod = method
- break
- [color=red] if '='+method in authlist:
- authmethod = method
- break # 本来这个部分不用加,但是我那个邮件服务器返回的是 auth=LOGIN,多了一个'=',所以我就加了这一段[/color]
- if self.debuglevel > 0:
- print "AuthMethod:", authmethod
- if authmethod == AUTH_CRAM_MD5:
- (code, resp) = self.docmd("AUTH", AUTH_CRAM_MD5)
- if code == 503:
- # 503 == 'Error: already authenticated'
- return (code, resp)
- (code, resp) = self.docmd(encode_cram_md5(resp, user, password))
- elif authmethod == AUTH_PLAIN:
- (code, resp) = self.docmd("AUTH",
- AUTH_PLAIN + " " + encode_plain(user, password))
- # 主要就是下面这一段了
- [color=red] elif authmethod == AUTH_LOGIN:
- (code, resp) = self.docmd("AUTH", AUTH_LOGIN)
- if code == 334:
- (code, resp) = self.docmd(base64.encodestring(user)[:-1])
- if code == 334:
- (code, resp) = self.docmd(base64.encodestring(password)[:-1]) [/color]
- elif authmethod == None:
- raise SMTPException("No suitable authentication method found.")
- if code not in [235, 503]:
- # 235 == 'Authentication successful'
- # 503 == 'Error: already authenticated'
- raise SMTPAuthenticationError(code, resp)
- return (code, resp)
AUTH_PLAIN = "PLAIN"
AUTH_CRAM_MD5 = "CRAM-MD5"
AUTH_LOGIN = "LOGIN"
if self.helo_resp is None and self.ehlo_resp is None:
if not (200 <= self.ehlo()[0] <= 299):
(code, resp) = self.helo()
if not (200 <= code <= 299):
raise SMTPHeloError(code, resp)
if not self.has_extn("auth"):
raise SMTPException("SMTP AUTH extension not supported by server.")
# Authentication methods the server supports:
authlist = self.esmtp_features["auth"].split()
# List of authentication methods we support: from preferred to
# less preferred methods. Except for the purpose of testing the weaker
# ones, we prefer stronger methods like CRAM-MD5:
#preferred_auths = [AUTH_CRAM_MD5, AUTH_PLAIN]
preferred_auths = [AUTH_CRAM_MD5, AUTH_PLAIN, AUTH_LOGIN]# add LOGIN
#preferred_auths = [AUTH_PLAIN, AUTH_CRAM_MD5]
# Determine the authentication method we'll use
authmethod = None
for method in preferred_auths:
if method in authlist:
authmethod = method
break
[color=red] if '='+method in authlist:
authmethod = method
break # 本来这个部分不用加,但是我那个邮件服务器返回的是 auth=LOGIN,多了一个'=',所以我就加了这一段[/color]
if self.debuglevel > 0:
print "AuthMethod:", authmethod
if authmethod == AUTH_CRAM_MD5:
(code, resp) = self.docmd("AUTH", AUTH_CRAM_MD5)
if code == 503:
# 503 == 'Error: already authenticated'
return (code, resp)
(code, resp) = self.docmd(encode_cram_md5(resp, user, password))
elif authmethod == AUTH_PLAIN:
(code, resp) = self.docmd("AUTH",
AUTH_PLAIN + " " + encode_plain(user, password))
# 主要就是下面这一段了
[color=red] elif authmethod == AUTH_LOGIN:
(code, resp) = self.docmd("AUTH", AUTH_LOGIN)
if code == 334:
(code, resp) = self.docmd(base64.encodestring(user)[:-1])
if code == 334:
(code, resp) = self.docmd(base64.encodestring(password)[:-1]) [/color]
elif authmethod == None:
raise SMTPException("No suitable authentication method found.")
if code not in [235, 503]:
# 235 == 'Authentication successful'
# 503 == 'Error: already authenticated'
raise SMTPAuthenticationError(code, resp)
return (code, resp)
但是还是报的同样的错误。我同时也把发成功的debug打印的出来,发现是在AUTH的时候gmail,和126都会有一个AUTH_type=LOGIN
但是国外的邮件server却返回的是NONE
我就是不清楚这个auth_type在server端是怎么配置的,还有什么其他的方法可以发邮件的吗?谢谢!
参考网址:http://www.iteye.com/problems/50919
python使用smtp库发送邮件相关推荐
- Python 使用SMTP协议发送邮件
引言 问题基于<计算机网络自定向下>第二章的课后套接字编程作业:邮件客户 题目的下载链接:python 压缩包 题目如下: 这个实验结束时,您将能够更好地了解SMTP协议.您还将获得使用P ...
- python应用系列教程——python使用smtp协议发送邮件:html文本邮件、图片邮件、文件附件邮件
全栈工程师开发手册 (作者:栾鹏) python教程全解 python使用smtp协议发送电子邮件.包含发送html文本邮件.包含图片附件的邮件,包含其他文件附件的邮件.可设置邮件的收发人,主题,内容 ...
- Python用SMTP自动发送邮件email
发送邮件是一个常见的需求,一般会采用SMTP代理的形式,进行邮件自动发送 在发送邮件时,主要有如下准备工作: ① 发件人.收件人.抄送人 ② 发件人邮箱授权码 ③ 邮件主题 ④ 邮件正文 ⑤ 附件 发 ...
- python通过代理发送邮件_使用Python通过SMTP发送邮件
有些业务可能由于各种各样的原因并不适用于Zabbix监控,这时如果要做到系统出问题能立即发送邮件,就需要自己来写监控脚本了,出问题要实时通过邮件报警,以下案例使用Python脚本实现通过SMTP协议发 ...
- python 实现SMTP发送邮件(四)-添加附件
Python实现SMTP发送邮件(四)-添加附件 [Python] 纯文本查看 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 ...
- python smtplib.SMTP()发送邮件报“UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xba in position 0: i
python smtplib.SMTP()发送邮件报"UnicodeDecodeError: 'utf-8' codec can't decode byte 0xba in position ...
- 使用 Python+Selenium + 第三方库实现的简单的 web 自动化测试框架 源码
使用 Python+Selenium + 第三方库实现简单的 web 自动化测试框架,为 web 自动化测试编写更加便利和可维护. 一.配置(config) 1.1 说明 设置自动化案例运行时的属性值 ...
- python的第三方库汇总
Python 资源大全中文版 环境管理 管理 Python 版本和环境的工具 p:非常简单的交互式 python 版本管理工具.官网 pyenv:简单的 Python 版本管理工具.官网 Vex:可以 ...
- python web 常用库
# Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.[awesome-python](https://github.com/v ...
最新文章
- Android 使用git 忽略文件
- 飞机的“黑色十分钟”能被人工智能消灭吗?
- tarjan详解(转)
- 活动目录数据库授权恢复
- ES6-symbol-创建symbol
- 个人的中小型项目前端架构浅谈(转)
- 代码健壮性 —— 异常保护和合法性检测
- RocketMQ安装使用
- Java 程序员常用资源工具集合(建议收藏)
- 计算机博士专业学位,计算机博士
- 在移动硬盘上装双系统(一个Ventory+微PE,一个Win10家庭版)
- RFQ 、IFB、RFP 、RFI的区别是什么
- input输入框只能输入字母
- Badboy入门操作手册
- cpu win10 安装yolo_yolov5 win10 数据集制作 各种踩坑
- DB2 Merge 语句的使用
- 矩阵的初等变换的应用
- 魔域mysql下载_魔域3.2无敌版之富甲天下下载_魔域3.2无敌版下载_快吧单机游戏...
- jdk6(Jdk6 tls)
- python实现淘宝秒杀_python实现简单淘宝秒杀功能
热门文章
- 如何划分大中小型项目?
- adb interface找不到驱动程序Android Studio (学习记录)
- 计算机、软件工程、信息工程专业,哪个适合女生?
- 装货单Shipping Order
- 触景无限“点睛计划”免费帮助传统企业完成智能化升级
- 大神之路-起始篇 | 第15章.计算机科学导论之【数据压缩】学习笔记
- ChatGPT来了,英语不能丢,但我不想上班
- mac ios pods 使用小结
- SpaceX 都发射火箭升空了,你还没做小程序吗?
- html语音输入功能讯飞,讯飞输入法电脑版语音输入功能怎么使用?