summaryrefslogtreecommitdiffstats
path: root/examples/mailtransportresource/mailtransport.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/mailtransportresource/mailtransport.cpp')
-rw-r--r--examples/mailtransportresource/mailtransport.cpp60
1 files changed, 44 insertions, 16 deletions
diff --git a/examples/mailtransportresource/mailtransport.cpp b/examples/mailtransportresource/mailtransport.cpp
index 3d56af9..84c1556 100644
--- a/examples/mailtransportresource/mailtransport.cpp
+++ b/examples/mailtransportresource/mailtransport.cpp
@@ -41,17 +41,17 @@ static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
41 struct upload_status *upload_ctx = (struct upload_status *)userp; 41 struct upload_status *upload_ctx = (struct upload_status *)userp;
42 const char *data; 42 const char *data;
43 43
44 if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) { 44 if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
45 return 0; 45 return 0;
46 } 46 }
47 47
48 data = &upload_ctx->data[upload_ctx->offset]; 48 data = &upload_ctx->data[upload_ctx->offset];
49 if(data) { 49 if (data) {
50 size_t len = strlen(data); 50 size_t len = strlen(data);
51 if (len > size * nmemb) { 51 if (len > size * nmemb) {
52 len = size * nmemb; 52 len = size * nmemb;
53 } 53 }
54 fprintf(stderr, "read n bytes: %d\n", int(len)); 54 fprintf(stdout, "read n bytes: %d\n", int(len));
55 memcpy(ptr, data, len); 55 memcpy(ptr, data, len);
56 upload_ctx->offset += len; 56 upload_ctx->offset += len;
57 return len; 57 return len;
@@ -69,8 +69,17 @@ static int progress_callback(void *clientp, curl_off_t dltotal, curl_off_t dlnow
69 return 0; 69 return 0;
70} 70}
71 71
72static int debug_callback(CURL *handle,
73 curl_infotype type,
74 char *data,
75 size_t size,
76 void *userptr)
77{
78 fprintf(stdout, "CURL_DEBUG: %s", data);
79 return 0;
80}
72 81
73bool 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, const QByteArray &cacert) 82bool 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, const QByteArray &cacert, QByteArray &errorMessage)
74{ 83{
75 CURL *curl; 84 CURL *curl;
76 CURLcode res = CURLE_OK; 85 CURLcode res = CURLE_OK;
@@ -88,7 +97,7 @@ bool sendMessageCurl(const char *to[], int numTos, const char *cc[], int numCcs,
88 curl_easy_setopt(curl, CURLOPT_URL, server); 97 curl_easy_setopt(curl, CURLOPT_URL, server);
89 98
90 if (useTls) { 99 if (useTls) {
91 curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_TRY); 100 curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
92 } 101 }
93 102
94 if (!verifyPeer) { 103 if (!verifyPeer) {
@@ -121,19 +130,29 @@ bool sendMessageCurl(const char *to[], int numTos, const char *cc[], int numCcs,
121 /* Since the traffic will be encrypted, it is very useful to turn on debug 130 /* Since the traffic will be encrypted, it is very useful to turn on debug
122 * information within libcurl to see what is happening during the transfer. 131 * information within libcurl to see what is happening during the transfer.
123 */ 132 */
124 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 133 // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
134 curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, debug_callback);
125 135
126 // curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1L); 136 // curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1L);
127 //Connection timeout of 10s 137 //Connection timeout of 40s
128 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L); 138 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 40L);
129 139
130 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0); 140 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
131 curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback); 141 curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
142 char errorBuffer[CURL_ERROR_SIZE];
143 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
132 144
133 res = curl_easy_perform(curl); 145 res = curl_easy_perform(curl);
134 if(res != CURLE_OK) { 146 if(res != CURLE_OK) {
135 fprintf(stderr, "curl_easy_perform() failed: %s\n", 147 errorMessage += curl_easy_strerror(res);
136 curl_easy_strerror(res)); 148 errorMessage += "; ";
149 }
150 long http_code = 0;
151 curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
152 if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK) {
153 //Succeeded
154 } else {
155 errorMessage += errorBuffer;
137 } 156 }
138 curl_slist_free_all(recipients); 157 curl_slist_free_all(recipients);
139 curl_easy_cleanup(curl); 158 curl_easy_cleanup(curl);
@@ -147,8 +166,6 @@ bool sendMessageCurl(const char *to[], int numTos, const char *cc[], int numCcs,
147bool MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteArray &server, const QByteArray &username, const QByteArray &password, const QByteArray &cacert, MailTransport::Options options) 166bool MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteArray &server, const QByteArray &username, const QByteArray &password, const QByteArray &cacert, MailTransport::Options options)
148{ 167{
149 QByteArray msg = message->encodedContent(); 168 QByteArray msg = message->encodedContent();
150 SinkLog() << "Sending message " << server << username << password << cacert;
151 SinkTrace() << "Sending message " << msg;
152 169
153 QByteArray from(message->from(true)->mailboxes().isEmpty() ? QByteArray() : message->from(true)->mailboxes().first().address()); 170 QByteArray from(message->from(true)->mailboxes().isEmpty() ? QByteArray() : message->from(true)->mailboxes().first().address());
154 QList<QByteArray> toList; 171 QList<QByteArray> toList;
@@ -159,8 +176,11 @@ bool MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteA
159 for (const auto &mb : message->cc(true)->mailboxes()) { 176 for (const auto &mb : message->cc(true)->mailboxes()) {
160 ccList << mb.address(); 177 ccList << mb.address();
161 } 178 }
162 bool verifyPeer = options & VerifyPeers; 179 const bool verifyPeer = options.testFlag(VerifyPeers);
163 bool useTls = options & UseTls; 180 const bool useTls = options.testFlag(UseTls);
181
182 SinkLog() << "Sending message " << server << username << password << "CaCert: " << cacert << "Use tls: " << useTls << " Verify peer: " << verifyPeer;
183 SinkTrace() << "Sending message " << msg;
164 184
165 const int numTos = toList.size(); 185 const int numTos = toList.size();
166 const char* to[numTos]; 186 const char* to[numTos];
@@ -173,6 +193,14 @@ bool MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteA
173 for (int i = 0; i < numCcs; i++) { 193 for (int i = 0; i < numCcs; i++) {
174 cc[i] = ccList.at(i); 194 cc[i] = ccList.at(i);
175 } 195 }
176 196 //Because curl will fail with smtps, but it won't tell you why.
177 return sendMessageCurl(to, numTos, cc, numCcs, msg, useTls, from.isEmpty() ? nullptr : from, username, password, server, verifyPeer, cacert); 197 auto serverAddress = server;
198 serverAddress.replace("smtps://", "smtp://");
199
200 QByteArray errorMessage;
201 auto ret = sendMessageCurl(to, numTos, cc, numCcs, msg, useTls, from.isEmpty() ? nullptr : from, username, password, serverAddress, verifyPeer, cacert, errorMessage);
202 if (!ret) {
203 SinkWarning() << "Failed to send message: " << errorMessage;
204 }
205 return ret;
178} 206}