博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Sendmail with Attachment script
阅读量:6226 次
发布时间:2019-06-21

本文共 2538 字,大约阅读时间需要 8 分钟。

四月 24th, 2008

This is the python script for linux sendmail programe or  with smtp server.

You can use it free.It's simple ,but support the attachment.

 
#
-*- coding: utf-8 -*-
#
sendmail with mimebase
import
smtplib
import
os,sys,codecs,string
import
locale
from
optparse
import
OptionParser
from
email.MIMEMultipart
import
MIMEMultipart
from
email.MIMEBase
import
MIMEBase
from
email.MIMEText
import
MIMEText
from
email.Utils
import
COMMASPACE, formatdate
from
email
import
Encoders
def
send_mail(send_from, send_to, subject, text, files, server):
#
assert type(send_to)==list
assert
type(files)
==
list msg
=
MIMEMultipart() msg[
'
From
'
]
=
send_from msg[
'
To
'
]
=
send_to msg[
'
Date
'
]
=
formatdate(localtime
=
True) msg[
'
Subject
'
]
=
subject msg.attach( MIMEText(text) )
for
f
in
files: part
=
MIMEBase(
'
application
'
,
"
octet-stream
"
) s
=
open(f,
"
rb
"
).read() part.set_payload( s ) Encoders.encode_base64(part) part.add_header(
'
Content-Disposition
'
,
'
attachment; filename="%s"
'
%
os.path.basename(f)) msg.attach(part)
if
(server): smtp
=
smtplib.SMTP(server) smtp.sendmail(send_from, send_to, msg.as_string()) smtp.close() cmd
=
"
sendmail %s
"
%
send_to
print
'
sending...
'
p
=
os.popen(cmd,
"
w
"
) p.write(msg.as_string()) p.close()
def
main(argv): locale.setlocale(locale.LC_CTYPE,
""
) encoding
=
locale.getlocale()[
1
]
if
not
encoding: encoding
=
"
us-ascii
"
sys.stdout
=
codecs.getwriter(encoding)(sys.stdout, errors
=
"
replace
"
) sys.stderr
=
codecs.getwriter(encoding)(sys.stderr, errors
=
"
replace
"
) usage
=
"
usage: %prog [options] arg \nnote:Ctrl+D or ... to end the body edit!
"
parser
=
OptionParser(usage
=
usage,version
=
"
%prog 0.1
"
) parser.add_option(
"
-n
"
,
"
--name
"
, dest
=
"
name
"
, help
=
"
Your name.
"
) parser.add_option(
""
,
"
--server
"
, dest
=
"
server
"
, help
=
"
The smtp server address.
"
) parser.add_option(
"
-f
"
,
"
--files
"
, dest
=
"
files
"
, help
=
"
Attachment files. Split by ,
"
) (options, args)
=
parser.parse_args()
if
not
options.name: options.name
=
raw_input(u
"
From:
"
)
if
len(args)
!=
1
: options.to
=
raw_input(u
"
To:
"
)
else
: options.to
=
args[0] subject
=
raw_input(u
"
Subject:
"
)
print
'
Body:
'
; raw
=
''
body
=
''
while
True: raw
=
raw_input(
'
'
)
if
raw
==
'
...
'
or
raw
==
'
\x04
'
:
break
;
else
: body
+=
raw
+
'
\n
'
files
=
[];
if
options.files: files
=
string.split(options.files,
'
,
'
) send_mail(options.name,options.to, subject, body,files,options.server)
if
__name__
==
"
__main__
"
: main(argv
=
sys.argv[
1
:])

转载地址:http://hnjna.baihongyu.com/

你可能感兴趣的文章
运营商NFV MANO梦想照进现实
查看>>
2015中国APT研究报告:中国是APT攻击的主要受害国
查看>>
Chatsworth公司推出了一款高科技数据中心柜锁
查看>>
微软豪购Linkedin 补移动社交船票?
查看>>
苹果iMessage上线Business Chat功能
查看>>
戴尔EMC、日立数据系统和NetApp现已出货博科第六代光纤通道方案
查看>>
南航率先加入综合交通出行大数据开放云平台
查看>>
英特尔应当收购联发科原因:成功进入移动芯片市场
查看>>
宗宁:企业微博品牌榜的新时代意义
查看>>
实例:某大型企业遭受勒索蠕虫袭击纪实
查看>>
OA选型之技术与性价比
查看>>
《Clojure数据分析秘笈》——1.8节从网页表中抓取数据
查看>>
《交互式程序设计 第2版》一3.6 导入外部库
查看>>
“云计算”让城市智慧起来
查看>>
Google计划收购数据科学社区Kaggle
查看>>
中国RFID市场规模及结构浅析
查看>>
厂商掘金智能家居市场 三大路径殊途同归
查看>>
京津冀大数据走廊:张北风电光伏成亮点
查看>>
任正非:80后90后是一代将星在闪烁
查看>>
Oracle 12c多租户特性详解:PDB 的备份与恢复
查看>>