I am trying to extract the attachments from an email mht file and write to separte mht files.I used the code in this link Extracting mht files to extract and write the attachments to a file.
Apparently I am unable to open the output mht files in the case of Content/Type: application/pdf and application/octet-stream. Below is the format of the output mht file which I am not able to open in IE. I am wondering why I am unable to open the file in IE. Any help or suggestions would be useful. Thanks.
MIME-Version: 1.0
Content-Type: multipart/related; type="text/html";
boundary="===============0805110039=="
--===============0805110039==
Content-Type: application/pdf
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-ID: <abcd.pdf>
<Payload>
--===============0805110039==--
Related
I'm trying to decode a base64 pdf file and send it to another endpoint.
I used a python policy for the decoding part and here's the code
import base64
pdfB64 = flow.getVariable("request.content")
pdfFile = base64.b64decode(pdfB64)
flow.setVariable("pdfFileDecoded",pdfFile)
Now, when I send my http post request which is below
headers :
Accept : */*
boundary : --Boundaryy
--Boundaryy
Content-Disposition: form-data; name="testdu12janvier"; filename="testdu12janvier.pdf"
Content-Type: application/pdf
<< Heres is sensitive data which is basically a base64 encoded pdf file >>
--Boundaryy--
When I send this POST request and trace it in Apigee Edge, I notice that something else is encoded before the pdf file I think its either the boundary or one of the headers. This makes a corrupt pdf file which can't be read.
How do I isolate the pdf file from the request body without removing boundaries? as I'll need to send multiple in near future.
How do I send an mtom message in python? I have tried using the requests library but I am getting an error from the server. The workflow is HTTP headers with a SOAP envelope and a binary attachment. I am using an MTOM template file which is structured as follows:
Content-Type: multipart/related;
boundary=boundary1234567890; type="application/xop+xml";
start="<0.urn:uuid:1FACEDB95C3509148F1570480012346#w3.org>"; start-info="text/xml"
Transfer-Encoding: chunked
SOAPAction: "soap-action"
--boundary1234567890
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <0.urn:uuid:1FACEDB95C3509148F1570480012346#w3.org>
set soap envelope here
--boundary1234567890
Content-Type: application/zip
Content-Transfer-Encoding: binary
Content-ID: <1.urn:uuid:1FACEDB95C3509148F1570480012347#w3.org>
set binary attachment here
I then send the message with the python requests library as follows:
import requests
data = open(template, 'rb').read()
response = requests.post(url, data=data, headers={})
However, I am getting an error from the server saying that my request was not able to be processed. I am probably doing something wrong, but I do not know how to send an MTOM message. Any help would be greatly appreciated.
I think the only real support for MTOM
is currently in Java and C#.You can use Axis2/C which I believe supports MTOM and
put a Python wrapper around it.
When I try to upload a CSV file and read them, the header content is also added to the csv at the top and at the end of the file :
----------------------------1323424324242342
Content-Disposition: form-data; name="file"; filename="test.csv"
Content-Type: text/csv
<actual content here>
----------------------------113131313331313133--
How do I get the actual content in the file and ignore the multipart headers?
Here is what I want to accomplish:
somebody (non-technical) prepares an html email and sends it to my gmail account.
I check the original email ("Show original" in the gmail interface) and extract the html part, saving it to a file original_gmail.html. This has CTE of quoted-printable (see below)
Using this file I prepare a MIME message which I can send with python.
My framework is already able of sending plain text and/or html mails, but I am unable to send this specially encoded html file. It looks like this in the "Show original" window:
--001a1133dfb0d17ec804f986e82c
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div><font color=3D"#073763" face=3D"arial,helvetica,sans-=
serif" style=3D"background-color:rgb(255,255,255)">Dear Family and dear fri=
ends, </font></div><div><div><font style=3D"background-color:rgb(255,255,25=
5)"><font color=3D"#0c343d" face=3D"arial,helvetica,sans-serif"></font><fon=
t color=3D"#073763">=C2=A0</font></font></div>
...
</div>
--001a1133dfb0d17ec804f986e82c--
(that is just the html part, there is another part with Content-Type: text/plain; charset=UTF-8
but I have no trouble with that)
How can I send this special quoted-printable section (verbatim) with python?
There is a module in the standard library just for that - quopri, which will do the decoding for you:
import quopri
txt = """--001a1133dfb0d17ec804f986e82c
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div><font color=3D"#073763" face=3D"arial,helvetica,sans-=
serif" style=3D"background-color:rgb(255,255,255)">Dear Family and dear fri=
ends, </font></div><div><div><font style=3D"background-color:rgb(255,255,25=
5)"><font color=3D"#0c343d" face=3D"arial,helvetica,sans-serif"></font><fon=
t color=3D"#073763">=C2=A0</font></font></div> ... </div>
--001a1133dfb0d17ec804f986e82c--"""
decoded = quopri.decodestring(txt)
print(decoded)
, which will output:
--001a1133dfb0d17ec804f986e82c
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir="ltr"><div><font color="#073763" face="arial,helvetica,sans-serif" style="background-color:rgb(255,255,255)">Dear Family and dear friends, </font></div><div><div><font style="background-color:rgb(255,255,255)"><font color="#0c343d" face="arial,helvetica,sans-serif"></font><font color="#073763"> </font></font></div> ... </div>
--001a1133dfb0d17ec804f986e82c--
I'm using email.Message class and gnupg library to:
1 - parse email string fetched from Twisted imap4 client.
2 - get the attached file, a .pgp
3 - decrypt it.
I can decrypt typical mail content, as:
-----BEGIN PGP MESSAGE-----
Version: PGP 9
(...)
-----END PGP MESSAGE-----
but the attachment affair is really making my life a hell.
Well, I tried a lot of different ways, but the most logical should be this:
message = email.message_from_string(content)
if message.is_multipart():
attachment = message.get_payload(1)
gpg = gnupg.GPG(gnupghome=settings.PGP_PATH)
return gpg.decrypt_file(attachment.get_payload(), passphrase=settings.PGP_PASSPH)
The attachment var first lines are:
From nobody Mon Oct 15 18:54:12 2012
Content-type: application/octet-stream;
name="No_Norm_AMLT_908_1210201201.txt.pgp"
Content-Disposition: attachment; filename="No_Norm_AMLT_908_1210201201.txt.pgp"
Content-Transfer-Encoding: base64
And then all the encrypted stuff.
Anyway... it's really strange, there's something I don't get. If I download the attachment from a normal mail client software (i.e. Thunderbird) I get a .pgp file that seems binary (strange characters appears if I edit it with a plain text editor) and I can decrypt it using the bash command:
gpg --decrypt No_Norm_AMLT_908_1210201201.txt.pgp
I don't know how to get the same result (the file decrypted) using email.Message class and gnupg, I tried to save the payload of the attachment to a file, and this is different from the downloaded from Thunderbird one, I can't decrypt it, I tried also to put it into a StringIO, and also encoding it with base64.
The message I get from gpg is:
[GNUPG:] NODATA 1
[GNUPG:] NODATA 2
gpg: decrypt_message failed: eof
Thank you!
Ok solved! I had to:
base64.decodestring(attachment.get_payload())
then decrypt it using gpg, and worked. This can be figured out because of the header:
Content-Transfer-Encoding: base64
The final code is:
message = email.message_from_string(content)
if message.is_multipart():
attachment = message.get_payload(1)
gpg = gnupg.GPG(gnupghome=settings.PGP_PATH)
return gpg.decrypt_file(base64.decodestring(attachment.get_payload()),
passphrase=settings.PGP_PASSPH)