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

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Secure Thoughts
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
The Blog of Author Tim Ferriss
B
Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
Arctic Wolf
T
The Exploit Database - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
AWS News Blog
AWS News Blog
MongoDB | Blog
MongoDB | Blog
Y
Y Combinator Blog
Google Online Security Blog
Google Online Security Blog
T
Troy Hunt's Blog
I
InfoQ
L
LINUX DO - 热门话题
WordPress大学
WordPress大学
C
Cisco Blogs
G
GRAHAM CLULEY
The Register - Security
The Register - Security
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Schneier on Security
Schneier on Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
H
Hacker News: Front Page
小众软件
小众软件
雷峰网
雷峰网
The Hacker News
The Hacker News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tor Project blog
博客园 - 聂微东
N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
The GitHub Blog
The GitHub Blog
腾讯CDC
P
Palo Alto Networks Blog
Scott Helme
Scott Helme

博客园 - 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