开发者

python imaplib gpg file contents into message

开发者 https://www.devze.com 2023-03-31 00:58 出处:网络
Lo, Having some issues with imaplib. I\'m trying to get the contents of a gpg file into the body of an email.

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'] = 'blah@blah.com' newEmail['To'] = 'blah@blah.com' newEmail.set_payload(gpgStr+'\n') srv.append('INBOX', '', imaplib.Time2Internaldate(time.time()), str(newEmail))

When gpg开发者_Python百科Str 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消