Python 发送带 附件 邮件 脚本

简介: [oracle@rac1 ~]$ cat sendAttEmail.py #!/usr/bin/python#coding=gbk#created by tianlesoftware...

[oracle@rac1 ~]$ cat sendAttEmail.py 

#!/usr/bin/python

#coding=gbk

#created by tianlesoftware

#2011/4/12

 

import os

import sys

import smtplib

import mimetypes

from email.MIMEText import MIMEText

from email.MIMEImage import MIMEImage

from email.MIMEMultipart import MIMEMultipart

 

 

#红色标记的修改成自己的信息

SMTP_SERVER='192.168.1.100'

EMAIL_USER=user

EMAIL_PASSWD=pwd

EMAIL_SUBJECT='This is a test email'

FROM_USER='dvd.dba@gmail.com'

TO_USERS=['dvd.dba@gmail.com','tianlesoftware@vip.qq.com']

 

 

def mysendmail(fromaddr,toaddrs,subject):

        COMMASPACE=','

        msg = MIMEMultipart()

        msg['From'] = fromaddr

        msg['To'] = COMMASPACE.join(toaddrs)

        msg['Subject'] = subject

 

        #邮件内容

        txt = MIMEText("this is the contents of the email!")

        msg.attach(txt)  

 

        #添加附件

        fileName = r'/home/oracle/awr.html'

        ctype, encoding = mimetypes.guess_type(fileName)

        if ctype is None or encoding is not None:

            ctype = 'application/octet-stream'

        maintype, subtype = ctype.split('/', 1)

        att = MIMEImage((lambda f: (f.read(), f.close()))(open(fileName, 'rb'))[0], _subtype = subtype)

        att.add_header('Content-Disposition', 'attachment', filename = fileName)

        msg.attach(att)  

        #添加附件如果有多个附件,就多添加几个

        fileName = r'/home/oracle/awr.html'

        ctype, encoding = mimetypes.guess_type(fileName)

        if ctype is None or encoding is not None:

            ctype = 'application/octet-stream'

        maintype, subtype = ctype.split('/', 1)

        att = MIMEImage((lambda f: (f.read(), f.close()))(open(fileName, 'rb'))[0], _subtype = subtype)

        att.add_header('Content-Disposition', 'attachment', filename = fileName)

        msg.attach(att)  

 

 

        server=smtplib.SMTP(SMTP_SERVER)

        server.login(EMAIL_USER,EMAIL_PASSWD)

        server.sendmail(fromaddr,toaddrs,msg.as_string())

        server.quit()

 

if __name__=='__main__':

        mysendmail(FROM_USER, TO_USERS, EMAIL_SUBJECT)

        #print 'send successful'

目录
相关文章
|
19天前
|
Linux Shell Python
Linux执行Python脚本
Linux执行Python脚本
26 1
|
8天前
|
JSON 测试技术 持续交付
自动化测试与脚本编写:Python实践指南
【4月更文挑战第9天】本文探讨了Python在自动化测试中的应用,强调其作为热门选择的原因。Python拥有丰富的测试框架(如unittest、pytest、nose)以支持自动化测试,简化测试用例的编写与维护。示例展示了使用unittest进行单元测试的基本步骤。此外,Python还适用于集成测试、系统测试等,提供模拟外部系统行为的工具。在脚本编写实践中,Python的灵活语法和强大库(如os、shutil、sqlite3、json)助力执行复杂测试任务。同时,Python支持并发、分布式执行及与Jenkins、Travis CI等持续集成工具的集成,提升测试效率和质量。
|
15天前
|
存储 监控 异构计算
【Python】GPU内存监控脚本
【Python】GPU内存监控脚本
|
15天前
|
Ubuntu Unix Linux
【Linux/Ubuntu】Linux/Ubuntu运行python脚本
【Linux/Ubuntu】Linux/Ubuntu运行python脚本
|
23天前
|
XML Shell Linux
性能工具之 JMeter 使用 Python 脚本快速执行
性能工具之 JMeter 使用 Python 脚本快速执行
38 1
性能工具之 JMeter 使用 Python 脚本快速执行
|
1月前
|
数据采集 测试技术 Python
Python自动化脚本的魅力与实践
Python自动化脚本的魅力与实践
48 0
|
1月前
|
数据安全/隐私保护 Python
使用Python脚本实现图片合成PDF功能
使用Python脚本实现图片合成PDF功能
26 0
|
1月前
|
安全 数据安全/隐私保护 开发者
如何使用Pyarmor保护你的Python脚本
如何使用Pyarmor保护你的Python脚本
42 0
|
1月前
|
数据处理 Python
Python自动化脚本
Python自动化脚本
23 0
|
2月前
|
Android开发 Python Windows
用纯python脚本玩转UU加速器
用纯python脚本玩转UU加速器

热门文章

最新文章