示例#1
0
def convert_html_to_doc(html, filename, conference):
    response = http.HttpResponse()
    response['Content-Type'] = 'application/msword'
    response[
        'Content-Disposition'] = 'attachment; filename=' + filename + '.doc'

    if conference.logo:
        image_filepath = "media/" + os.path.basename(conference.logo.url)
        html = html.replace(settings.MEDIA_URL + "/" + conference.logo.url,
                            image_filepath)

    # pack the html and image into a MIME file
    doc = MIMEMultipart('related')
    part1 = MIMEBase('text', 'html')
    part1.set_payload(base64.encodestring(html.encode("UTF-8")), "utf-8")
    part1.add_header("Content-Location", "file:///C:/" + filename + ".htm")
    part1.replace_header("Content-Transfer-Encoding", "base64")
    doc.attach(part1)

    if conference.logo:
        conference.logo.open("rb")
        image_str = conference.logo.read()
        conference.logo.close()
        mimetype = mimetypes.guess_type(conference.logo.url)[0].split('/')

        part2 = MIMEBase(mimetype[0], mimetype[1])
        part2.set_payload(base64.encodestring(image_str))
        part2.add_header("Content-Location", "file:///C:/" + image_filepath)
        part2.add_header("Content-Transfer-Encoding", "base64")

        doc.attach(part2)

    response.content = doc.as_string()
    return response
 def set_headers(message: MIMEBase, headers: Dict[str, str]):
     for hdr_name, hdr_value in headers.items():
         if message[hdr_name]:
             message.replace_header(hdr_name, hdr_value)
         else:
             message.add_header(hdr_name, hdr_value)
     return message
示例#3
0
def convert_html_to_doc(html, filename, conference):
    response = http.HttpResponse()
    response['Content-Type'] ='application/msword'
    response['Content-Disposition'] = 'attachment; filename=' + filename + '.doc'

    if conference.logo:
        image_filepath = "media/" + os.path.basename(conference.logo.url)
        html = html.replace(settings.MEDIA_URL + "/" + conference.logo.url, image_filepath)
    
    # pack the html and image into a MIME file 
    doc = MIMEMultipart('related')
    part1 = MIMEBase('text', 'html')
    part1.set_payload(base64.encodestring(html.encode("UTF-8")),"utf-8")
    part1.add_header("Content-Location", "file:///C:/" + filename + ".htm")
    part1.replace_header("Content-Transfer-Encoding", "base64")
    doc.attach(part1)

    if conference.logo:
        conference.logo.open("rb")
        image_str = conference.logo.read()
        conference.logo.close()
        mimetype = mimetypes.guess_type(conference.logo.url)[0].split('/')
    
        part2 = MIMEBase(mimetype[0], mimetype[1])
        part2.set_payload(base64.encodestring(image_str))
        part2.add_header("Content-Location", "file:///C:/" + image_filepath)
        part2.add_header("Content-Transfer-Encoding", "base64")
    
        doc.attach(part2)
    
    response.content = doc.as_string()
    return response
