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,才是你自己的邮箱。

这段程序测试是通不过的,不过下面这个程序貌似更好.

  1. #!/usr/bin/env python
  2. # coding:utf-8
  3. import email
  4. import smtplib
  5. class Mailer:
  6. def __init__(self, smtp_host, smtp_user, smtp_passwd, smtp_port = 25) :
  7. self.smtp_host = smtp_host
  8. self.smtp_user = smtp_user
  9. self.smtp_passwd = smtp_passwd
  10. self.smtp_port = smtp_port
  11. self.mail = email.MIMEMultipart.MIMEMultipart('related')
  12. self.alter = email.MIMEMultipart.MIMEMultipart('alternative')
  13. self.mail.attach(self.alter)
  14. self.attachments = []
  15. def mailfrom(self, mail_from) :
  16. self._from = mail_from
  17. self.mail['from'] = mail_from
  18. def mailto(self, mail_to) :
  19. """
  20. mail_to : comma separated emails
  21. """
  22. self._to = mail_to
  23. if type(mail_to) == list:
  24. self.mail['to'] = ','.join(mail_to)
  25. elif type(mail_to) == str :
  26. self.mail['to'] = mail_to
  27. else :
  28. raise Exception('invalid mail to')
  29. def mailsubject(self, mail_subject) :
  30. self.mail['subject'] = mail_subject
  31. def text_body(self, body, encoding = 'utf-8') :
  32. self.alter.attach(email.MIMEText.MIMEText(body, 'plain', encoding))
  33. def html_body(self, body, encoding = 'utf-8') :
  34. self.alter.attach(email.MIMEText.MIMEText(body, 'html', encoding))
  35. def addattach(self, filepath, mime_type = 'octect-stream', rename = None) :
  36. import os
  37. f = open(filepath, 'rb')
  38. filecontent = f.read()
  39. f.close()
  40. mb = email.MIMEBase.MIMEBase('application', mime_type)
  41. mb.set_payload(filecontent)
  42. email.Encoders.encode_base64(mb)
  43. fn = os.path.basename(filepath)
  44. mb.add_header('Content-Disposition', 'attachment', filename = rename or fn)
  45. self.mail.attach(mb)
  46. def send(self):
  47. self.mail['Date'] = email.Utils.formatdate( )
  48. smtp = False
  49. try:
  50. smtp = smtplib.SMTP()
  51. smtp.set_debuglevel(1)
  52. smtp.connect(self.smtp_host, self.smtp_port)
  53. smtp.login(self.smtp_user, self.smtp_passwd)
  54. smtp.sendmail(self._from, self._to, self.mail.as_string())
  55. return  True
  56. except Exception, e:
  57. import traceback
  58. print traceback.format_exc()
  59. return False
  60. #finally :
  61. smtp and smtp.quit()
  62. def get_mailer() :
  63. mailer = Mailer(, , )#smtp,user,pwd
  64. mailer.mailfrom('')#from email
  65. return mailer
  66. if __name__ == '__main__':
  67. mailer = get_mailer()
  68. import sys
  69. #mailto = sys.argv[1]
  70. #subject = sys.argv[2]
  71. #body = sys.stdin.read()
  72. #mailer.mailto(mailto)
  73. #mailer.mailsubject(subject)
  74. #mailer.html_body(body)
  75. #if len(sys.argv) == 4 :
  76. #    mailer.addattach(sys.argv[3])
  77. #mailer.send()
  78. mailto = 'so@gmail.com'
  79. subject = 'Daily Report'
  80. body = 'Hello'
  81. mailer.mailto(mailto)
  82. mailer.mailsubject(subject)
  83. mailer.html_body(body)
  84. #if len(sys.argv) == 4 :
  85. mailer.addattach('318110214911a041c0ed077f0.xls')
  86. 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_PLAIN = "PLAIN"  
  1. AUTH_CRAM_MD5 = "CRAM-MD5"
  2. AUTH_LOGIN = "LOGIN"
  3. if self.helo_resp is None and self.ehlo_resp is None:
  4. if not (200 <= self.ehlo()[0] <= 299):
  5. (code, resp) = self.helo()
  6. if not (200 <= code <= 299):
  7. raise SMTPHeloError(code, resp)
  8. if not self.has_extn("auth"):
  9. raise SMTPException("SMTP AUTH extension not supported by server.")
  10. # Authentication methods the server supports:
  11. authlist = self.esmtp_features["auth"].split()
  12. # List of authentication methods we support: from preferred to
  13. # less preferred methods. Except for the purpose of testing the weaker
  14. # ones, we prefer stronger methods like CRAM-MD5:
  15. #preferred_auths = [AUTH_CRAM_MD5, AUTH_PLAIN]
  16. preferred_auths = [AUTH_CRAM_MD5, AUTH_PLAIN, AUTH_LOGIN]# add LOGIN
  17. #preferred_auths = [AUTH_PLAIN, AUTH_CRAM_MD5]
  18. # Determine the authentication method we'll use
  19. authmethod = None
  20. for method in preferred_auths:
  21. if method in authlist:
  22. authmethod = method
  23. break
  24. [color=red]            if '='+method in authlist:
  25. authmethod = method
  26. break # 本来这个部分不用加,但是我那个邮件服务器返回的是 auth=LOGIN,多了一个'=',所以我就加了这一段[/color]
  27. if self.debuglevel > 0:
  28. print "AuthMethod:", authmethod
  29. if authmethod == AUTH_CRAM_MD5:
  30. (code, resp) = self.docmd("AUTH", AUTH_CRAM_MD5)
  31. if code == 503:
  32. # 503 == 'Error: already authenticated'
  33. return (code, resp)
  34. (code, resp) = self.docmd(encode_cram_md5(resp, user, password))
  35. elif authmethod == AUTH_PLAIN:
  36. (code, resp) = self.docmd("AUTH",
  37. AUTH_PLAIN + " " + encode_plain(user, password))
  38. # 主要就是下面这一段了
  39. [color=red]       elif authmethod == AUTH_LOGIN:
  40. (code, resp) = self.docmd("AUTH", AUTH_LOGIN)
  41. if code == 334:
  42. (code, resp) = self.docmd(base64.encodestring(user)[:-1])
  43. if code == 334:
  44. (code, resp) = self.docmd(base64.encodestring(password)[:-1])   [/color]
  45. elif authmethod == None:
  46. raise SMTPException("No suitable authentication method found.")
  47. if code not in [235, 503]:
  48. # 235 == 'Authentication successful'
  49. # 503 == 'Error: already authenticated'
  50. raise SMTPAuthenticationError(code, resp)
  51. 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库发送邮件相关推荐

  1. Python 使用SMTP协议发送邮件

    引言 问题基于<计算机网络自定向下>第二章的课后套接字编程作业:邮件客户 题目的下载链接:python 压缩包 题目如下: 这个实验结束时,您将能够更好地了解SMTP协议.您还将获得使用P ...

  2. python应用系列教程——python使用smtp协议发送邮件:html文本邮件、图片邮件、文件附件邮件

    全栈工程师开发手册 (作者:栾鹏) python教程全解 python使用smtp协议发送电子邮件.包含发送html文本邮件.包含图片附件的邮件,包含其他文件附件的邮件.可设置邮件的收发人,主题,内容 ...

  3. Python用SMTP自动发送邮件email

    发送邮件是一个常见的需求,一般会采用SMTP代理的形式,进行邮件自动发送 在发送邮件时,主要有如下准备工作: ① 发件人.收件人.抄送人 ② 发件人邮箱授权码 ③ 邮件主题 ④ 邮件正文 ⑤ 附件 发 ...

  4. python通过代理发送邮件_使用Python通过SMTP发送邮件

    有些业务可能由于各种各样的原因并不适用于Zabbix监控,这时如果要做到系统出问题能立即发送邮件,就需要自己来写监控脚本了,出问题要实时通过邮件报警,以下案例使用Python脚本实现通过SMTP协议发 ...

  5. python 实现SMTP发送邮件(四)-添加附件

    Python实现SMTP发送邮件(四)-添加附件 [Python]  纯文本查看  复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 ...

  6. 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 ...

  7. 使用 Python+Selenium + 第三方库实现的简单的 web 自动化测试框架 源码

    使用 Python+Selenium + 第三方库实现简单的 web 自动化测试框架,为 web 自动化测试编写更加便利和可维护. 一.配置(config) 1.1 说明 设置自动化案例运行时的属性值 ...

  8. python的第三方库汇总

    Python 资源大全中文版 环境管理 管理 Python 版本和环境的工具 p:非常简单的交互式 python 版本管理工具.官网 pyenv:简单的 Python 版本管理工具.官网 Vex:可以 ...

  9. python web 常用库

    # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.[awesome-python](https://github.com/v ...

