Sirex
Sirex

Reputation: 219

python imaplib gpg file contents into message

Lo,

Having some issues with imaplib. I'm trying to get the contents of a gpg file into the body of an email.

The encrypted file looks something like this:

ÕþëÂüÿΩfXаÕ庼H»[ßÖq«Ì5ßö

my code looks something like this:

gpgFH = open(gpgFile, 'rb')
gpgStr = gpgFH.read()

newEmail = email.message.Message() newEmail['Subject'] = 'blah' newEmail['From'] = '[email protected]' newEmail['To'] = '[email protected]' newEmail.set_payload(gpgStr+'\n') srv.append('INBOX', '', imaplib.Time2Internaldate(time.time()), str(newEmail))

When gpgStr is "hello" this works fine. When it is that encrypted jibberish, it dosent. I'm guessing unicode rears its ugly head at some point in the solution, but i'm struggling to make it work.

Upvotes: 1

Views: 220

Answers (1)

Seth
Seth

Reputation: 46413

Try base64-encoding the file's data. The above code is putting binary data into the email, which isn't going to work.

import base64

gpgFH = open(gpgFile, 'rb')
gpgStr = gpgFH.read()
gpgEncoded = base64.b64encode(gpgStr)
...

Alternatively, you might want to add the GPG data as an attachment instead of the body.

Upvotes: 2

Related Questions