示例#4
0
def sendArf(item, spam=False):
    global reportSender
    global mailSmtp
    global reportEmailCc
    global reportEmailSpamCc

    msg = MIMEBase('multipart', 'report')
    msg.set_param('report-type', 'feedback-report', requote=False)

    msg["To"] = str(item['emailAbuse'])
    msg["From"] = reportSender
    msg["Subject"] = "Abuse report for: " + str(item['subject'])

    if spam:
        text = "This is an email in the abuse report format (ARF) for an email message coming via these \r\n"
        text = text + "IPs " + str(item['sourceIp']) + " on " + str(
            item['arrivalDate']) + ".\r\n"
        text = text + "This report indicates that the attached email was not wanted by the recipient.\r\n"
        text = text + "This report may indicates a compromised machine and may contain URLs to malware, treat with caution!\r\n\r\n"
        text = text + "This ARF report contains all the information you will need to assess the problem.\r\n"
        text = text + "The zip attachment is the complete email encrypted with the password " + str(
            arfPassword) + "\r\n"
        text = text + "For more information about this format please see http://tools.ietf.org/html/rfc5965.\r\n"
    else:
        text = "This is an email in the abuse report format (ARF) for an email message received from \r\n"
        text = text + "IP " + str(item['sourceIp']) + " " + str(
            item['sourceDomain']) + " on " + str(
                item['arrivalDate']) + " UTC.\r\n"
        text = text + "This report likely indicates a compromised machine and may contain URLs to malware, treat with caution!\r\n\r\n"
        text = text + "The attached email was selected amongst emails that failed DMARC,\r\n"
        text = text + "therefore it indicates that the author tried to pass for someone else\r\n"
        text = text + "indicating fraud and not spam. The faster you fix or isolate the compromised machine, \r\n"
        text = text + "the better you protect your customers or members and the Internet at large.\r\n\r\n"
        text = text + "This ARF report contains all the information you will need to assess the problem.\r\n"
        text = text + "The zip attachment is the complete email encrypted with the password " + str(
            arfPassword) + "\r\n"
        text = text + "For more information about this format please see http://tools.ietf.org/html/rfc5965.\r\n"

    msgtxt = MIMEText(text)
    msg.attach(msgtxt)

    msgreport = MIMEBase('message', "feedback-report")
    msgreport.set_charset("US-ASCII")

    if spam:
        text = "Feedback-Type: abuse\r\n"
    else:
        text = "Feedback-Type: fraud\r\n"
    text = text + "User-Agent: pyforensic/1.1\r\n"
    text = text + "Version: 1.0\r\n"
    if not spam:
        text = text + "Source-IP: " + str(item['sourceIp']) + "\r\n"
    else:
        ipList = item['sourceIp'].split(", ")
        for ip in ipList:
            text = text + "Source-IP: " + str(ip) + "\r\n"

    text = text + "Arrival-Date: " + str(item['arrivalDate']) + " UTC\r\n"

    text = text + "Attachment-Password: "******"\r\n"

    if 'urlList' in item:
        for uri in item['urlList']:
            o = urlparse.urlparse(uri)
            urlReport = True
            if o.hostname is not None:
                for domain in wldomain:
                    if o.hostname[-len(domain):] == domain:
                        urlReport = False
                if urlReport == True:
                    text = text + "Reported-Uri: " + str(uri) + "\r\n"

    msgreport.set_payload(text)
    msg.attach(msgreport)

    #msgrfc822 = MIMEBase('message', "rfc822")
    msgrfc822 = MIMEBase('text', "rfc822-headers")
    msgrfc822.add_header('Content-Disposition', 'inline')
    parts = re.split(r'\r\n\r\n|\n\n', item['content'])
    rfc822headers = parts[0]
    #msgrfc822.set_payload(item['content'])
    msgrfc822.set_payload(rfc822headers)

    msg.attach(msgrfc822)

    #prepare the zip encrypted
    temp = tempfile.NamedTemporaryFile(prefix='mail',
                                       suffix='.eml',
                                       delete=False)
    tempname = temp.name
    temp.write(item['content'])
    temp.flush()
    ziptemp = tempfile.NamedTemporaryFile(prefix='mail',
                                          suffix='.zip',
                                          delete=True)
    ziptempname = ziptemp.name
    ziptemp.close()
    workdir = os.path.dirname(ziptempname)
    filenamezip = os.path.basename(ziptempname)
    filenameemail = os.path.basename(tempname)
    os.chdir(workdir)
    option = '-P%s' % arfPassword
    rc = subprocess.call(['zip', option] + [filenamezip, filenameemail])
    temp.close()

    ziptemp = open(ziptempname, "r")
    msgzip = MIMEBase('application', "zip")
    msgzip.set_payload(ziptemp.read())
    encoders.encode_base64(msgzip)
    msgzip.add_header('Content-Disposition',
                      'attachment',
                      filename=filenamezip)
    ziptemp.close()

    msg.attach(msgzip)

    #delete created files
    os.remove(ziptempname)
    os.remove(tempname)

    #print "******************\r\n"
    #print msg.as_string()
    #print "******************\r\n"

    s = smtplib.SMTP(mailSmtp)
    # send to IP owners first
    if msg["To"] != "":
        toList = msg["To"].split(",")
        s.sendmail(msg["From"], toList, msg.as_string())
    # send a copy
    reportEmail = reportEmailCc
    if spam:
        reportEmail = reportEmailSpamCc
    if reportEmail != "":
        toList = reportEmail.split(",")
        for emailAddress in toList:
            if msg.has_key("To"):
                msg.replace_header("To", str(emailAddress))
            else:
                msg["To"] = str(emailAddress)
            s.sendmail(msg["From"], emailAddress, msg.as_string())

    s.quit()
示例#5
0
    def _message(self):
        """Creates the email"""
        ascii_attachments = current_app.extensions['mail'].ascii_attachments
        encoding = self.charset or 'utf-8'

        attachments = self.attachments or []

        if len(attachments) == 0 and not self.alts:
            # No html content and zero attachments means plain text
            msg = self._mimetext(self.body, self.subtype)
        elif len(attachments) > 0 and not self.alts:
            # No html and at least one attachment means multipart
            subtype = self.subtype or 'mixed'
            msg = MIMEMultipart(_subtype=subtype)
            msg.attach(self._mimetext(self.body))
        else:
            # Anything else
            subtype = self.subtype or 'mixed'
            msg = MIMEMultipart(_subtype=subtype)
            alternative = MIMEMultipart(_subtype='alternative')
            alternative.attach(self._mimetext(self.body))
            for mimetype, content in self.alts.items():
                alternative.attach(self._mimetext(content, mimetype))
            msg.attach(alternative)

        if self.subject:
            msg['Subject'] = sanitize_subject(force_text(self.subject),
                                              encoding)

        msg['From'] = sanitize_address(self.sender, encoding)
        msg['To'] = ', '.join(
            list(set(sanitize_addresses(self.recipients, encoding)))
        )

        msg['Date'] = formatdate(self.date, localtime=True)
        # see RFC 5322 section 3.6.4.
        msg['Message-ID'] = self.msgId

        if self.cc:
            msg['Cc'] = ', '.join(
                list(set(sanitize_addresses(self.cc, encoding)))
            )

        if self.reply_to:
            msg['Reply-To'] = sanitize_address(self.reply_to, encoding)

        if self.extra_headers:
            for k, v in self.extra_headers.items():
                msg[k] = v

        SPACES = re.compile(r'[\s]+', re.UNICODE)
        for attachment in attachments:
            f = MIMEBase(*attachment.content_type.split('/'))
            f.set_payload(attachment.data)
            encode_base64(f)

            filename = attachment.filename
            if filename and ascii_attachments:
                # force filename to ascii
                filename = unicodedata.normalize('NFKD', filename)
                filename = filename.encode('ascii', 'ignore').decode('ascii')
                filename = SPACES.sub(u' ', filename).strip()

            try:
                filename and filename.encode('ascii')
            except UnicodeEncodeError:
                if not PY3:
                    filename = filename.encode('utf8')
                filename = ('UTF8', '', filename)

            f.add_header('Content-Disposition',
                         attachment.disposition,
                         filename=filename)

            for key, value in attachment.headers.items():
                f.add_header(key, value)

            if attachment.content_id:
                try:
                    f.replace_header('Content-ID', attachment.content_id)
                except KeyError:
                    f.add_header('Content-ID', attachment.content_id)

            msg.attach(f)
        if message_policy:
            msg.policy = message_policy

        return msg
