Пример #1
0
    def writeToHTML(self, attachments, html_file):
        check = True
        try:
            #HTML-Header
            html_file.write("<!DOCTYPE html> <html lang=\"en\"> <head> <title>")
            html_file.write(self.getSubject())
            html_file.write("</title> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"> </head> <body> <div class=\"row\"> <div class=\"col-md-12\">")

            #HTML-Table with To,From,Subject
            html_file.write("<table boarder=\"1\">\n")
            html_file.write("\t<tr>\n")
            html_file.write("\t\t<td>From: </td>\n")
            html_file.write("\t\t<td>" + self.getFrom() + "</td>\n")
            html_file.write("\t<tr>\n")

            html_file.write("\t<tr>\n")
            html_file.write("\t\t<td>To: </td>\n")
            html_file.write("\t\t<td>" + self.getTo() + "</td>\n")
            html_file.write("\t<tr>\n")

            html_file.write("\t<tr>\n")
            html_file.write("\t\t<td>Subject: </td>\n")
            html_file.write("\t\t<td>" + self.getSubject() + "</td>\n")
            html_file.write("\t<tr>\n")

            html_file.write("\t<tr>\n")
            html_file.write("\t\t<td>Date: </td>\n")
            html_file.write("\t\t<td>" + self.getDate() + "</td>\n")
            html_file.write("\t<tr>\n")

            #Information in Table if Attachments
            if len(attachments) > 0:
                html_file.write("\t<tr>\n")
                html_file.write("\t\t<td>Attachments: </td><td>")
                for attachment in attachments:
                    html_file.write("<a href=\"" + attachment[0] + "\">" + cgi.escape(encode_string(str(attachment[1]), None)) + "</a>")
                    if attachment is not attachments[-1]:
                        html_file.write(", ")
                html_file.write("</td>\n")
                html_file.write("\t<tr>\n")

            html_file.write("</table>\n")
            html_file.write("<div class=\"col-md-8 col-md-offset-1 footer\"> <hr /><div style=\"white-space: pre-wrap;\">")
            #Write content to File
            check, content_of_mail = self.getContent()
            if content_of_mail['text']:
                html_file.write("<pre>")
                strip_header = re.sub(r"(?i)<html>.*?<head>.*?</head>.*?<body>", "", content_of_mail['text'],
                                  flags=re.DOTALL)
                strip_header = re.sub(r"(?i)</body>.*?</html>", "", strip_header, flags=re.DOTALL)
                strip_header = re.sub(r"(?i)<!DOCTYPE.*?>", "", strip_header, flags=re.DOTALL)
                strip_header = re.sub(r"(?i)POSITION: absolute;", "", strip_header, flags=re.DOTALL)
                strip_header = re.sub(r"(?i)TOP: .*?;", "", strip_header, flags=re.DOTALL)
                html_file.write(strip_header)
                html_file.write("</pre>\n")

            if content_of_mail['html']:
                strip_header = re.sub(r"(?i)<html>.*?<head>.*?</head>.*?<body>", "", content_of_mail['html'],
                                  flags=re.DOTALL)
                strip_header = re.sub(r"(?i)</body>.*?</html>", "", strip_header, flags=re.DOTALL)
                strip_header = re.sub(r"(?i)<!DOCTYPE.*?>", "", strip_header, flags=re.DOTALL)
                strip_header = re.sub(r"(?i)POSITION: absolute;", "", strip_header, flags=re.DOTALL)
                strip_header = re.sub(r"(?i)TOP: .*?;", "", strip_header, flags=re.DOTALL)
                html_file.write(strip_header)

            #HTML-Footer
            #html_file.write("</div> <div class=\"col-md-8 col-md-offset-1 footer\"> <hr /><div style=\"white-space: pre-wrap;\">")
            #html_file.write(lazy_mail.getHeader())
            html_file.write("</div></div></body></html>")

        except Exception as e:
            logging.error("Could not write HTML because of %s", e)
            raise e

        return check
Пример #2
0
    def saveAttachmentsToHardDisk(self, folder):
        attachments_tuple_for_html = []
        filename_count = dict()  #to handle attachments with same name
        successful = True
        for part in self.getParsedMail().walk():
            attachment_filename = "(Could not encode)"
            attachment_filename_encoding = None

            try:
                content_maintype = part.get_content_maintype()

                if content_maintype == 'multipart' or content_maintype == 'text' or content_maintype == 'html':
                    continue

                if part.get('Content-Disposition') == None:
                    continue

                try:
                    attachment_filename = decode_header(part.get_filename())[0][0]
                    attachment_filename_encoding = decode_header(part.get_filename())[0][1]
                except Exception as e:
                    logging.debug("Workaround Filename Encoding")
                    logging.debug(str(part))
                    try:
                        attachment_filename = encode_string(part.get_filename(), None)  #"(could not encode filename)"
                        logging.debug(attachment_filename)
                    except:
                        logging.error("Could not encode filename, %s", e)
                        attachment_filename = "(Could not encode)"
                        attachment_filename_encoding = None
                        successful = False

                if not attachment_filename:
                    logging.warning("Empty part in mail. Don't know what to do with it!")
                    logging.debug(str(part))
                    continue

                #put a (x) behind filename if same filename already exists
                if attachment_filename in filename_count:
                    filename_count[attachment_filename] = filename_count[attachment_filename] + 1
                    logging.debug("Same Filename %s", attachment_filename)
                    root, ext = os.path.splitext(attachment_filename)
                    attachment_filename = root + "(" + str(filename_count[attachment_filename]) + ")" + ext

                else:
                    filename_count[attachment_filename] = 1

                attachment_folder_name = os.path.join("attachments", self.getHashcode(), "")
                attachment_folder_path = os.path.join(folder, attachment_folder_name)
                attachments_tuple_for_html += [(attachment_folder_name + attachment_filename, cgi.escape(
                    encode_string(attachment_filename, attachment_filename_encoding)))]  #TODO

                if not os.path.exists(attachment_folder_path):
                    os.makedirs(attachment_folder_path)

                attachment_path = attachment_folder_path + attachment_filename

                attachment_file_disk = open(attachment_path, "wb")
                attachment_file_disk.write(part.get_payload(decode=True))
                logging.info("Saved attachment %s to %s", attachment_filename, attachment_path)
                self.STATS.succesfully_safed_attachment()
            except Exception as e:
                successful = False
                self.STATS.failed_to_safe_attachment()
                logging.error("Failed to save attachment %s: %s", attachment_filename, e)

        return successful, attachments_tuple_for_html