def get_content_block(self, f): binding_map = { '.xlsx': ('application', 'vnd.openxmlformats-officedocument.spreadsheetml.sheet'), '.xls': ('application', 'vnd.ms-excel'), '.csv': ('text', 'csv'), '.json': ('application', 'json'), '.syslog': ('text', 'syslog') } # Check the file extension to see if it is a supported type name_part, ext_part = os.path.splitext(f.name) if ext_part.lower() == '.xml': cb = tm11.ContentBlock(tm11.ContentBinding(t.CB_STIX_XML_101), f.read()) else: binding_tuple = binding_map.get(ext_part.lower(), None) if not binding_tuple: logger.error('File extension not supported: %s. Supported extensions: %s' % (ext_part, binding_map.keys())) return # Read the file and create a MIME message for it maintype = binding_tuple[0] subtype = binding_tuple[1] # Note: This is MIME subtype, not TAXII subtype mime_msg = MIMENonMultipart(maintype, subtype) mime_msg.add_header('Content-Disposition', 'attachment', filename=f.name) mime_msg.set_payload(f.read()) encode_base64(mime_msg) cb = tm11.ContentBlock('%s/%s' % (maintype, subtype), mime_msg.as_string()) return cb
def formatMail(mailText): """returns a mail with headers and content properly formatted as a bytestring and MIME. mailText must be a unicode instance or pure ASCII """ rawHeaders, rawBody = mailText.split("\n\n", 1) cs = charset.Charset("utf-8") cs.body_encoding = charset.QP cs.header_encoding = charset.QP # they've botched MIMEText so bad it can't really generate # quoted-printable UTF-8 any more. So, let's forget MIMEText: msg = MIMENonMultipart("text", "plain", charset="utf-8") msg.set_payload(rawBody, charset=cs) for key, value in Parser().parsestr(rawHeaders.encode("utf-8")).items(): if re.match("[ -~]*$", value): # it's plain ASCII, don't needlessly uglify output msg[key] = value else: msg[key] = Header(value, cs) msg["Date"] = emailutils.formatdate(time.time(), localtime=False, usegmt=True) msg["X-Mailer"] = "DaCHS VO Server" return msg.as_string()
def send_sms_email(filepath, new_door_state): """ Sends the email for door status change. """ config = configparser.ConfigParser() config.read(filepath) smtp_server = config.get('smtp', 'smtp_server') smtp_port = config.get('smtp', 'smtp_port') smtp_user = config.get('smtp', 'smtp_user') smtp_password = config.get('smtp', 'smtp_password') server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() server.login(smtp_user, smtp_password) time.sleep(1) msg = "" for email_addr, when_sms in config.items('email'): send_message = True if (new_door_state == "Open"): if (when_sms == "closed"): send_message = False if send_message: mmsg2 = MIMENonMultipart('text', 'plain', charset='utf-8') mmsg2["Subject"] = "Door: " + new_door_state mmsg2["From"] = "*****@*****.**" mmsg2["To"] = email_addr mmsg2["Cc"] = "" msg = " " mmsg2.set_payload(msg) server.sendmail(mmsg2["From"], mmsg2["To"].split(",") + mmsg2["Cc"].split(","), mmsg2.as_string()) server.quit()
def _send_thread(sender, recipient, subject, message): MODULE.info('sending mail: thread running') msg = MIMENonMultipart('text', 'plain', charset='utf-8') cs = email.charset.Charset("utf-8") cs.body_encoding = email.charset.QP msg["Subject"] = subject msg["From"] = sender msg["To"] = recipient msg.set_payload(message, charset=cs) server = smtplib.SMTP('localhost') server.set_debuglevel(0) server.sendmail(sender, recipient, msg.as_string()) server.quit() return True
def send(self): path_ucr = self.ucr.get( "umc/self-service/passwordreset/email/text_file") if path_ucr and os.path.exists(path_ucr): path = path_ucr else: path = os.path.join(os.path.realpath(os.path.dirname(__file__)), "email_body.txt") with open(path, "rb") as fp: txt = fp.read() fqdn = ".".join([self.ucr["hostname"], self.ucr["domainname"]]) frontend_server = self.ucr.get( "umc/self-service/passwordreset/email/webserver_address", fqdn) link = "https://{fqdn}/univention/self-service/#page=newpassword".format( fqdn=frontend_server) tokenlink = "https://{fqdn}/univention/self-service/#page=newpassword&token={token}&username={username}".format( fqdn=frontend_server, username=urllib.quote(self.data["username"]), token=urllib.quote(self.data["token"])) txt = txt.format(username=self.data["username"], token=self.data["token"], link=link, tokenlink=tokenlink) msg = MIMENonMultipart('text', 'plain', charset='utf-8') cs = email.charset.Charset("utf-8") cs.body_encoding = email.charset.QP msg["Subject"] = "Password reset" msg["Date"] = formatdate(localtime=True) msg["From"] = self.ucr.get( "umc/self-service/passwordreset/email/sender_address", "Password Reset Service <noreply@{}>".format(fqdn)) msg["To"] = self.data["address"] msg.set_payload(txt, charset=cs) smtp = smtplib.SMTP(self.server) smtp.sendmail(msg["From"], self.data["address"], msg.as_string()) smtp.quit() self.log("Sent mail with token to address {}.".format( self.data["address"])) return True
def mbox(self): from email.mime.nonmultipart import MIMENonMultipart from email.encoders import encode_7or8bit body = '' if self.description: body += self.description.content + '\n' body += self.content mbox = MIMENonMultipart('text', 'plain', charset='utf-8') mbox['Subject'] = ": ".join([t.name for t in self.tags] + [self.name.strip().capitalize()]) mbox['From'] = '%s <%s>' % (self.submitter.name, self.submitter.email) mbox['Message-Id'] = self.msgid mbox.set_payload(body.encode('utf-8')) encode_7or8bit(mbox) return mbox.as_string()
def mbox(self): from email.mime.nonmultipart import MIMENonMultipart from email.encoders import encode_7or8bit body = '' if self.comments[0].msgid == self.msgid: body += self.comments[0].content + '\n' body += self.content mbox = MIMENonMultipart('text', 'plain', charset='utf-8') mbox['Subject'] = self.name mbox['From'] = '%s <%s>' % (self.submitter.name, self.submitter.email) mbox['Message-Id'] = self.msgid mbox.set_payload(body.encode('utf-8')) encode_7or8bit(mbox) return mbox.as_string()
def get_content_block(self, f): binding_map = { '.xlsx': ('application', 'vnd.openxmlformats-officedocument.spreadsheetml.sheet'), '.xls': ('application', 'vnd.ms-excel'), '.csv': ('text', 'csv'), '.json': ('application', 'json'), '.syslog': ('text', 'syslog') } # Check the file extension to see if it is a supported type name_part, ext_part = os.path.splitext(f.name) if ext_part.lower() == '.xml': cb = tm11.ContentBlock(tm11.ContentBinding(t.CB_STIX_XML_101), f.read()) else: binding_tuple = binding_map.get(ext_part.lower(), None) if not binding_tuple: logger.error( 'File extension not supported: %s. Supported extensions: %s' % (ext_part, binding_map.keys())) return # Read the file and create a MIME message for it maintype = binding_tuple[0] subtype = binding_tuple[ 1] # Note: This is MIME subtype, not TAXII subtype mime_msg = MIMENonMultipart(maintype, subtype) mime_msg.add_header('Content-Disposition', 'attachment', filename=f.name) mime_msg.set_payload(f.read()) encode_base64(mime_msg) cb = tm11.ContentBlock('%s/%s' % (maintype, subtype), mime_msg.as_string()) return cb
def _serialize_request(self, request): """Convert an HttpRequest object into a string. Args: request: HttpRequest, the request to serialize. Returns: The request as a string in application/http format. """ # Construct status line parsed = urlparse.urlparse(request.uri) request_line = urlparse.urlunparse( (None, None, parsed.path, parsed.params, parsed.query, None) ) status_line = request.method + ' ' + request_line + ' HTTP/1.1\n' major, minor = request.headers.get('content-type', 'application/json').split('/') msg = MIMENonMultipart(major, minor) headers = request.headers.copy() # MIMENonMultipart adds its own Content-Type header. if 'content-type' in headers: del headers['content-type'] for key, value in headers.iteritems(): msg[key] = value msg['Host'] = parsed.netloc msg.set_unixfrom(None) if request.body is not None: msg.set_payload(request.body) msg['content-length'] = str(len(request.body)) body = msg.as_string(False) # Strip off the \n\n that the MIME lib tacks onto the end of the payload. if request.body is None: body = body[:-2] return status_line.encode('utf-8') + body
def _notify_about_account_deregistration(self, username, mail): if not mail: return ucr.load() path_ucr = ucr.get("umc/self-service/account-deregistration/email/text_file") if path_ucr and os.path.exists(path_ucr): path = path_ucr else: path = "/usr/share/univention-self-service/email_bodies/deregistration_notification_email_body.txt" with open(path, "r") as fp: txt = fp.read() txt = txt.format(username=username) msg = MIMENonMultipart('text', 'plain', charset='utf-8') msg["Subject"] = "Account deletion" msg["Date"] = formatdate(localtime=True) msg["From"] = ucr.get("umc/self-service/account-deregistration/email/sender_address", "Password Reset Service <noreply@{}>".format(".".join([ucr["hostname"], ucr["domainname"]]))) msg["To"] = mail cs = email.charset.Charset("utf-8") cs.body_encoding = email.charset.QP msg.set_payload(txt, charset=cs) smtp = smtplib.SMTP(ucr.get("umc/self-service/account-deregistration/email/server", "localhost")) smtp.sendmail(msg["From"], msg["To"], msg.as_string()) smtp.quit()
def send(self): path_ucr = self.ucr.get("umc/self-service/account-verification/email/text_file") if path_ucr and os.path.exists(path_ucr): path = path_ucr else: path = "/usr/share/univention-self-service/email_bodies/verification_email_body.txt" with open(path, "r") as fp: txt = fp.read() fqdn = ".".join([self.ucr["hostname"], self.ucr["domainname"]]) frontend_server = self.ucr.get("umc/self-service/account-verification/email/webserver_address", fqdn) link = "https://{fqdn}/univention/self-service/#page=verifyaccount".format(fqdn=frontend_server) tokenlink = "https://{fqdn}/univention/self-service/#page=verifyaccount&token={token}&username={username}&method={method}".format( fqdn=frontend_server, username=urllib.quote(self.data["username"]), token=urllib.quote(self.data["token"]), method=self.send_method(), ) txt = txt.format(username=self.data["username"], token=self.data["token"], link=link, tokenlink=tokenlink) msg = MIMENonMultipart('text', 'plain', charset='utf-8') cs = email.charset.Charset("utf-8") cs.body_encoding = email.charset.QP msg["Subject"] = "Account verification" msg["Date"] = formatdate(localtime=True) msg["From"] = self.ucr.get("umc/self-service/account-verification/email/sender_address", "Account Verification Service <noreply@{}>".format(fqdn)) msg["To"] = self.data["address"] msg.set_payload(txt, charset=cs) smtp = smtplib.SMTP(self.server) smtp.sendmail(msg["From"], self.data["address"], msg.as_string()) smtp.quit() self.log("Sent mail with token to address {}.".format(self.data["address"])) return True