示例#6
0
def dict_to_email(messagedict):
    messagedict = copy.deepcopy(messagedict)
    extra_attrs = {}
    if settings.CELERY_EMAIL_MESSAGE_EXTRA_ATTRIBUTES:
        for attr in settings.CELERY_EMAIL_MESSAGE_EXTRA_ATTRIBUTES:
            if attr in messagedict:
                extra_attrs[attr] = messagedict.pop(attr)

    attachments = messagedict.pop('attachments')
    attachment_headers = messagedict.pop('attachment_headers')

    if isinstance(messagedict, dict) and "content_subtype" in messagedict:
        content_subtype = messagedict["content_subtype"]
        del messagedict["content_subtype"]
    else:
        content_subtype = None
    if isinstance(messagedict, dict) and "mixed_subtype" in messagedict:
        mixed_subtype = messagedict["mixed_subtype"]
        del messagedict["mixed_subtype"]
    else:
        mixed_subtype = None

    if hasattr(messagedict, 'from_email'):
        ret = messagedict
    elif 'alternatives' in messagedict:
        ret = EmailMultiAlternatives(**messagedict)
    else:
        ret = EmailMessage(**messagedict)

    if isinstance(ret, EmailMessage):
        # Properly build attachments with headers
        for index, attachment in enumerate(attachments):
            # Extract attachment params
            attachment_filename = attachment[0]
            attachment_mime = attachment[2]
            attachment_payload = base64.b64decode(
                attachment[1].encode('ascii'))

            if attachment_mime is None:
                attachment_type = 'application'
                attachment_subtype = 'octet-stream'
            else:
                attachment_type, attachment_subtype = attachment_mime.split(
                    '/')

            if str(index) in attachment_headers:

                # Create attachment object
                mime = MIMEBase(attachment_type, attachment_subtype)
                for header_key, header_value in attachment_headers[str(index)]:
                    try:
                        mime.replace_header(header_key, header_value)
                    except KeyError:
                        mime.add_header(header_key, header_value)

                mime.set_payload(
                    base64.b64encode(attachment_payload).decode('ascii'))

                # Assign attachment headers
                for header in attachment_headers[str(index)]:
                    header, header_value = header
                    try:
                        mime.replace_header(header, header_value)
                    except KeyError:
                        mime.add_header(header, header_value)

                try:
                    mime.replace_header('Content-Transfer-Encoding', 'base64')
                except KeyError:
                    mime.add_header('Content-Transfer-Encoding', 'base64')

                ret.attach(mime)
            else:
                ret.attach(attachment_filename, attachment_payload,
                           attachment_mime)
    else:
        for attachment in attachments:
            filename, contents, mimetype, headers = attachment
            binary_contents = base64.b64decode(contents.encode('ascii'))
            messagedict['attachments'].append(
                (filename, binary_contents, mimetype))

    for attr, val in extra_attrs.items():
        setattr(ret, attr, val)
    if content_subtype:
        ret.content_subtype = content_subtype
        messagedict[
            "content_subtype"] = content_subtype  # bring back content subtype for 'retry'
    if mixed_subtype:
        ret.mixed_subtype = mixed_subtype
        messagedict[
            "mixed_subtype"] = mixed_subtype  # bring back mixed subtype for 'retry'

    return ret
