python邮件发送

在基于互联网的应用中,程序经常需要自动地发送电子邮件。如:一个网站的注册系统会在用户注册时发送一封邮件来确认注册;当用户忘记登陆密码的时候,通过邮件来取回密码。smtplib模块是python中smtp(简单邮件传输协议)的客户端实现。我们可以使用smtplib模块,轻松的发送电子邮件。下面的例子用了不到十行代码来发送电子邮件:

  1. #coding=gbk
  2. import smtplib
  3. smtp = smtplib.SMTP()
  4. smtp.connect("smtp.yeah.net", "25")
  5. smtp.login(‘用户名‘, ‘密码‘)
  6. smtp.sendmail(‘[email protected]‘, ‘[email protected]‘, ‘From: [email protected]/r/nTo: [email protected]/r/nSubject: this is a email from python demo/r/n/r/nJust for test~_~‘)
  7. smtp.quit()

这个例子够简单吧^_^!下面详细介绍stmplib模块中的类和方法。

smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])

  SMTP类构造函数,表示与SMTP服务器之间的连接,通过这个连接我们可以向smtp服务器发送指令,执行相关操作(如:登陆、发送邮件)。该类提供了许多方法,将在下面介绍。它的所有参数都是可选的,其中host参数表示smtp服务器主机名,上面例子中的smtp主机为"smtp.yeah.net";port表示smtp服务的端口,默认是25;如果在创建SMTP对象的时候提供了这两个参数,在初始化的时候会自动调用connect方法去连接服务器。
  smtplib模块还提供了SMTP_SSL类和LMTP类,对它们的操作与SMTP基本一致。
  smtplib.SMTP提供的方法:

SMTP.set_debuglevel(level)

  设置是否为调试模式。默认为False,即非调试模式,表示不输出任何调试信息。

SMTP.connect([host[, port]])

  连接到指定的smtp服务器。参数分别表示smpt主机和端口。注意: 也可以在host参数中指定端口号(如:smpt.yeah.net:25),这样就没必要给出port参数。

SMTP.docmd(cmd[, argstring])

  向smtp服务器发送指令。可选参数argstring表示指令的参数。下面的例子完全通过调用docmd方法向服务器发送指令来实现邮件的发送(在smtp.yeah.net邮件服务器上试验通过。其他邮件服务器没有试过):

  1. import smtplib, base64, time
  2. userName = base64.encodestring(‘from‘).strip()
  3. password = base64.encodestring(‘password‘).strip()
  4. smtp = smtplib.SMTP()
  5. smtp.connect("smtp.yeah.net:25")
  6. print smtp.docmd(‘helo‘, ‘from‘)
  7. print smtp.docmd(‘auth login‘)
  8. print smtp.docmd(userName)
  9. print smtp.docmd(password)
  10. print smtp.docmd(‘mail from:‘, ‘<[email protected]>‘)
  11. print smtp.docmd(‘rcpt to:‘, ‘<[email protected]>‘)
  12. #data 指令表示邮件内容
  13. print smtp.docmd(‘data‘)
  14. print smtp.docmd(‘‘‘‘‘from: [email protected]
  15. to: [email protected]
  16. subject: subject
  17. email body
  18. .
  19. ‘‘‘)
  20. smtp.quit()

SMTP.helo([hostname])

  使用"helo"指令向服务器确认身份。相当于告诉smtp服务器“我是谁”。

SMTP.has_extn(name)

  判断指定名称在服务器邮件列表中是否存在。出于安全考虑,smtp服务器往往屏蔽了该指令。

SMTP.verify(address)

  判断指定邮件地址是否在服务器中存在。出于安全考虑,smtp服务器往往屏蔽了该指令。

SMTP.login(user, password)

  登陆到smtp服务器。现在几乎所有的smtp服务器,都必须在验证用户信息合法之后才允许发送邮件。

SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])

  发送邮件。这里要注意一下第三个参数,msg是字符串,表示邮件。我们知道邮件一般由标题,发信人,收件人,邮件内容,附件等构成,发送邮件的时候,要注意msg的格式。这个格式就是smtp协议中定义的格式。在上面的例子中,msg的值为:

  1. ‘‘‘‘‘From: [email protected]
  2. To: [email protected]
  3. Subject: test
  4. just for test‘‘‘

  这个字符串的的意思表示邮件发件人为"[email protected]",收件人为" [email protected]",邮件标题为"test",邮件内容为"just for test"。细心的你可能会疑问:如果要发送的邮件内容很复杂,包含图片、视频、附件等内容,按照MIME的格式来拼接字符串,将是一件非常麻烦的事。不用担心,python已经考虑到了这点,它为我们提供了email模块,使用该模块可以轻松的发送带图片、视频、附件等复杂内容的邮件。在介绍完smtplib模块之后,我会简单介绍email模块的基本使用。

SMTP.quit()

  断开与smtp服务器的连接,相当于发送"quit"指令。

email及其相关子模块

  emial模块用来处理邮件消息,包括MIME和其他基于RFC 2822的消息文档。使用这些模块来定义邮件的内容,是非常简单的。下面是一些常用的类:

class email.mime.multipart. MIMEMultipart: 多个MIME对象的集合。

class email.mime.audio. MIMEAudio: MIME音频对象。

class email.mime.image. MIMEImage: MIME二进制文件对象。

class email.mime.text. MIMEText: MIME文本对象。

      看上面的解释可能会觉得云里雾里,其实我对smtp, MIME的理解也很肤浅。但在大多数时候,我们只要会用就可以了。下面是一个简单的例子来演示如何使用这些类来发  送带附件的邮件:

#coding=gbk

  1. import smtplib, mimetypes
  2. from email.mime.text import MIMEText
  3. from email.mime.multipart import MIMEMultipart
  4. from email.mime.image import MIMEImage
  5. msg = MIMEMultipart()
  6. msg[‘From‘] = "[email protected]"
  7. msg[‘To‘] = ‘[email protected]‘
  8. msg[‘Subject‘] = ‘email for tesing‘
  9. #添加邮件内容
  10. txt = MIMEText("这是邮件内容~~")
  11. msg.attach(txt)
  12. #添加二进制附件
  13. fileName = r‘e:/PyQt4.rar‘
  14. ctype, encoding = mimetypes.guess_type(fileName)
  15. if ctype is None or encoding is not None:
  16. ctype = ‘application/octet-stream‘
  17. maintype, subtype = ctype.split(‘/‘, 1)
  18. att1 = MIMEImage((lambda f: (f.read(), f.close()))(open(fileName, ‘rb‘))[0], _subtype = subtype)
  19. att1.add_header(‘Content-Disposition‘, ‘attachment‘, filename = fileName)
  20. msg.attach(att1)
  21. #发送邮件
  22. smtp = smtplib.SMTP()
  23. smtp.connect(‘smtp.yeah.net:25‘)
  24. smtp.login(‘from‘, ‘密码‘)
  25. smtp.sendmail(‘[email protected]‘, ‘[email protected]‘, msg.as_string())
  26. smtp.quit()
  27. print ‘邮件发送成功‘

  是不是很简单。简单就是美,用最少的代码把问题解决,这就是Python。更多关于smtplib的信息,请参考Python手册 smtplib模块。

发送不带附件的简单的邮件的代码:

1 import smtplib
  2 from email.mime.text import MIMEText
  3 from email.MIMEMultipart import MIMEMultipart
  4 
  5 def sendsimplemail (warning):
  6         msg = MIMEText(warning)
  7         msg[‘Subject‘] = ‘python first mail‘
  8         msg[‘From‘] = ‘[email protected]‘
  9         try:
 10                 smtp = smtplib.SMTP()
 11                 smtp.connect(r‘smtp.163.com‘)
 12                 smtp.login(‘[email protected]‘, ‘密码‘)
 13                 smtp.sendmail(‘[email protected]‘, [‘[email protected]‘], msg.as_string())
 14                 smtp.close()
 15         except Exception, e:
 16                         print e
 17 
 18 if __name__ == ‘__main__‘:
 19         sendsimplemail(warning = "This is a warning!!!")

时间: 2024-08-06 20:08:47

python邮件发送的相关文章

python 邮件发送

#!/usr/bin/env python # -*- coding:UTF-8 -*- #需要在邮箱处设置开启SMTP服务(第三方客户端发送) import smtplibfrom email.mime.text import MIMETextfrom email.utils import formataddrdef mail(): ret = True try: msg = MIMEText('测试邮件','plain','utf-8') msg['From'] = formataddr([

python邮件发送开服记录

#-*-coding:utf-8 -*- #!/usr/bin/python import sys reload(sys) # reload 才能调用 setdefaultencoding 方法 sys.setdefaultencoding('utf-8') # 设置 'utf-8' import MySQLdb import houtai_dbname from email.mime.text import MIMEText from email.header import Header im

Python邮件发送脚本(Linux,Windows)通用

脚本 #!/usr/bin/python #-*- coding:utf-8 -*- #Python Mail for chenglee #if fileformat=dos, update fileformat=unix #code:set fileformat=unix #check:set ff ? import smtplib from email.mime.text import MIMEText from email.utils import formataddr my_sender

Python邮件发送源码

从最初的实现邮件发送功能,改了几次,有了如下代码. 该代码还可以继续开发,完善.以实现更复杂的功能. #-*- coding:utf-8 -*- i = 0 while i < 10: import smtplib from email.mime.text import MIMEText from email.header import Header import time # 第三方 SMTP 服务 mail_host="smtp.qq.com" #设置服务器 mail_use

python邮件发送脚本

转自:http://phinecos.cnblogs.com/ 1 #!/usr/bin/python 2 #coding=utf-8 3 4 #@author:dengyike 5 #@date:2010-09-28 6 #@version:1.0 7 #@description: auto sending email with attachment file 8 9 import email 10 import mimetypes 11 from email.MIMEMultipart im

python 邮件发送的代码 网上的方法汇总一下

一.文件形式的邮件 #!/usr/bin/env python3#coding: utf-8import smtplibfrom email.mime.text import MIMETextfrom email.header import Header sender = '***'receiver = '***'subject = 'python email test'smtpserver = 'smtp.163.com'username = '***'password = '***' msg

python 邮件发送 zabbix 图片

获取图片 http://www.iyunv.com/thread-21332-1-1.html 邮件内嵌图片 http://www.tuicool.com/articles/vaAVri

Python邮件发送功能

import smtplibfrom email.mime.text import MIMEText_user = "[email protected]"#发件人_pwd = "wcpxldrtuthagjbc"#qq邮箱授权码_to = "[email protected]"#收件人 msg = MIMEText("Hellow,This is my first Email!")#邮件内容msg["Subject&

python邮件发送代码

1.通过socket 1 In [1]: import socket 2 3 In [2]: smtp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 4 5 In [3]: smtp.connect(("163mx00.mxmail.netease.com", 25)) 6 7 In [4]: smtp.send("Hello Mr He.\r\n") 8 Out[4]: 14 9 10 In [5]: sm