最新文章

  1. Android 使用git 忽略文件
  2. 飞机的“黑色十分钟”能被人工智能消灭吗?
  3. tarjan详解(转)
  4. 活动目录数据库授权恢复
  5. ES6-symbol-创建symbol
  6. 个人的中小型项目前端架构浅谈(转)
  7. 代码健壮性 —— 异常保护和合法性检测
  8. RocketMQ安装使用
  9. Java 程序员常用资源工具集合(建议收藏)
  10. 计算机博士专业学位,计算机博士
  11. 在移动硬盘上装双系统(一个Ventory+微PE,一个Win10家庭版)
  12. RFQ 、IFB、RFP 、RFI的区别是什么
  13. input输入框只能输入字母
  14. Badboy入门操作手册
  15. cpu win10 安装yolo_yolov5 win10 数据集制作 各种踩坑
  16. DB2 Merge 语句的使用
  17. 矩阵的初等变换的应用
  18. 魔域mysql下载_魔域3.2无敌版之富甲天下下载_魔域3.2无敌版下载_快吧单机游戏...
  19. jdk6(Jdk6 tls)
  20. python实现淘宝秒杀_python实现简单淘宝秒杀功能

热门文章

  1. 如何划分大中小型项目?
  2. adb interface找不到驱动程序Android Studio (学习记录)
  3. 计算机、软件工程、信息工程专业,哪个适合女生?
  4. 装货单Shipping Order
  5. 触景无限“点睛计划”免费帮助传统企业完成智能化升级
  6. 大神之路-起始篇 | 第15章.计算机科学导论之【数据压缩】学习笔记
  7. ChatGPT来了,英语不能丢,但我不想上班
  8. mac ios pods 使用小结
  9. SpaceX 都发射火箭升空了,你还没做小程序吗?
  10. html语音输入功能讯飞,讯飞输入法电脑版语音输入功能怎么使用?