Created
March 28, 2015 00:43
-
-
Save ruiztulio/14a09ca3dfcd8e626dfc to your computer and use it in GitHub Desktop.
Simple and wired script to send an email with attachements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # -*- coding: utf-8 -*- | |
| import smtplib | |
| from email.mime.application import MIMEApplication | |
| from email.mime.multipart import MIMEMultipart | |
| from email.mime.text import MIMEText | |
| from email.MIMEBase import MIMEBase | |
| from email import Encoders | |
| from os.path import basename | |
| import re | |
| def send_mail(info, files): | |
| msg = MIMEMultipart() | |
| msg["From"] = info['from'] | |
| msg["To"] = ",".join(info['to']) | |
| msg["Subject"] = info['subject'] | |
| msg.attach(MIMEText(info["message"])) | |
| for f in files or []: | |
| with open(f, "rb") as fil: | |
| part = MIMEBase('application', "octet-stream") | |
| part.set_payload(fil.read()) | |
| Encoders.encode_base64(part) | |
| part.add_header('Content-Disposition', 'attachment; filename="%s"' % basename(f)) | |
| msg.attach(part) | |
| server = smtplib.SMTP(info['server']) | |
| server.starttls() | |
| server.login(info['from'], info['pswd']) | |
| problems = server.sendmail(info['from'], info['to'], msg.as_string()) | |
| server.quit() | |
| def mail_body(): | |
| r = resume_log("/tmp/uall.log") | |
| res = """ | |
| Hi, | |
| The build you requested have finished, you can inspect the instance in | |
| the following url: | |
| http://bardock.vauxoo.com:8069 | |
| Attached to this mail you can find the log files genereted during the process. | |
| Error/warns count: | |
| Import Errors: %s | |
| Errors: %s | |
| Warnings (no translation related): %s | |
| Translation warnings: %s | |
| Critical error: %s | |
| Regards. | |
| """ % (len(r.get('import_errors')),len(r.get('errors')),len(r.get('warnings')),len(r.get('warnings_trans')),len(r.get('critical'))) | |
| return res | |
| def main(): | |
| mail_info = {'from': '[email protected]', | |
| 'to': ['[email protected]', '[email protected]'], | |
| 'pswd': 'le_password', | |
| 'subject': 'Build complete - Vauxoo 8.0', | |
| 'server': "smtp.gmail.com:587"} | |
| body = mail_body() | |
| mail_info.update({'message': body}) | |
| send_mail(mail_info, ["/tmp/build_logs.tar.bz2", "/tmp/branches-info.json"]) | |
| def resume_log(log_file): | |
| with open(log_file, "rt") as log: | |
| lines = log.read().split('\n') | |
| res = { | |
| 'import_errors': [], | |
| 'errors': [], | |
| 'warnings': [], | |
| 'critical': [], | |
| 'warnings_trans': [] | |
| } | |
| crit = re.compile(r'.*\d\sCRITICAL\s.*') | |
| warn_trans = re.compile(r'.*\d\sWARNING\s.*no translation for language.*') | |
| warn = re.compile(r'.*\d\sWARNING\s.*') | |
| err = re.compile(r'.*\d\sERROR\s.*') | |
| imp = re.compile(r'^ImportError.*') | |
| re.compile(r'.*\d\s\s.*') | |
| for line in lines: | |
| l = line.strip() | |
| if crit.match(l): | |
| res.get('critical').append(l) | |
| elif warn_trans.match(l): | |
| res.get('warnings_trans').append(l) | |
| elif err.match(l): | |
| res.get('errors').append(l) | |
| elif imp.match(l): | |
| res.get('import_errors').append(l) | |
| elif warn.match(l): | |
| res.get('warnings').append(l) | |
| return res | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment