1 用脚本语言发送邮件是系统管理员必备技能
对系统定期检查并生成文档和报表是管理员最不喜欢的。
发布这些文档最快速有效的方法就是发送邮件。
收集系统信息、格式化信息不在本文讨论。
目标:通过gmail将信息发送给目标邮箱。
环境: HP-UX 11i v2,Python 2.5.2
2 实现
2.1 简单发送邮件
通过Gmail发送,Gmail的SMTP地址为:smtp.gmail.com
import smtplib#导入需要的module
def prompt(prompt): # 定义一个目标邮箱输入的函数
return raw_input(prompt).strip()
mail_server = ' smtp.gmail.com '
mail_server_port = 25
from_addr = ' someone@gmail.com '
to_addr = prompt( " To: " ).split() # 提示输入目标邮箱
from_header = ' From: %s\r\n ' % from_addr
to_header = ' To: %s\r\n\r\n ' % to_addr
subject_header = ' Subject: nothing interesting '
body = ' This is a new SMTP test mail by SSL. '
email_message = ' \n\n\n%s%s%s\n\n%s ' % (from_header, to_header, subject_header, body)
# print email_message
s = smtplib.SMTP(mail_server, mail_server_port) # 创建SMTP对象
# s.set_debuglevel(1)
s.ehlo() # 向Gamil发送SMTP 'ehlo' 命令
s.starttls() # 启动TLS模式,Gmail要求
s.login( " someone@gmail.com " , " 123456789 " ) # 用户验证
s.sendmail(from_addr, to_addr, email_message) # 发送邮件
s.quit() # 退出
def prompt(prompt): # 定义一个目标邮箱输入的函数
return raw_input(prompt).strip()
mail_server = ' smtp.gmail.com '
mail_server_port = 25
from_addr = ' someone@gmail.com '
to_addr = prompt( " To: " ).split() # 提示输入目标邮箱
from_header = ' From: %s\r\n ' % from_addr
to_header = ' To: %s\r\n\r\n ' % to_addr
subject_header = ' Subject: nothing interesting '
body = ' This is a new SMTP test mail by SSL. '
email_message = ' \n\n\n%s%s%s\n\n%s ' % (from_header, to_header, subject_header, body)
# print email_message
s = smtplib.SMTP(mail_server, mail_server_port) # 创建SMTP对象
# s.set_debuglevel(1)
s.ehlo() # 向Gamil发送SMTP 'ehlo' 命令
s.starttls() # 启动TLS模式,Gmail要求
s.login( " someone@gmail.com " , " 123456789 " ) # 用户验证
s.sendmail(from_addr, to_addr, email_message) # 发送邮件
s.quit() # 退出
信息发出。
2.2 MIME邮件发送
MIME--多功能Internet 邮件扩充服务。有了它就可以发送带主题、发送人、接收人和附件等等信息的邮件。在python中也有具体的实现方法:
import smtplib,email,os,sys
from email.MIMEBase import MIMEBasefrom email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email import Encoders
from email.header import Header
def prompt(prompt):
return raw_input(prompt).strip()
mail_server = ' smtp.gmail.com '
mail_server_port = 587
from_addr = ' yourmail@gmail.com '
to_addr = prompt( " To: " ).split()
attachment = ' demo1.py '
msg = MIMEMultipart()
msg[ ' From ' ] = from_addr
msg[ ' To ' ] = str(to_addr)
msg[ ' Subject ' ] = Header( ' ===MIME Email Demo=== ' , ' utf8 ' )
msg[ ' Reply-To ' ] = from_addr
print msg.as_string()
email_message = ' this is sent by MIME Demo '
msg.attach(MIMEText(email_message)) # , _subtype='html', _charset='utf8',))
fp = open(attachment, ' rb ' )
part = MIMEBase( ' application ' , " octet-stream " )
part.set_payload(fp.read())
fp.close()
Encoders.encode_base64(part)
part.add_header( ' Content-Disposition ' , ' attachment; filename="%s" ' % attachment)
msg.attach(part)
s = smtplib.SMTP(mail_server, mail_server_port)
# s.set_debuglevel(1)
s.ehlo()
s.starttls()
s.login( " yourmail@gmail.com " , " yourpassword " )
s.sendmail(from_addr, to_addr, msg.as_string())
s.quit()
这里创建了MIMEMultipart对象并用它生成需要发送的字符串进行发送,在接受到邮件后会显示对应的发件人、主题等内容。
(2.2节于2010-4-5更新)
3 结束
本篇使用Python版本2.5.2,和2.6.*在 starttls()函数中有一些不同,2.6.*中starttls()会自动判断并发送ehlo命令,但是2.5.2中不会,因此需要在执行starttls()之前执行
ehlo()函数,否则会报错:
smtplib.SMTPException: SMTP AUTH extension
not
supported by server