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.
精彩评论