diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-08-02 16:52:45 -0600 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-08-02 16:52:45 -0600 |
commit | 64436e0787382d5c7fb3dae5b6128e1d93a77979 (patch) | |
tree | d3a1b18fc278f908a88373eba77c9187db924efa /framework/src/domain/mime/mailtemplates.cpp | |
parent | 105474a893bfc6b6cd9fb86cb7b6bb6e39bb23e0 (diff) | |
download | kube-64436e0787382d5c7fb3dae5b6128e1d93a77979.tar.gz kube-64436e0787382d5c7fb3dae5b6128e1d93a77979.zip |
Moved mailcomposing into mailtemplates
so we can start testing it.
Diffstat (limited to 'framework/src/domain/mime/mailtemplates.cpp')
-rw-r--r-- | framework/src/domain/mime/mailtemplates.cpp | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/framework/src/domain/mime/mailtemplates.cpp b/framework/src/domain/mime/mailtemplates.cpp index 71a267a8..ce1cd52f 100644 --- a/framework/src/domain/mime/mailtemplates.cpp +++ b/framework/src/domain/mime/mailtemplates.cpp | |||
@@ -34,6 +34,7 @@ | |||
34 | 34 | ||
35 | #include <KCodecs/KCharsets> | 35 | #include <KCodecs/KCharsets> |
36 | #include <KMime/Types> | 36 | #include <KMime/Types> |
37 | #include <KCodecs/KEmailAddress> | ||
37 | 38 | ||
38 | #include <mimetreeparser/objecttreeparser.h> | 39 | #include <mimetreeparser/objecttreeparser.h> |
39 | 40 | ||
@@ -885,3 +886,94 @@ QString MailTemplates::plaintextContent(const KMime::Message::Ptr &msg) | |||
885 | } | 886 | } |
886 | return plain; | 887 | return plain; |
887 | } | 888 | } |
889 | |||
890 | static KMime::Content *createAttachmentPart(const QByteArray &content, const QString &filename, bool isInline, const QByteArray &mimeType, const QString &name) | ||
891 | { | ||
892 | |||
893 | KMime::Content *part = new KMime::Content; | ||
894 | part->contentDisposition(true)->setFilename(filename); | ||
895 | if (isInline) { | ||
896 | part->contentDisposition(true)->setDisposition(KMime::Headers::CDinline); | ||
897 | } else { | ||
898 | part->contentDisposition(true)->setDisposition(KMime::Headers::CDattachment); | ||
899 | } | ||
900 | part->contentType(true)->setMimeType(mimeType); | ||
901 | part->contentType(true)->setName(name, "utf-8"); | ||
902 | //Just always encode attachments base64 so it's safe for binary data | ||
903 | part->contentTransferEncoding(true)->setEncoding(KMime::Headers::CEbase64); | ||
904 | part->setBody(content); | ||
905 | return part; | ||
906 | } | ||
907 | |||
908 | static KMime::Content *createBodyPart(const QByteArray &body) { | ||
909 | auto mainMessage = new KMime::Content; | ||
910 | mainMessage->setBody(body); | ||
911 | mainMessage->contentType(true)->setMimeType("text/plain"); | ||
912 | // return MailCrypto::sign(mainMessage, {}); | ||
913 | return mainMessage; | ||
914 | } | ||
915 | |||
916 | static void applyAddresses(const QStringList &list, std::function<void(const QByteArray &, const QByteArray &)> callback) | ||
917 | { | ||
918 | for (const auto &to : list) { | ||
919 | QByteArray displayName; | ||
920 | QByteArray addrSpec; | ||
921 | QByteArray comment; | ||
922 | KEmailAddress::splitAddress(to.toUtf8(), displayName, addrSpec, comment); | ||
923 | callback(addrSpec, displayName); | ||
924 | } | ||
925 | } | ||
926 | |||
927 | // static void applyAddresses(const QString &list, std::function<void(const QByteArray &, const QByteArray &)> callback) | ||
928 | // { | ||
929 | // applyAddresses(KEmailAddress::splitAddressList(list), callback); | ||
930 | // } | ||
931 | |||
932 | KMime::Message::Ptr MailTemplates::createMessage(KMime::Message::Ptr existingMessage, const QStringList &to, const QStringList &cc, const QStringList &bcc, const KMime::Types::Mailbox &from, const QString &subject, const QString &body, const QList<Attachment> &attachments) | ||
933 | { | ||
934 | auto mail = existingMessage; | ||
935 | if (!mail) { | ||
936 | mail = KMime::Message::Ptr::create(); | ||
937 | } | ||
938 | mail->to(true)->clear(); | ||
939 | applyAddresses(to, [&](const QByteArray &addrSpec, const QByteArray &displayName) { | ||
940 | mail->to(true)->addAddress(addrSpec, displayName); | ||
941 | }); | ||
942 | |||
943 | mail->cc(true)->clear(); | ||
944 | applyAddresses(cc, [&](const QByteArray &addrSpec, const QByteArray &displayName) { | ||
945 | mail->cc(true)->addAddress(addrSpec, displayName); | ||
946 | }); | ||
947 | |||
948 | mail->bcc(true)->clear(); | ||
949 | applyAddresses(bcc, [&](const QByteArray &addrSpec, const QByteArray &displayName) { | ||
950 | mail->bcc(true)->addAddress(addrSpec, displayName); | ||
951 | }); | ||
952 | |||
953 | mail->from(true)->addAddress(from); | ||
954 | |||
955 | mail->subject(true)->fromUnicodeString(subject, "utf-8"); | ||
956 | if (!mail->messageID()) { | ||
957 | mail->messageID(true)->generate("org.kde.kube"); | ||
958 | } | ||
959 | if (!mail->date(true)->dateTime().isValid()) { | ||
960 | mail->date(true)->setDateTime(QDateTime::currentDateTimeUtc()); | ||
961 | } | ||
962 | |||
963 | if (!attachments.isEmpty()) { | ||
964 | mail->contentType(true)->setMimeType("multipart/mixed"); | ||
965 | mail->contentType()->setBoundary(KMime::multiPartBoundary()); | ||
966 | mail->contentTransferEncoding()->setEncoding(KMime::Headers::CE7Bit); | ||
967 | mail->setPreamble("This is a multi-part message in MIME format.\n"); | ||
968 | for (const auto &attachment : attachments) { | ||
969 | mail->addContent(createAttachmentPart(attachment.data, attachment.filename, attachment.isInline, attachment.mimeType, attachment.name)); | ||
970 | } | ||
971 | mail->addContent(createBodyPart(body.toUtf8())); | ||
972 | } else { | ||
973 | //FIXME same implementation as above for attachments | ||
974 | mail->setBody(body.toUtf8()); | ||
975 | } | ||
976 | |||
977 | mail->assemble(); | ||
978 | return mail; | ||
979 | } | ||