惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
C
CERT Recently Published Vulnerability Notes
P
Proofpoint News Feed
Vercel News
Vercel News
博客园 - 三生石上(FineUI控件)
IT之家
IT之家
Help Net Security
Help Net Security
月光博客
月光博客
N
News and Events Feed by Topic
Cloudbric
Cloudbric
博客园 - 司徒正美
L
LangChain Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tenable Blog
The Register - Security
The Register - Security
The Hacker News
The Hacker News
I
InfoQ
The Last Watchdog
The Last Watchdog
MyScale Blog
MyScale Blog
Schneier on Security
Schneier on Security
WordPress大学
WordPress大学
小众软件
小众软件
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
宝玉的分享
宝玉的分享
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
K
Kaspersky official blog
L
LINUX DO - 热门话题
N
News | PayPal Newsroom
F
Fortinet All Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security @ Cisco Blogs
Recorded Future
Recorded Future
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
Google Online Security Blog
Google Online Security Blog
S
Schneier on Security
C
Cisco Blogs
N
News and Events Feed by Topic
V2EX - 技术
V2EX - 技术
Latest news
Latest news
PCI Perspectives
PCI Perspectives
T
The Blog of Author Tim Ferriss
P
Palo Alto Networks Blog
T
Tor Project blog
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Webroot Blog
Webroot Blog
Attack and Defense Labs
Attack and Defense Labs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org

博客园 - ddr888

Ubuntu16.04LTS国内快速源 bitnami redmine版本由2.3.1升级至3.2.2过程 Ubuntu1404安装gogs过程 Excel根据单元格背景色批量设置格式的宏 服务器监控之热身:只是试用monit SAP ABAP鸟瞰【AV+PPT】 常用SAP表 HP-UX 11i v2上Oracle10.2基本安装指南 Debian5 dom0上跑Xen Debian5 lenny domU&Xen ubuntu10.04 Lucid domU--我爱你,飞一般的感觉 SAP BW BEx工具集简单介绍【AV+PPT】 resolve.conf引起登录HPUX的CDE故障 cx_Oracle说:Python访问Oracle并不难 让服务器时间“云”起来【拍死标题党】 ubuntu 10.04 源 HP-UX 11i v2安装使用python 2.5.2 birdnest 102 api出现internel error处理 mac上配置rails开发环境 Heroku使用手记 ubuntu常用网络服务配置
HP-UX下使用python发送邮件
ddr888 · 2010-03-25 · via 博客园 - ddr888

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

= 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 MIMEBase
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email import Encoders
from email.header import Headerdef 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)

= 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