diff options
Diffstat (limited to 'examples/mailtransportresource/mailtransport.cpp')
-rw-r--r-- | examples/mailtransportresource/mailtransport.cpp | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/examples/mailtransportresource/mailtransport.cpp b/examples/mailtransportresource/mailtransport.cpp index 4f7e59b..3d56af9 100644 --- a/examples/mailtransportresource/mailtransport.cpp +++ b/examples/mailtransportresource/mailtransport.cpp | |||
@@ -21,6 +21,9 @@ | |||
21 | #include <QByteArray> | 21 | #include <QByteArray> |
22 | #include <QList> | 22 | #include <QList> |
23 | #include <QDebug> | 23 | #include <QDebug> |
24 | #include <common/log.h> | ||
25 | |||
26 | SINK_DEBUG_AREA("mailtransport") | ||
24 | 27 | ||
25 | extern "C" { | 28 | extern "C" { |
26 | 29 | ||
@@ -57,12 +60,18 @@ static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp) | |||
57 | return 0; | 60 | return 0; |
58 | } | 61 | } |
59 | 62 | ||
60 | 63 | static int progress_callback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) | |
61 | bool 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 | { | 64 | { |
63 | //For ssl use "smtps://mainserver.example.net | 65 | if (ultotal > 0) { |
64 | const char* cacert = 0; // = "/path/to/certificate.pem"; | 66 | auto progress = ulnow * 100.0/ ultotal; |
67 | fprintf(stdout, "Upload progress: %d \n", int(progress)); | ||
68 | } | ||
69 | return 0; | ||
70 | } | ||
65 | 71 | ||
72 | |||
73 | bool 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) | ||
74 | { | ||
66 | CURL *curl; | 75 | CURL *curl; |
67 | CURLcode res = CURLE_OK; | 76 | CURLcode res = CURLE_OK; |
68 | struct curl_slist *recipients = NULL; | 77 | struct curl_slist *recipients = NULL; |
@@ -86,8 +95,8 @@ bool sendMessageCurl(const char *to[], int numTos, const char *cc[], int numCcs, | |||
86 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); | 95 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); |
87 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); | 96 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); |
88 | } | 97 | } |
89 | if (cacert) { | 98 | if (!cacert.isEmpty()) { |
90 | curl_easy_setopt(curl, CURLOPT_CAINFO, cacert); | 99 | curl_easy_setopt(curl, CURLOPT_CAINFO, cacert.constData()); |
91 | } | 100 | } |
92 | 101 | ||
93 | if (from) { | 102 | if (from) { |
@@ -114,6 +123,13 @@ bool sendMessageCurl(const char *to[], int numTos, const char *cc[], int numCcs, | |||
114 | */ | 123 | */ |
115 | curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); | 124 | curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); |
116 | 125 | ||
126 | // curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1L); | ||
127 | //Connection timeout of 10s | ||
128 | curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L); | ||
129 | |||
130 | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0); | ||
131 | curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback); | ||
132 | |||
117 | res = curl_easy_perform(curl); | 133 | res = curl_easy_perform(curl); |
118 | if(res != CURLE_OK) { | 134 | if(res != CURLE_OK) { |
119 | fprintf(stderr, "curl_easy_perform() failed: %s\n", | 135 | fprintf(stderr, "curl_easy_perform() failed: %s\n", |
@@ -128,10 +144,11 @@ bool sendMessageCurl(const char *to[], int numTos, const char *cc[], int numCcs, | |||
128 | 144 | ||
129 | }; | 145 | }; |
130 | 146 | ||
131 | bool MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteArray &server, const QByteArray &username, const QByteArray &password, const QByteArray &cacert) | 147 | bool MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteArray &server, const QByteArray &username, const QByteArray &password, const QByteArray &cacert, MailTransport::Options options) |
132 | { | 148 | { |
133 | QByteArray msg = message->encodedContent(); | 149 | QByteArray msg = message->encodedContent(); |
134 | qDebug() << "Sending message " << msg; | 150 | SinkLog() << "Sending message " << server << username << password << cacert; |
151 | SinkTrace() << "Sending message " << msg; | ||
135 | 152 | ||
136 | QByteArray from(message->from(true)->mailboxes().isEmpty() ? QByteArray() : message->from(true)->mailboxes().first().address()); | 153 | QByteArray from(message->from(true)->mailboxes().isEmpty() ? QByteArray() : message->from(true)->mailboxes().first().address()); |
137 | QList<QByteArray> toList; | 154 | QList<QByteArray> toList; |
@@ -142,8 +159,8 @@ bool MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteA | |||
142 | for (const auto &mb : message->cc(true)->mailboxes()) { | 159 | for (const auto &mb : message->cc(true)->mailboxes()) { |
143 | ccList << mb.address(); | 160 | ccList << mb.address(); |
144 | } | 161 | } |
145 | bool useTls = true; | 162 | bool verifyPeer = options & VerifyPeers; |
146 | bool verifyPeer = false; | 163 | bool useTls = options & UseTls; |
147 | 164 | ||
148 | const int numTos = toList.size(); | 165 | const int numTos = toList.size(); |
149 | const char* to[numTos]; | 166 | const char* to[numTos]; |
@@ -157,5 +174,5 @@ bool MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteA | |||
157 | cc[i] = ccList.at(i); | 174 | cc[i] = ccList.at(i); |
158 | } | 175 | } |
159 | 176 | ||
160 | return sendMessageCurl(to, numTos, cc, numCcs, msg, useTls, from.isEmpty() ? nullptr : from, username, password, server, verifyPeer); | 177 | return sendMessageCurl(to, numTos, cc, numCcs, msg, useTls, from.isEmpty() ? nullptr : from, username, password, server, verifyPeer, cacert); |
161 | } | 178 | } |