I'm writing a plugin in thunderbird using native messaging (following the ping pong example in python) to call a Delphi program to copy an e-mail locally as an ".eml" file. The problem I am facing seems to be the encoding. In addition, the resulting file contains double quotes ("") at the start and the end of the file as well as escaped double quotes (\"). I just want to have a 1 to 1 copy and not to change its content.
Example of a mail content:
"test"
€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ
éöàäèüâêû
However, in the file, it looks more like this:
\"test\"
€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“â€â€¢â€“—˜™š›œžŸ
éöà äèüâêû
I might have found the problem, which is explained here:
https://www.i18nqa.com/debug/utf8-debug.html
However, I do not really know how to adapt my code to solve this problem.
Thank you for your help!
Here is my background.js:
async function main() {
messenger.menus.create({
contexts : ["message_list"],
id: "copy@mail.lu",
onclick : passMsg,
title: messenger.i18n.getMessage("lang.menuTitle")
});
}
async function passMsg(OnClickData) {
if (OnClickData.selectedMessages && OnClickData.selectedMessages.messages.length > 0) {
let MessageHeader = OnClickData.selectedMessages.messages[0];
let raw = await messenger.messages.getRaw(MessageHeader.id);
let port=browser.runtime.connectNative("copymail");
port.onMessage.addListener((message) => {
port.disconnect();
});
port.postMessage(raw);
} else {
console.log("No message selected");
}
}
main();
Here is my Delphi code:
procedure WriteSTDInputToFile(const Filename: String);
var
Buffer: array [0 .. 3] of Byte;
msgLen: LongInt;
msg: UTF8String;
myFile: TextFile;
StdIn: THandleStream;
jsonValue: TJSONValue;
begin
StdIn := THandleStream.Create(GetStdHandle(STD_INPUT_HANDLE));
try
msgLen := 0;
if StdIn.Read(Buffer, SizeOf(msgLen)) > 0 then
msgLen := PLongInt(@Buffer)^;
if msgLen > 0 then
begin
SetLength(msg, msgLen);
StdIn.Read(PUTF8Char(msg)^, msgLen);
if msg <> '' then
begin
AssignFile(myFile, Filename, CP_UTF8);
ReWrite(myFile);
jsonValue := TJSONObject.ParseJSONValue(msg);
try
write(myFile, UTF8Encode(jsonValue.ToString));
finally
jsonValue.Free;
end;
CloseFile(myFile);
end;
end;
finally
if Assigned(StdIn) then
StdIn.Free;
end;
end;
Resulting file content:
"X-MDAV-Result: clean
X-MDAV-Processed: mail.test.lu, Wed, 28 Oct 2020 08:13:22 +0100
X-Spam-Processed: mail.test.lu, Wed, 28 Oct 2020 08:13:22 +0100
Return-path: <copy@mail.lu>
X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on MAIL01E
X-Spam-Level:
X-Spam-Status: No, score=0.7 required=10.0 tests=HTML_MESSAGE,MPART_ALT_DIFF
shortcircuit=no autolearn=disabled version=3.4.2
Authentication-Results: test.lu;
auth=pass (plain) smtp.auth=ascholtes@test.lu
Received: from [172.16.17.35] [(172.16.17.35)] by test.lu (172.31.3.6) with ESMTPSA id md50033234892.msg;
Wed, 28 Oct 2020 08:13:21 +0100
X-MDRemoteIP: 172.16.17.35
X-MDArrival-Date: Wed, 28 Oct 2020 08:13:21 +0100
X-Authenticated-Sender: ascholtes@test.lu
X-Rcpt-To: copy@mail.lu
X-MDRcpt-To: copy@mail.lu
X-Return-Path: copy@mail.lu
X-Envelope-From: copy@mail.lu
X-MDaemon-Deliver-To: ascholtes@test.lu
To: Ayuth Scholtes <copy@mail.lu>
From: Ayuth Scholtes <copy@mail.lu>
Subject: Test
Organization: CISS
Message-ID: <7eb36f7c-a7af-c272-c189-eded642c3e1c@test.lu>
Date: Wed, 28 Oct 2020 08:13:21 +0100
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101
Thunderbird/68.10.0
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary=\"------------6068A746223BB2C9F1771938\"
Content-Language: lb-LU
This is a multi-part message in MIME format.
--------------6068A746223BB2C9F1771938
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
|\"test\" â¬âÆââ¦â â¡Ëâ°Å â¹ÅŽâââââ¢ââËâ¢Å¡âºÅžŸ éöà äèüâêû|
--------------6068A746223BB2C9F1771938
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">
</head>
<body>
<pre class=\"lang-pascal s-code-block hljs delphi\"><code>\"test\"
â¬âÆââ¦â â¡Ëâ°Å â¹ÅŽâââââ¢ââËâ¢Å¡âºÅžŸ
éöà äèüâêû</code></pre>
</body>
</html>
--------------6068A746223BB2C9F1771938--
"