From 00bcc137ab2d82d786f1b4423f3d3b4e5c30e71d Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sun, 8 Oct 2017 14:33:32 +0200 Subject: Html with attachments --- framework/src/domain/mime/mailtemplates.cpp | 37 ++++++++++++---------- .../src/domain/mime/tests/mailtemplatetest.cpp | 36 +++++++++++++++++++++ 2 files changed, 57 insertions(+), 16 deletions(-) (limited to 'framework/src/domain') 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 return part; } -static KMime::Content *createBodyPart(const QByteArray &body) { +static KMime::Content *createPlainBodyPart(const QByteArray &body) { auto mainMessage = new KMime::Content; mainMessage->setBody(body); mainMessage->contentType(true)->setMimeType("text/plain"); @@ -920,6 +920,24 @@ static KMime::Content *createHtmlBodyPart(const QByteArray &body) { return mainMessage; } +static KMime::Content *createBodyPart(const QByteArray &body, bool htmlBody) { + if (htmlBody) { + auto bodyPart = new KMime::Content; + bodyPart->contentType(true)->setMimeType("multipart/alternative"); + bodyPart->contentType()->setBoundary(KMime::multiPartBoundary()); + + QTextDocument doc; + doc.setHtml(body); + + bodyPart->addContent(createPlainBodyPart(doc.toPlainText().toUtf8())); + bodyPart->addContent(createHtmlBodyPart(body)); + return bodyPart; + } else { + return createPlainBodyPart(body); + } + return nullptr; +} + static KMime::Types::Mailbox::List stringListToMailboxes(const QStringList &list) { KMime::Types::Mailbox::List mailboxes; @@ -975,25 +993,12 @@ KMime::Message::Ptr MailTemplates::createMessage(KMime::Message::Ptr existingMes bodyPart->contentType()->setBoundary(KMime::multiPartBoundary()); bodyPart->contentTransferEncoding()->setEncoding(KMime::Headers::CE7Bit); bodyPart->setPreamble("This is a multi-part message in MIME format.\n"); - //TODO deal with html - bodyPart->addContent(createBodyPart(body.toUtf8())); + bodyPart->addContent(createBodyPart(body.toUtf8(), htmlBody)); for (const auto &attachment : attachments) { bodyPart->addContent(createAttachmentPart(attachment.data, attachment.filename, attachment.isInline, attachment.mimeType, attachment.name)); } } else { - if (htmlBody) { - bodyPart = new KMime::Content; - bodyPart->contentType(true)->setMimeType("multipart/alternative"); - bodyPart->contentType()->setBoundary(KMime::multiPartBoundary()); - - QTextDocument doc; - doc.setHtml(body); - - bodyPart->addContent(createBodyPart(doc.toPlainText().toUtf8())); - bodyPart->addContent(createHtmlBodyPart(body.toUtf8())); - } else { - bodyPart = createBodyPart(body.toUtf8()); - } + bodyPart = createBodyPart(body.toUtf8(), htmlBody); } bodyPart->assemble(); 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 @@ #include "mailtemplates.h" #include "mailcrypto.h" +static KMime::Content *getSubpart(KMime::Content *msg, const QByteArray &mimeType) +{ + for (const auto c : msg->contents()) { + if (c->contentType(false)->mimeType() == mimeType) { + return c; + } + } + return nullptr; +} + static std::vector< GpgME::Key, std::allocator< GpgME::Key > > getKeys(bool smime = false) { QGpgME::KeyListJob *job = nullptr; @@ -276,6 +286,32 @@ private slots: const auto contents = result->contents(); //1 Plain + 2 Attachments QCOMPARE(contents.size(), 3); + auto p = getSubpart(result.data(), "text/plain"); + QVERIFY(p); + } + + void testCreateHtmlMailWithAttachments() + { + QStringList to = {{"to@example.org"}}; + QStringList cc = {{"cc@example.org"}};; + QStringList bcc = {{"bcc@example.org"}};; + KMime::Types::Mailbox from; + from.fromUnicodeString("from@example.org"); + QString subject = "subject"; + QString body = "body"; + QList attachments = {{"name", "filename", "mimetype", true, "inlineAttachment"}, {"name", "filename", "mimetype", false, "nonInlineAttachment"}}; + + auto result = MailTemplates::createMessage({}, to, cc, bcc, from, subject, body, true, attachments); + + QVERIFY(result); + QCOMPARE(result->subject()->asUnicodeString(), subject); + QVERIFY(result->contentType()->isMimeType("multipart/mixed")); + QVERIFY(result->date(false)->dateTime().isValid()); + const auto contents = result->contents(); + //1 alternative + 2 Attachments + QCOMPARE(contents.size(), 3); + auto p = getSubpart(result.data(), "multipart/alternative"); + QVERIFY(p); } void testCreatePlainMailSigned() -- cgit v1.2.3