Monday, 31 May 2021

What should be the content type to set for a multipart email after parsing and making some changes to it?

I have a multipart email with all types of attachments ie. multiple email, plain text, pdf attachments, inline images and html too. After walking through the different parts of the multipart body and adding some text to the body of the main email, I wish to regenerate the whole email as an original. What should be the correct method to do that. Using python 3.6. Code snippet what I have tried is as follows:

mail_attached_bool = False
new_message1 = email.message.EmailMessage()
attached_bool = False

mhtml = 'Modified html variable'
mbody = 'Modified text variable'

# while parsing the multipart of the raw message: msg
if msg.is_multipart():
    for part in msg.walk():
        if part.get_content_type() == 'multipart/report':
            new_message.attach(mbody)
            if mhtml:            
                new_message.attach(mhtml)

            for rel1 in part.walk():
                if rel1.get_content_type() == 'message/delivery-status':
                    new_message.attach(rel1)
                if rel1.get_content_type() == 'text/rfc822-headers':
                    new_message.attach(rel1)

        if part.get_content_type() in ["multipart/related",
                                       "multipart/mixed"]:
            new_message1.set_type("multipart/related")
            if mhtml:
                new_message1.attach(mhtml)
                print(999999)
            elif mbody:
                if mbody == '':
                    mbody = MIMEText(warning_txt,'plain')
                new_message1.attach(mbody)

        for rel in part.walk():
            mail_attached_bool = False
            attached_bool = False
            print(rel.get_content_type(), '------------cccccccc')
            # other kinds of attachments
            cdispo = str(rel.get('Content-Disposition'))
            attach = re.compile('application/*')
            attachment = attach.search(rel.get_content_type())

            if rel.get_content_type() in ['message/rfc822',]:
                new_message1.set_type('multipart/related')
                print(rel.get_content_type(), '----------content type')
                mail_attached_bool = True
                attached_bool = True
                x += 1
            
            if rel.is_multipart() and rel.get_content_type() \
               not in \
               ["multipart/alternative",
                "message/rfc822"
               ]:
                new_message1.set_type(rel.get_content_type())

            # ignore the first html as its the mail body
            if rel.get_content_type() == "text/html" and cdispo=='None':
                i += 1
                if i == 1 and html_body:
                    continue
                print('i: ',i)
            # ignore the first plain text as its the mail body
            if rel.get_content_type() == "text/plain" and cdispo=='None':
                j += 1
                if j == 1 and text_body:
                    continue
                print('j: ',j)

            #--------------#                    
            if 1:#rel.get_content_type() != 'message/rfc822':#mail_attached_bool is False:
                # has mail content apart from body (ios)
                if rel.get_content_type() == "text/html":
                    new_message1.attach(rel)
                    print(rel.get_filename(),'-----   html  attached')

                if rel.get_content_type() == "text/plain" and \
                   rel.get('Content-Disposition') in [None, "inline"]:
                    new_message1.attach(rel)
                    print('---------------text attachment', 666666)

                if rel.get_content_type() in ['image/png',
                                              'image/jpeg',
                                              'image/jpg'] \
                                              or ('attachment' in cdispo) \
                                    or ('inline' in cdispo) or (attachment):

                    # inline images and text
                    if "inline" in cdispo and \
                       not rel.get_content_type() in [
                           "text/plain",
                       ] \
                           and not attached_bool:
                        attached_bool = True
                        new_message1.attach(rel)
                        
                    if attachment or "attachment" in cdispo and \
                       (not attached_bool) or cdispo == 'None':
                        new_message1.attach(rel)
                        attached_bool = True

                    elif cdispo == 'None' and (not attached_bool):
                        new_message1.attach(rel)
                        print('attaching here')

                if rel.get_content_type() in ['text/calendar']:
                    new_message1.attach(rel)

            if mail_attached_bool:
                new_message1.attach(rel)

        new_message.set_type('multipart/alternative')
        new_message.attach(new_message1)
        if new_message1:
            print('new_message1 exists')
            break

Then send the mail. When the mail is sent it is attaching the main mail body and its attachment 2 times in the new message object. Why does this happen? What is the correct content type to set for the new mail?



from What should be the content type to set for a multipart email after parsing and making some changes to it?

No comments:

Post a Comment