summaryrefslogtreecommitdiffstats
path: root/framework/mail/mailtransport.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'framework/mail/mailtransport.cpp')
-rw-r--r--framework/mail/mailtransport.cpp160
1 files changed, 160 insertions, 0 deletions
diff --git a/framework/mail/mailtransport.cpp b/framework/mail/mailtransport.cpp
new file mode 100644
index 00000000..49d858e1
--- /dev/null
+++ b/framework/mail/mailtransport.cpp
@@ -0,0 +1,160 @@
1/*
2 Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19#include "mailtransport.h"
20
21#include <QByteArray>
22#include <QList>
23#include <QDebug>
24
25extern "C" {
26
27#include <stdio.h>
28#include <string.h>
29#include <curl/curl.h>
30
31struct upload_status {
32 int offset;
33 const char *data;
34};
35
36static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
37{
38 struct upload_status *upload_ctx = (struct upload_status *)userp;
39 const char *data;
40
41 if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
42 return 0;
43 }
44
45 data = &upload_ctx->data[upload_ctx->offset];
46 if(data) {
47 size_t len = strlen(data);
48 if (len > size * nmemb) {
49 len = size * nmemb;
50 }
51 fprintf(stderr, "read n bytes: %d\n",len);
52 memcpy(ptr, data, len);
53 upload_ctx->offset += len;
54 return len;
55 }
56
57 return 0;
58}
59
60
61void sendMessageCurl(const char *to[], int numTos, const char *cc[], int numCcs, const char *msg, bool useTls, const char* from, const char *username, const char *password, const char *server, bool verifyPeer)
62{
63 //For ssl use "smtps://mainserver.example.net
64 const char* cacert = 0; // = "/path/to/certificate.pem";
65
66 CURL *curl;
67 CURLcode res = CURLE_OK;
68 struct curl_slist *recipients = NULL;
69 struct upload_status upload_ctx;
70
71 upload_ctx.offset = 0;
72 upload_ctx.data = msg;
73
74 curl = curl_easy_init();
75 if(curl) {
76 curl_easy_setopt(curl, CURLOPT_USERNAME, username);
77 curl_easy_setopt(curl, CURLOPT_PASSWORD, password);
78
79 curl_easy_setopt(curl, CURLOPT_URL, server);
80
81 if (useTls) {
82 curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_TRY);
83 }
84
85 if (!verifyPeer) {
86 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
87 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
88 }
89 if (cacert) {
90 curl_easy_setopt(curl, CURLOPT_CAINFO, cacert);
91 }
92
93 if (from) {
94 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
95 }
96
97 for (int i = 0; i < numTos; i++) {
98 recipients = curl_slist_append(recipients, to[i]);
99 }
100 for (int i = 0; i < numCcs; i++) {
101 recipients = curl_slist_append(recipients, cc[i]);
102 }
103 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
104
105 /* We're using a callback function to specify the payload (the headers and
106 * body of the message). You could just use the CURLOPT_READDATA option to
107 * specify a FILE pointer to read from. */
108 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
109 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
110 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
111
112 /* Since the traffic will be encrypted, it is very useful to turn on debug
113 * information within libcurl to see what is happening during the transfer.
114 */
115 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
116
117 res = curl_easy_perform(curl);
118 if(res != CURLE_OK) {
119 fprintf(stderr, "curl_easy_perform() failed: %s\n",
120 curl_easy_strerror(res));
121 }
122 curl_slist_free_all(recipients);
123 curl_easy_cleanup(curl);
124 }
125}
126
127};
128
129void MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteArray &server, const QByteArray &username, const QByteArray &password, const QByteArray &cacert)
130{
131 QByteArray msg = message->encodedContent();
132 qWarning() << "Sending message " << msg;
133
134 QByteArray from(message->from(true)->mailboxes().isEmpty() ? QByteArray() : message->from(true)->mailboxes().first().address());
135 QList<QByteArray> toList;
136 for (const auto &mb : message->to(true)->mailboxes()) {
137 toList << mb.address();
138 }
139 QList<QByteArray> ccList;
140 for (const auto &mb : message->cc(true)->mailboxes()) {
141 ccList << mb.address();
142 }
143 bool useTls = true;
144 bool verifyPeer = false;
145
146 const int numTos = toList.size();
147 const char* to[numTos];
148 for (int i = 0; i < numTos; i++) {
149 to[i] = toList.at(i);
150 }
151
152 const int numCcs = ccList.size();
153 const char* cc[numCcs];
154 for (int i = 0; i < numCcs; i++) {
155 cc[i] = ccList.at(i);
156 }
157
158 sendMessageCurl(to, numTos, cc, numCcs, msg, useTls, from.isEmpty() ? nullptr : from, username, password, server, verifyPeer);
159 qWarning() << "Message sent";
160}