示例#7
0
def sendArf(item, spam=False):
    global reportSender
    global mailSmtp
    global reportEmailCc
    global reportEmailSpamCc

    msg = MIMEBase('multipart','report')
    msg.set_param('report-type','feedback-report',requote=False)

    msg["To"] = str(item['emailAbuse'])
    msg["From"] = reportSender
    msg["Subject"] = "Abuse report for: "+str(item['subject'])

    if spam:
        text = "This is an email in the abuse report format (ARF) for an email message coming via these \r\n"
        text = text+"IPs "+str(item['sourceIp'])+" on "+str(item['arrivalDate'])+".\r\n"
        text = text+"This report indicates that the attached email was not wanted by the recipient.\r\n"
        text = text+"This report may indicates a compromised machine and may contain URLs to malware, treat with caution!\r\n\r\n"
        text = text+"This ARF report contains all the information you will need to assess the problem.\r\n"
        text = text+"The zip attachment is the complete email encrypted with the password "+str(arfPassword)+"\r\n";
        text = text+"For more information about this format please see http://tools.ietf.org/html/rfc5965.\r\n";
    else:
        text = "This is an email in the abuse report format (ARF) for an email message received from \r\n"
        text = text+"IP "+str(item['sourceIp'])+" "+str(item['sourceDomain'])+" on "+str(item['arrivalDate'])+" UTC.\r\n"
        text = text+"This report likely indicates a compromised machine and may contain URLs to malware, treat with caution!\r\n\r\n"
        text = text+"The attached email was selected amongst emails that failed DMARC,\r\n"
        text = text+"therefore it indicates that the author tried to pass for someone else\r\n"
        text = text+"indicating fraud and not spam. The faster you fix or isolate the compromised machine, \r\n"
        text = text+"the better you protect your customers or members and the Internet at large.\r\n\r\n"
        text = text+"This ARF report contains all the information you will need to assess the problem.\r\n"
        text = text+"The zip attachment is the complete email encrypted with the password "+str(arfPassword)+"\r\n";
        text = text+"For more information about this format please see http://tools.ietf.org/html/rfc5965.\r\n";

    msgtxt = MIMEText(text)
    msg.attach(msgtxt)

    msgreport = MIMEBase('message', "feedback-report")
    msgreport.set_charset("US-ASCII")
    
    if spam:
        text = "Feedback-Type: abuse\r\n"
    else:
        text = "Feedback-Type: fraud\r\n"
    text = text + "User-Agent: pyforensic/1.1\r\n"
    text = text + "Version: 1.0\r\n"
    if not spam:
        text = text + "Source-IP: "+str(item['sourceIp'])+"\r\n"
    else:
        ipList = item['sourceIp'].split(", ")
        for ip in ipList:
            text = text + "Source-IP: "+str(ip)+"\r\n"

    text = text + "Arrival-Date: "+str(item['arrivalDate'])+" UTC\r\n"

    text = text + "Attachment-Password: "******"\r\n"

    if 'urlList' in item:
        for uri in item['urlList']:
            o = urlparse.urlparse(uri)
            urlReport=True
            if o.hostname is not None:
                for domain in wldomain:
                    if o.hostname[-len(domain):]==domain:
                        urlReport=False
                if urlReport==True:
                    text = text + "Reported-Uri: "+str(uri)+"\r\n"

    msgreport.set_payload(text)
    msg.attach(msgreport)

    #msgrfc822 = MIMEBase('message', "rfc822")
    msgrfc822 = MIMEBase('text', "rfc822-headers")
    msgrfc822.add_header('Content-Disposition','inline')
    parts=re.split(r'\r\n\r\n|\n\n',item['content'])
    rfc822headers=parts[0]
    #msgrfc822.set_payload(item['content'])
    msgrfc822.set_payload(rfc822headers)
    
    msg.attach(msgrfc822)

    #prepare the zip encrypted
    temp=tempfile.NamedTemporaryFile(prefix='mail',suffix='.eml',delete=False)
    tempname=temp.name
    temp.write(item['content'])
    temp.flush()
    ziptemp = tempfile.NamedTemporaryFile(prefix='mail',suffix='.zip',delete=True)
    ziptempname=ziptemp.name
    ziptemp.close()
    workdir = os.path.dirname(ziptempname)
    filenamezip = os.path.basename(ziptempname)
    filenameemail = os.path.basename(tempname)
    os.chdir(workdir)
    option = '-P%s' % arfPassword
    rc = subprocess.call(['zip', option] + [filenamezip, filenameemail])
    temp.close()

    
    ziptemp = open(ziptempname,"r")
    msgzip = MIMEBase('application', "zip")
    msgzip.set_payload(ziptemp.read())
    encoders.encode_base64(msgzip)
    msgzip.add_header('Content-Disposition', 'attachment', filename=filenamezip)
    ziptemp.close()

    msg.attach(msgzip)

    #delete created files
    os.remove(ziptempname)
    os.remove(tempname)


    #print "******************\r\n"
    #print msg.as_string()
    #print "******************\r\n"

    s = smtplib.SMTP(mailSmtp)
    # send to IP owners first
    if msg["To"] != "":
        toList = msg["To"].split(",")
        s.sendmail(msg["From"], toList, msg.as_string())
    # send a copy
    reportEmail=reportEmailCc
    if spam:
        reportEmail=reportEmailSpamCc
    if reportEmail != "":
        toList = reportEmail.split(",")
        for emailAddress in toList:
            if msg.has_key("To"):
                msg.replace_header("To",str(emailAddress))
            else:
                msg["To"]=str(emailAddress)
            s.sendmail(msg["From"], emailAddress, msg.as_string())
            
    s.quit()