Recently, with the help of a colleague, I tried to write a gadget with Python to send a message, eliminating the hassle of configuring the MAILRC parameter. The following is an example:
[[email protected] scripts]# more sendmail.py #!/usr/bin/env python2#coding:utf-8 import Smtplibimport Datetimefrom email.mime.text Import mimetextfrom email.header import headerfrom email.mime.multipart Import Mimemultipart from Email.mime.text import mimetext from email.mime.image import mimeimagesender = ' [email prote CTED] ' receiver = [' [email protected] ', ' [email protected] ']subject = ' pro_weekly_dbmonitor ' smtpserver = ' Mail.163.com ' username = ' monitor ' password = ' secrets ' #f = Open ("/dba/dbabackup/backup.log", "r") #content = F.read () # F.close () content = ' More see the attached file ' msg = Mimemultipart (' related ') msg[' Subject '] = ' postgresql_monitor '-- Get Time Today=datetime.datetime.now (). Strftime ('%y-%m-%d ')--the attachment to be sent file_name= '/home/postgres/report/db_ ' +str (today) + '. Report.txt ' att = mimetext (open (file_name, ' RB '). Read (), ' base64 ', ' Utf-8 ') att["content-type"] = ' application/ Octet-stream ' att["content-disposition"] = ' attachment; Filename= "Db_report.txt" ' Msg.attach (atT)--SMTPSMTP = Smtplib to send the required call. SMTP () smtp.connect (' mail.163.com ') smtp.login (username, password) smtp.sendmail (sender, receiver, msg.as_string ()) Smtp.quit ()
By practice, available.
Python e-mail sample