diff options
-rw-r--r-- | framework/src/domain/mime/mailtemplates.cpp | 37 | ||||
-rw-r--r-- | framework/src/domain/mime/tests/mailtemplatetest.cpp | 36 |
2 files changed, 57 insertions, 16 deletions
diff --git a/framework/src/domain/mime/mailtemplates.cpp b/framework/src/domain/mime/mailtemplates.cpp index 25ce1385..02e71bce 100644 --- a/framework/src/domain/mime/mailtemplates.cpp +++ b/framework/src/domain/mime/mailtemplates.cpp | |||
@@ -906,7 +906,7 @@ static KMime::Content *createAttachmentPart(const QByteArray &content, const QSt | |||
906 | return part; | 906 | return part; |
907 | } | 907 | } |
908 | 908 | ||
909 | static KMime::Content *createBodyPart(const QByteArray &body) { | 909 | static KMime::Content *createPlainBodyPart(const QByteArray &body) { |
910 | auto mainMessage = new KMime::Content; | 910 | auto mainMessage = new KMime::Content; |
911 | mainMessage->setBody(body); | 911 | mainMessage->setBody(body); |
912 | mainMessage->contentType(true)->setMimeType("text/plain"); | 912 | mainMessage->contentType(true)->setMimeType("text/plain"); |
@@ -920,6 +920,24 @@ static KMime::Content *createHtmlBodyPart(const QByteArray &body) { | |||
920 | return mainMessage; | 920 | return mainMessage; |
921 | } | 921 | } |
922 | 922 | ||
923 | static KMime::Content *createBodyPart(const QByteArray &body, bool htmlBody) { | ||
924 | if (htmlBody) { | ||
925 | auto bodyPart = new KMime::Content; | ||
926 | bodyPart->contentType(true)->setMimeType("multipart/alternative"); | ||
927 | bodyPart->contentType()->setBoundary(KMime::multiPartBoundary()); | ||
928 | |||
929 | QTextDocument doc; | ||
930 | doc.setHtml(body); | ||
931 | |||
932 | bodyPart->addContent(createPlainBodyPart(doc.toPlainText().toUtf8())); | ||
933 | bodyPart->addContent(createHtmlBodyPart(body)); | ||
934 | return bodyPart; | ||
935 | } else { | ||
936 | return createPlainBodyPart(body); | ||
937 | } | ||
938 | return nullptr; | ||
939 | } | ||
940 | |||
923 | static KMime::Types::Mailbox::List stringListToMailboxes(const QStringList &list) | 941 | static KMime::Types::Mailbox::List stringListToMailboxes(const QStringList &list) |
924 | { | 942 | { |
925 | KMime::Types::Mailbox::List mailboxes; | 943 | KMime::Types::Mailbox::List mailboxes; |
@@ -975,25 +993,12 @@ KMime::Message::Ptr MailTemplates::createMessage(KMime::Message::Ptr existingMes | |||
975 | bodyPart->contentType()->setBoundary(KMime::multiPartBoundary()); | 993 | bodyPart->contentType()->setBoundary(KMime::multiPartBoundary()); |
976 | bodyPart->contentTransferEncoding()->setEncoding(KMime::Headers::CE7Bit); | 994 | bodyPart->contentTransferEncoding()->setEncoding(KMime::Headers::CE7Bit); |
977 | bodyPart->setPreamble("This is a multi-part message in MIME format.\n"); | 995 | bodyPart->setPreamble("This is a multi-part message in MIME format.\n"); |
978 | //TODO deal with html | 996 | bodyPart->addContent(createBodyPart(body.toUtf8(), htmlBody)); |
979 | bodyPart->addContent(createBodyPart(body.toUtf8())); | ||
980 | for (const auto &attachment : attachments) { | 997 | for (const auto &attachment : attachments) { |
981 | bodyPart->addContent(createAttachmentPart(attachment.data, attachment.filename, attachment.isInline, attachment.mimeType, attachment.name)); | 998 | bodyPart->addContent(createAttachmentPart(attachment.data, attachment.filename, attachment.isInline, attachment.mimeType, attachment.name)); |
982 | } | 999 | } |
983 | } else { | 1000 | } else { |
984 | if (htmlBody) { | 1001 | bodyPart = createBodyPart(body.toUtf8(), htmlBody); |
985 | bodyPart = new KMime::Content; | ||
986 | bodyPart->contentType(true)->setMimeType("multipart/alternative"); | ||
987 | bodyPart->contentType()->setBoundary(KMime::multiPartBoundary()); | ||
988 | |||
989 | QTextDocument doc; | ||
990 | doc.setHtml(body); | ||
991 | |||
992 | bodyPart->addContent(createBodyPart(doc.toPlainText().toUtf8())); | ||
993 | bodyPart->addContent(createHtmlBodyPart(body.toUtf8())); | ||
994 | } else { | ||
995 | bodyPart = createBodyPart(body.toUtf8()); | ||
996 | } | ||
997 | } | 1002 | } |
998 | bodyPart->assemble(); | 1003 | bodyPart->assemble(); |
999 | 1004 | ||
diff --git a/framework/src/domain/mime/tests/mailtemplatetest.cpp b/framework/src/domain/mime/tests/mailtemplatetest.cpp index 0ea5305f..b0d91182 100644 --- a/framework/src/domain/mime/tests/mailtemplatetest.cpp +++ b/framework/src/domain/mime/tests/mailtemplatetest.cpp | |||
@@ -14,6 +14,16 @@ | |||
14 | #include "mailtemplates.h" | 14 | #include "mailtemplates.h" |
15 | #include "mailcrypto.h" | 15 | #include "mailcrypto.h" |
16 | 16 | ||
17 | static KMime::Content *getSubpart(KMime::Content *msg, const QByteArray &mimeType) | ||
18 | { | ||
19 | for (const auto c : msg->contents()) { | ||
20 | if (c->contentType(false)->mimeType() == mimeType) { | ||
21 | return c; | ||
22 | } | ||
23 | } | ||
24 | return nullptr; | ||
25 | } | ||
26 | |||
17 | static std::vector< GpgME::Key, std::allocator< GpgME::Key > > getKeys(bool smime = false) | 27 | static std::vector< GpgME::Key, std::allocator< GpgME::Key > > getKeys(bool smime = false) |
18 | { | 28 | { |
19 | QGpgME::KeyListJob *job = nullptr; | 29 | QGpgME::KeyListJob *job = nullptr; |
@@ -276,6 +286,32 @@ private slots: | |||
276 | const auto contents = result->contents(); | 286 | const auto contents = result->contents(); |
277 | //1 Plain + 2 Attachments | 287 | //1 Plain + 2 Attachments |
278 | QCOMPARE(contents.size(), 3); | 288 | QCOMPARE(contents.size(), 3); |
289 | auto p = getSubpart(result.data(), "text/plain"); | ||
290 | QVERIFY(p); | ||
291 | } | ||
292 | |||
293 | void testCreateHtmlMailWithAttachments() | ||
294 | { | ||
295 | QStringList to = {{"to@example.org"}}; | ||
296 | QStringList cc = {{"cc@example.org"}};; | ||
297 | QStringList bcc = {{"bcc@example.org"}};; | ||
298 | KMime::Types::Mailbox from; | ||
299 | from.fromUnicodeString("from@example.org"); | ||
300 | QString subject = "subject"; | ||
301 | QString body = "body"; | ||
302 | QList<Attachment> attachments = {{"name", "filename", "mimetype", true, "inlineAttachment"}, {"name", "filename", "mimetype", false, "nonInlineAttachment"}}; | ||
303 | |||
304 | auto result = MailTemplates::createMessage({}, to, cc, bcc, from, subject, body, true, attachments); | ||
305 | |||
306 | QVERIFY(result); | ||
307 | QCOMPARE(result->subject()->asUnicodeString(), subject); | ||
308 | QVERIFY(result->contentType()->isMimeType("multipart/mixed")); | ||
309 | QVERIFY(result->date(false)->dateTime().isValid()); | ||
310 | const auto contents = result->contents(); | ||
311 | //1 alternative + 2 Attachments | ||
312 | QCOMPARE(contents.size(), 3); | ||
313 | auto p = getSubpart(result.data(), "multipart/alternative"); | ||
314 | QVERIFY(p); | ||
279 | } | 315 | } |
280 | 316 | ||
281 | void testCreatePlainMailSigned() | 317 | void testCreatePlainMailSigned() |