From a277a4493b041b5012e0b95eb68edcb384be855c Mon Sep 17 00:00:00 2001 From: TheDragonkeeper Date: Sat, 4 Nov 2017 12:43:20 +0000 Subject: [PATCH] Lets Send Packages Of Nuts ! Here is my extension for sending emails; also includes sending files and html format. --- payloads/extensions/sendemail.py | 72 ++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 payloads/extensions/sendemail.py diff --git a/payloads/extensions/sendemail.py b/payloads/extensions/sendemail.py new file mode 100644 index 0000000..0cf3fd7 --- /dev/null +++ b/payloads/extensions/sendemail.py @@ -0,0 +1,72 @@ +import smtplib, os +from email.MIMEMultipart import MIMEMultipart +from email.MIMEBase import MIMEBase +from email.MIMEText import MIMEText +from email.MIMEImage import MIMEImage +from email.Utils import COMMASPACE, formatdate +from email import Encoders +import ConfigParser + +def send_mail(send_from, send_to, subject, text, files=None, + data_attachments=None, server="None", port=587, + tls=True, html=False, images=None, + username=None, password=None, + config_file=None, config=None): + + if files is None: + files = [] + + if images is None: + images = [] + + if data_attachments is None: + data_attachments = [] + + if config_file is not None: + config = ConfigParser.ConfigParser() + config.read(config_file) + + if config is not None: + server = config.get('smtp', 'server') + port = config.get('smtp', 'port') + tls = config.get('smtp', 'tls').lower() in ('true', 'yes', 'y') + username = config.get('smtp', 'username') + password = config.get('smtp', 'password') + + msg = MIMEMultipart('related') + msg['From'] = send_from + msg['To'] = send_to if isinstance(send_to, basestring) else COMMASPACE.join(send_to) + msg['Date'] = formatdate(localtime=True) + msg['Subject'] = subject + + msg.attach( MIMEText(text, 'html' if html else 'plain') ) + + for f in files: + part = MIMEBase('application', "octet-stream") + part.set_payload( open(f,"rb").read() ) + Encoders.encode_base64(part) + part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f)) + msg.attach(part) + + for f in data_attachments: + part = MIMEBase('application', "octet-stream") + part.set_payload( f['data'] ) + Encoders.encode_base64(part) + part.add_header('Content-Disposition', 'attachment; filename="%s"' % f['filename']) + msg.attach(part) + + for (n, i) in enumerate(images): + fp = open(i, 'rb') + msgImage = MIMEImage(fp.read()) + fp.close() + msgImage.add_header('Content-ID', ''.format(str(n+1))) + msg.attach(msgImage) + + smtp = smtplib.SMTP(server, int(port)) + if tls: + smtp.starttls() + + if username is not None: + smtp.login(username, password) + smtp.sendmail(send_from, send_to, msg.as_string()) + smtp.close()