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--
Related
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.
Having a weird problem with emails I am sending out via Python email / smtplib.
I am attempting to compose an email with:
Alternatives of plain-text and HTML message bodies
An image embedded inline in the HTML body
A separate non-inline attachment
The MIME structure is setup like this:
multipart/mixed
multipart/alternative
text/plain
multipart/related
text/html
image/png - inline
application/pdf - attachment
This seems to work fine on every mail client I've tested {BlueMail on Android, iOS mail client, Roundcube} except for the Windows 10 mail client. For some reason, the Windows 10 built-in mail client seems to show the inline image just fine, but shows no trace of the other attachment.
The limited information I have been able to find on the internet points to this being a bug with the Windows 10 mail client, but I have personally received other emails in this client with both inline and attached attachments, which are displayed just fine - so there obviously is some sort of workaround / alternative message structure that works.
My question is thus: How can I format this message differently so that it will show up properly in all relevant mail clients?
I am composing the email like this, in Python:
message = MIMEMultipart("mixed")
message["From"] = ...
.
.
.
bodyText = "..."
bodyHTML = "..."
mailFrom = "..."
targetEmail = "..."
imageContent = ...
messageBody = MIMEMultipart("alternative")
messageBody.attach(MIMEText(bodyText, "plain"))
messageBodyHTML = MIMEMultipart("related")
messageBodyHTML.attach(MIMEText(bodyHTML, "html"))
messageImage = MIMEImage(imageContent)
messageImage.add_header("Content-Disposition", 'inline; filename="..."')
messageImage.add_header("Content-ID", "<id used in html body>")
messageBodyHTML.attach(messageImage)
messageBody.attach(messageBodyHTML)
message.attach(messageBody)
attachment = MIMEApplication(fileContent, Name=fileName)
attachment.add_header("Content-Disposition", 'attachment; filename="..."')
message.attach(attachment)
self.smtplibSession.sendmail(mailSource, targetEmail, message.as_string())
Update: Here's the message data from Windows 10 mail (as output via the "save" feature - there's no way to view the original message raw data that I can find...)
MIME-Version: 1.0
Date: Thu, 30 May 2019 17:45:28 +0200
From: xxxxx <xxxxx>
Subject: xxxxx
Thread-Topic: xxxxx
To: "xxxxx" <xxxxx>
Content-Type: multipart/related;
boundary="_5D6C043C-FD42-42F9-B0E0-841DBFBA96D5_"
--_5D6C043C-FD42-42F9-B0E0-841DBFBA96D5_
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="utf-8"
<center><img src=3D"cid:embedded-image" alt=...
--_5D6C043C-FD42-42F9-B0E0-841DBFBA96D5_
Content-Type: image/png; name="embedded-image.png"
Content-ID: <embedded-image>
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename="embedded-image.png"
iVBORw0KGgoAAAAN...
--_5D6C043C-FD42-42F9-B0E0-841DBFBA96D5_--
I'm not sure if this is a result of saving the email from the app, or this is what the app is actually storing, but it seems that the Windows 10 Mail app is cutting out everything outside the multipart/related stanza - that is, it's only taking the chosen alternative and not storing anything else.
For comparison, I've found and exported an email that displayed properly, with an image, html, and attachment, but the format seems to be a lot simpler - that email consisted only of a multipart/mixed layer with text/html and an application/pdf attachment. That email used an external image referenced in the HTML, instead of embedding it in the message - I would like to avoid hosting the images in each email externally.
Unlike you, there was no problem with the attachment file, instead I've had problems in displaying inline images (Windows 10 Mail 16005.11629.20174.0).
Unfortunately, handling non-standard approaches in MIME messages correctly is a feature that is expected to have good email clients. Apparently Windows 10 Mail is not as "good" yet.
The structure I recommend you to use is:
multipart/mixed
├─── multipart/related
│ ├─── multipart/alternative
│ │ ├─── text/plain
│ │ └─── text/html
│ └─── image/png - inline image
└─── application/pdf - attachment
I've had no problems with this structure in the following clients.
Windows 10 Mail
Gmail Web & Android
Outlook Web & Android & Windows Desktop
Blue Mail Android
Roundcube Web
MailEnable Web
So, give the following code a try to see if it works for you.
message = MIMEMultipart("mixed")
message["From"] = ...
.
.
.
bodyText = "..."
bodyHTML = "..."
mailFrom = "..."
targetEmail = "..."
imageContent = ...
fileContent = ...
relatedBody = MIMEMultipart("related")
messageBody = MIMEMultipart("alternative")
messageBody.attach(MIMEText(bodyText, "plain"))
messageBody.attach(MIMEText(bodyHTML, "html"))
relatedBody.attach(messageBody)
messageImage = MIMEImage(imageContent)
messageImage.add_header("Content-Disposition", 'inline; filename="..."')
messageImage.add_header("Content-ID", "<id used in html body>")
relatedBody.attach(messageImage)
message.attach(relatedBody)
attachment = MIMEApplication(fileContent)
attachment.add_header("Content-Disposition", 'attachment; filename="..."')
message.attach(attachment)
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==--
I have a situation here regarding POST HTTP request:
(host and links used here are up and running)
POST /inventory-check.cgi HTTP/1.1
Host: www.joes-hardware.com
Accept-Encoding: identity
Content-Length: 7
Content-Type: text/plain
item=563
when I send the above request string to the Host, then server sends me weird stuffs (along with expected result)
HTTP/1.1 200 OK
Date: Thu, 31 Oct 2013 12:07:48 GMT
Server: Apache/2.2.22 (Unix) DAV/2 FrontPage/5.0.2.2635 mod_ssl/2.2.22 OpenSSL/1.0.1c
Transfer-Encoding: chunked
Content-Type: text/html
6b
<HTML><BODY>
<H1>Joe's Hardware Store Inventory Check</H1>
Yes! Item number 56 is in stock!
</BODY></HTML>
0
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>501 Method Not Implemented</title>
</head><body>
<h1>Method Not Implemented</h1>
<p>3 to /index.html not supported.<br />
</p>
<hr>
<address>Apache/2.2.22 (Unix) DAV/2 FrontPage/5.0.2.2635 mod_ssl/2.2.22 OpenSSL/1.0.1c Server at totty.temp.veriohosting.com Port 80</address>
</body></html>
I checked the request with urllib module in python and that gives me only the expected output (Here i have omitted response details)
<HTML><BODY>
<H1>Joe's Hardware Store Inventory Check</H1>
Yes! Item number 56 is in stock!
</BODY></HTML>
What am I missing??
Actully I am new to HTTP and have experience in c/c++/python...Any help will be appreciated.. thanks in advance
item=563 is 8 bytes, but you declare a Content-Length of 7. Therefore, the server sees two requests, one which is a valid HTTP request for item 56 and one which is an invalid HTTP request consisting of the character 3 only, and sends you two responses.
Your request lies about the content-length.
That said, it's still a bit odd what the server does with the additional character 3.
I've been doing a lot of work with gpg-mailgate lately to the point where I have it working the way I want. I ran across an issue today that I'm not sure what is going on.
Email coming in from an an exchange server hits mailgate and gets deilvered as unreadable junk.
h�hi�¤��u,fA�����#õ�YvΑ�+���u��U)
��x�'�'y��PԎ���|��]����J�ϧ�D���E�{���b#>_�I��r~+*X^�����GlĀ���+�S�p�W0h+�G��)B��MZW`0Dhc�+#�q�a8�7%���������,�H���v�L������X̢2�� J�sڒ��k��W�<�30^�YUw}I�в��/�91���x)��Q ��0D�a
Is what I get. No clue why.
Date: Sun, 22 Sep 2013 05:25:12 -0400
Subject: Exchange test
Thread-Topic: Exchange test
Thread-Index: Ac63daSA+YXi7iiQR4Wy3htLwjbA7A==
Message-ID: <E7E690AD15261D4CAC417490E3C654587B1D6C869C#mail.compassnetworkgroup.com>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
acceptlanguage: en-US
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
MIME-Version: 1.0
QW5vdGhlciB0ZXN0DQoNClNlbnQgZnJvbSBteSBBbmRyb2lkIHBob25lIHVzaW5nIFRvdWNoRG93
biAod3d3Lm5pdHJvZGVzay5jb20pDQo=
The headers don't look that out of the ordinary except for the TNEF additions. I've been writing the raw data to a log before mailgate even begins it's work just to track this down.
Here is the code for mailgate.
http://pastebin.com/ywSPQKij
I can add to this that I'm sending from a mobile client that is set to plain. If i do this from outlook set to plan it works. The headers differ:
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
That still doesn't answer why it outputs garbage thogh. Other emails coming through as base64, utf-8 are just fine.