[Kde-pim] Email parsing with KMime
Simon MORIN
simon.g.morin at gmail.com
Mon May 19 10:27:31 BST 2008
Hello everyone !
I am currently writing an IMAP client based on IMAP KIOSlave. Receiving emails
works very well, I have a problem when it comes to parse them.
I am using KMime to parse the received emails and I succeded in extracting
information like the subject and the sender but I did not find how to extract
attachments and message.
My parsing code :
-------------------------------------------------------------------------------------------------------------------
qDebug() << "#### BUFFER ####";
qDebug() << buffer; // It is filled with KIOSlaves/KIO::Job
qDebug() << "^^^^ BUFFER ^^^^";
KMime::Message msg;
msg.setContent(buffer);
msg.parse();
KMime::Headers::Subject *subject = msg.subject();
KMime::Headers::From *from = msg.from();
KMime::Headers::ContentType *contentType = msg.contentType();
KMime::Content *textContent = msg.textContent(); // also tried with msg.body()
qDebug() << "################################################";
qDebug() << "Subject : " << subject->asUnicodeString();
qDebug() << "From : " << from->asUnicodeString();
qDebug() << "ContentType : " << contentType->asUnicodeString();
qDebug() << "ContentList : " << msg.contents().count();
qDebug() << "isTopLevel : " << msg.isTopLevel();
qDebug() << "TextContent : " << textContent->decodedText();
KMime::Content::List list = msg.attachments();
foreach (KMime::Content *c, list) {
qDebug() << "attachments : ";
}
-------------------------------------------------------------------------------------------------------------------
The email I'm using as example (attachment has been truncated to clarify this
message, but I receive it entirely) :
-------------------------------------------------------------------------------------------------------------------
"Return-Path: <xxxxx at server.net>
X-Original-To: yyyyyyyy at server.fr
Delivered-To: yyyyyyyy at server.fr
Received: from swip.net (mailfe16.tele2.fr [212.247.155.236])
by ns1.in-cube.fr (Postfix) with ESMTP id 6956921C43F
for <yyyyyyyy at server.f>; Thu, 15 May 2008 11:14:10 +0200 (CEST)
X-Cloudmark-Score: 0.000000 []
X-Cloudmark-Analysis: v=1.0 c=1 a=pVnfisNvH1wA:15
a=BeU0tpR/o6fNtzzsb0tEYw==:17 a=CTZ-cyHp61CmNF8wezMA:9
a=dOOiiLsxfFU5VUS8jMEqp4PecNEA:4 a=TmLYuLy49JwA:10 a=FgQqJ79YvNMF2P38qboA:9
a=GeeuSLW9dhGAq
EcJSNzJUDCNPfoA:4 a=21jJXJW0LzcA:10 a=HXjIzolwW10A:10
Received: from [193.251.65.138] (account azertyuiop at tele2.fr HELO yoda)
by mailfe16.swip.net (CommuniGate Pro SMTP 5.1.13)
with ESMTPA id 197221082 for yyyyyyyy at server.f; Thu, 15 May 2008 11:13:44
+0200
From: Simon MORIN <xxxxx at server.net>
To: yyyyyyyy at server.f
Subject: Test avec PJ
Date: Thu, 15 May 2008 11:13:24 +0200
User-Agent: KMail/1.9.9
MIME-Version: 1.0
Content-Type: Multipart/Mixed;
boundary="Boundary-00=_16/KIrrn+Yjnjwa"
Message-Id: <200805151113.25183.xxxxx at server.net>
--Boundary-00=_16/KIrrn+Yjnjwa
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Test
Simon MORIN
--Boundary-00=_16/KIrrn+Yjnjwa
Content-Type: image/png;
name="atlantis2.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="atlantis2.png"
iVBORw0KGgoAAAANSUhEUgAABaAAAAOECAYAAABXTZbSAAAgAElEQVR4nOyd2ZasOq5FMblH/f8H
n8T1ELiTZSG5IYjINe+ou08GYBvjVlbjtm3zGwAAAAAAAAAAAAAAAAAwmf3dBQAAAAAAAAAAAAAA
AADAgTQAAAAAAAAAAAAAAAAAJYAATQAAAAAAAAAAAAAAACAJUAADQAAAAAAAAAAAAAAAGAJEEAD
.......
AAAAAAAA4BEE0AAAAAAAAACARxBAAwAAAAAAAAAeQQANAAAAAAAAAHgEATQAAAAAAAAA4BEE0AAA
AAAAAACARxBAAwAAAAAAAAAeQQANAAAAAAAAAHgEATQAAAAAAAAA4BEE0AAAAAAAAACARxBAAwAA
AAAAAAAeQQANAAAAAAAAAHjEfwBA5pMc4Xwc4AAAAABJRU5ErkJggg==
--Boundary-00=_16/KIrrn+Yjnjwa--
"
-------------------------------------------------------------------------------------------------------------------
The output of my code :
-------------------------------------------------------------------------------------------------------------------
######################################################
Debug: widgetmail_p.cpp ( result )# 86 : Subject : "Test avec PJ^M"
Debug: widgetmail_p.cpp ( result )# 87 : From : "Simon MORIN
<xxxxx at server.net>"
Debug: widgetmail_p.cpp ( result )# 88 : ContentType : "text/plain;
charset="US-ASCII""
Debug: widgetmail_p.cpp ( result )# 89 : ContentList : 0
Debug: widgetmail_p.cpp ( result )# 90 : isTopLevel : true
Debug: widgetmail_p.cpp ( result )# 91 : TextContent : ""
-------------------------------------------------------------------------------------------------------------------
As we can see, "Subject" is OK, "From" is OK and I'm not sure if "ContentType"
is OK. What bother me is that "ContentList" = 0, (should be 1)
and "TextContent" contains nothing.
What should I do to extract those two part of the email ?
Thank you very much for your answer.
Simon MORIN
_______________________________________________
KDE PIM mailing list kde-pim at kde.org
https://mail.kde.org/mailman/listinfo/kde-pim
KDE PIM home page at http://pim.kde.org/
More information about the kde-pim
mailing list