balena / python-smime

S/MIME encoder in pure python
Apache License 2.0
18 stars 17 forks source link

Multipart Emails #15

Closed mushadow closed 3 years ago

mushadow commented 3 years ago

Having issues encrypting multipart emails with Python 2.7

Traceback (most recent call last): File "buildExampleDjangoEmail.py", line 57, in encrypted_email = smime.encrypt(email.message().as_string(), pem_key) File "c:\Python27\lib\site-packages\smime\encrypt.py", line 42, in encrypt to_encode = MIMEText(msg.get_payload()) File "c:\Python27\lib\email\mime\text.py", line 30, in init self.set_payload(_text, _charset) File "c:\Python27\lib\email\message.py", line 226, in set_payload self.set_charset(charset) File "c:\Python27\lib\email\message.py", line 268, in set_charset cte(self) File "c:\Python27\lib\email\encoders.py", line 73, in encode_7or8bit orig.encode('ascii') AttributeError: 'list' object has no attribute 'encode'

mushadow commented 3 years ago

I found if you edit out the "to_encode = MIMEText(msg.get_payload())" line and just pass the message string straight to the contents the multipart will work.

smime/encrypt.py:29

def encrypt(message, certs, algorithm='aes256_cbc'):
    """
    Takes the contents of the message parameter, formatted as in RFC 2822, and encrypts them,
    so that they can only be read by the intended recipient specified by pubkey.
    :return: string containing the new encrypted message.
    """
    # Get the chosen block cipher
    block_cipher = get_cipher(algorithm)
    if block_cipher == None:
        raise ValueError('Unknown block algorithm')

    # Get the message content
    msg = message_from_string(message)
    #to_encode = MIMEText(msg.get_payload())
    #content = to_encode .as_string()
    content = msg.as_string()

    # Generate the recipient infos