用python发带附件的邮件用来定时备份mysql数据库
最近迁移了wordpress,系统升级为CentOS 6,很奇怪的一个问题,在原来CentOS 5.8下用的很正常的定时备份数据库并通过邮件发送的脚本不能发送附件,其他都正常,邮件内容也是uuencode生成的文件编码,但是就是不产生附件.而且找不出原因,望有知道的不吝赐教. 为了解
最近迁移了wordpress,系统升级为CentOS 6,很奇怪的一个问题,在原来CentOS 5.8下用的很正常的定时备份数据库并通过邮件发送的脚本不能发送附件,其他都正常,邮件内容也是uuencode生成的文件编码,但是就是不产生附件.而且找不出原因,望有知道的不吝赐教.
为了解决这一问题,我用Python写了一个mail客户端,可以发送附件,是一个命令行程序.废话不多说.贴代码:
#!/usr/bin/env python #-*- coding: utf8 -*- ''' #============================================================================= # FileName: mail.py # Desc: To send email # Author: cold # Email: wh_linux@126.com # HomePage: http://www.linuxzen.com # Version: 0.0.1 # LastChange: 2012-04-21 16:37:20 # History: #============================================================================= ''' ''' 用于发送邮件,可以发送附件 命令行程序 ''' import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import sys # 打印帮助信息 def helpinfo(): print ''' Useage: pymail -u user@domain -p passwd -h smtp server host -t to who [-a attachment file path] [-n attachment name] Useage: email content use . to end -h specify smtp server host -u which user you login the smtp server,and must with it domain -p the password of the smtp user -t The email recipient,multiple addresses can use ',' split -a Add attachment -n Secify attachment name in the email Author:cold(wh_linux@126.com) Homepge:http://www.linuxzen.com ''' # 所有选项 options = ['-t', '-a', '-n', '-h', '-u', '-p', '-s'] # 获取选项长度 argvnum = len(sys.argv) # 检测命令行参数 for i in range(argvnum): if ( i %2 != 0): if (sys.argv[i] not in options): print 'Unknow option ', sys.argv[i] , ', Please use -h see help!' sys.exit(3) # 如果是-h或者没有命令行参数则显示帮助 try: if sys.argv[1] == '-h' or len(sys.argv) == 0: helpinfo() except: helpinfo() # 检测-n参数 if ('-n' in sys.argv) and ('-a' not in sys.argv): print 'Error:option "-n" must use after -a' sys.exit(2) # 下面则是获取各个参数内容 try: tmpmailto = sys.argv[sys.argv.index('-t') + 1] if ',' in tmpmailto: mailto = tmpmailto.split(',') else: mailto = [tmpmailto,] except ValueError: print 'Error: need Mail Recipient' sys.exit(1) haveattr=True try: attrpath = sys.argv[sys.argv.index('-a') + 1] try: attrname = sys.argv[sys.argv.index('-n') +1 ] except ValueError: attrname = attrpath.split('/')[-1] except: attrname = None haveattr = False attrpath = None try: mail_host = sys.argv[sys.argv.index('-h') +1] except ValueError: print 'Waring: No specify smtp server use 127.0.0.1' mail_host = '127.0.0.1' try: mail_useremail = sys.argv[sys.argv.index('-u') +1] except ValueError: print 'Waring: No specify user, use root' mail_useremail = 'root@localhost' try: mail_sub = sys.argv[sys.argv.index('-s') + 1] except: mail_sub = 'No Subject' mail_user = mail_useremail.split('@')[0] mail_postfix = mail_useremail.split('@')[1] try: mail_pass = sys.argv[sys.argv.index('-p') +1] except ValueError: mail_pass = '' # 定义邮件发送函数 def send_mail(to_list, sub, content, haveattr, attrpath, attrname): me = mail_user + "" # 判断是否有附件 if (haveattr): if (not attrpath): print 'Error : no input file of attachments' return False # 有附件则创建一个带附件的实例 msg = MIMEMultipart() # 构造附件 att = MIMEText(open(attrpath, 'rb').read(),'base64', 'utf8') att["Content-Type"] = 'application/octest-stream' att["Content-Disposition"] = 'attachment;filename="'+ attrname +'"' msg.attach(att) msg.attach(MIMEText(content)) else: # 无责创建一个文本的实例 msg = MIMEText(content) # 邮件头 msg['Subject'] = sub msg['From'] = me msg['To'] = ";".join(to_list) try: # 发送邮件 s = smtplib.SMTP() s.connect(mail_host) if (mail_host != '127.0.0.1'): s.login(mail_user, mail_pass) s.sendmail(me, to_list, msg.as_string()) s.close() return True except Exception, e: print str(e) return False if __name__ == '__main__': try: content = '' while True: c = raw_input('') if c == '.': break content += c + '\n' except EOFError: for line in sys.stdin: content += line if send_mail(mailto, mail_sub, content, haveattr, attrpath, attrname): print "Success" else: print "Failed"
将这个脚本保存为pymail放到/usr/bin/下,并赋予其执行权限:
chmod +x /usr/bin/pymail
可以使用 -h指定smtp发件服务器,默认认为指定-h需要认证, 所以就需要smtp服务器支持认证,同时需要-u指定用户名(需加"@域名"),-p指定密码. 如果不指定-h就会使用本地smtp服务器,默认不需要认证,所以本地的smtp服务器就不能支持认证,同时不需指定-u,-p参数
-t 指定收件人多个可用,号分割. -a 指定附件路径 -n 指定附件名(可省略) -h 显示帮助信息. -s 指定邮件主题
执行后会要求输入邮件内容,写完用.结束 也可以用管道下面给出几个实例:
#使用本地smtp服务发送 echo 'linuxzen.com backup' | pymail -s "Linuxzen backup" -t 123456@qq.com -a /tmp/linuxzen.tar.gz # 使用126邮箱发送 echo 'linuxzen.com backup' | pymail -u linuxzen@126.com -p linuxzen.com -h smtp.126.com -s 'Linuxzen backup " -t 123456@qq.com -a /tmp/linuxzen.tar.gz # 不使用管道发送 pymail -u linuxzen@126.com -p linuxzen.com -h smtp.126.com -s 'hello world' -t 123456@qq.com -a /tmp/linuxzen.tar.gz Hello this is a test mail .
下面之前使用的mysql定时备份的脚本:
#!/bin/bash export PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin DATE=`date +%Y%m%d` mysqldump -u root blogdata > /tmp/blogdate."$DATE".sql cd /tmp tar -zcf blogdata."$DATE".sql.tar.gz blogdata."$DATE".sql uuencode blogdata."$DATE".sql.tar.gz blogdata."$DATE".sql.tar.gz | mail -s 'MySQL Backup' 123456@qq.com
没有命令uuencode安装sharutils包即可
yum -y install sharutils
然后使用crontab调用这个脚本定时执行,前面说了 这个脚本在CentOS5.x下正常工作,但是放到CentOS6下就不带附件,所以使用我们自己编写的python脚本脚本内容如下:
#!/bin/bash export PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin DATE=`date +%Y%m%d` mysqldump -u root blogdata > /tmp/myblog."$DATE".sql cd /tmp tar -zcf blogdata."$DATE".sql.tar.gz myblog."$DATE".sql echo 'MySQL backup' | pymail -u linuzen@126.com -p linuxzen.com -h smtp.126.com -s 'MySQL backup' -a /tmp/blogdata."$DATE".sql.tar.gz -t 123456@qq.com
我们使用126邮箱来发送 这样就可以把自带的sendmail 停掉:
service sendmail stop chkonfig --del sendmail
原文地址:用python发带附件的邮件用来定时备份mysql数据库, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Laravel is suitable for projects that teams are familiar with PHP and require rich features, while Python frameworks depend on project requirements. 1.Laravel provides elegant syntax and rich features, suitable for projects that require rapid development and flexibility. 2. Django is suitable for complex applications because of its "battery inclusion" concept. 3.Flask is suitable for fast prototypes and small projects, providing great flexibility.

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.
