├── src ├── ESPMail.h └── libquickmail │ ├── smtpsocket.h │ ├── smtpsocket.cpp │ ├── quickmail.h │ └── quickmail.cpp ├── library.properties ├── .gitignore ├── keywords.txt ├── README.md ├── examples └── mail_send │ └── mail_send.ino └── LICENSE /src/ESPMail.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/grzesl/ESPMail/HEAD/src/ESPMail.h -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=ESPMail 2 | version=1.0.1 3 | author=Grzegorz Leśniak 4 | maintainer=Grzegorz Leśniak 5 | sentence=EMail library. 6 | paragraph=Use this library to send email through smtp server. 7 | category=Communication 8 | url=https://github.com/grzesl/ESPMail 9 | architectures=esp8266 10 | includes=ESPMail.h 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For ESPMail 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | ESPMail KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | begin KEYWORD2 16 | enableDebugMode KEYWORD2 17 | setTitle KEYWORD2 18 | addTo KEYWORD2 19 | addCC KEYWORD2 20 | setBody KEYWORD2 21 | send KEYWORD2 22 | ~ESPMail KEYWORD2 23 | 24 | ####################################### 25 | # Instances (KEYWORD3) 26 | ####################################### 27 | 28 | mail KEYWORD3 29 | 30 | ####################################### 31 | # Constants (LITERAL1) 32 | ####################################### 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESPMail 2 | Library for sending emails through SMTP mail server. This software use libquickmail. 3 | 4 | ## Usage 5 | 6 | ```console 7 | ESPMail mail; 8 | mail.begin(); 9 | 10 | mail.setSubject("from@example.com", "EMail Subject"); 11 | mail.addTo("to@example.com"); 12 | mail.addCC("cc@example.com"); 13 | mail.addBCC("bcc@example.com"); 14 | 15 | mail.addAttachment("test.txt", "This is content of attachment."); 16 | 17 | mail.setBody("This is an example e-mail.\nRegards Grzesiek"); 18 | //mail.setHTMLBody("This is an example html e-mail.\nRegards Grzesiek"); 19 | 20 | if (mail.send("smtp.server.com", 587, "your_smtp_user", "your_smtp_password") == 0) 21 | { 22 | Serial.println("Mail send OK"); 23 | } 24 | ``` 25 | 26 | ## Debug Mode 27 | Add this line before send to show communication with SMTP server. 28 | 29 | ```console 30 | (...) 31 | mail.enableDebugMode(); 32 | mail.send("smtp.server.com", 587, "your_smtp_user", "your_smtp_password") 33 | ``` -------------------------------------------------------------------------------- /examples/mail_send/mail_send.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Sending emails example. 3 | Name: mail_send.ino 4 | Created: 6/28/2017 1:34:35 PM 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | const char* ssid = "your_ssid"; 11 | const char* password = "your_password"; 12 | ESPMail mail; 13 | int send = 1; 14 | 15 | void setup_wifi() { 16 | Serial.print("\nConnecting to "); 17 | Serial.println(ssid); 18 | 19 | WiFi.begin(ssid, password); 20 | 21 | while (WiFi.status() != WL_CONNECTED) { 22 | Serial.print("."); 23 | delay(500); 24 | } 25 | 26 | Serial.println("\nWiFi connected"); 27 | Serial.print("IP address: "); 28 | Serial.println(WiFi.localIP()); 29 | } 30 | 31 | void setup() { 32 | Serial.begin(115200); 33 | 34 | setup_wifi(); 35 | 36 | mail.begin(); 37 | } 38 | 39 | void loop() { 40 | 41 | if (send == 0) 42 | return; 43 | 44 | mail.setSubject("from@example.com", "EMail Subject"); 45 | mail.addTo("to@example.com"); 46 | mail.addCC("cc@example.com"); 47 | mail.addBCC("bcc@example.com"); 48 | 49 | mail.addAttachment("test.txt", "This is content of attachment."); 50 | 51 | mail.setBody("This is an example e-mail.\nRegards Grzesiek"); 52 | //mail.setHTMLBody("This is an example html e-mail.\nRegards Grzesiek"); 53 | 54 | //mail.enableDebugMode(); 55 | 56 | Serial.print("\n\nESPMail ver:"); 57 | Serial.println(mail.getVersion()); 58 | Serial.print("Mail subject:"); 59 | Serial.println(mail.getSubject()); 60 | Serial.print("\n\n"); 61 | 62 | if (mail.send("smtp.server.com", 587, "your_smtp_user", "your_smtp_password") == 0) 63 | { 64 | Serial.println("Mail send OK"); 65 | } 66 | 67 | send = 0; 68 | } -------------------------------------------------------------------------------- /src/libquickmail/smtpsocket.h: -------------------------------------------------------------------------------- 1 | /*! \file smtpsocket.h 2 | * \brief header file for TCP/IP socket and SMTP communication functions 3 | * \author Brecht Sanders 4 | * \date 2012-2013 5 | * \copyright GPL 6 | */ 7 | /* 8 | This file is part of libquickmail. 9 | 10 | libquickmail is free software: you can redistribute it and/or modify 11 | it under the terms of the GNU General Public License as published by 12 | the Free Software Foundation, either version 3 of the License, or 13 | (at your option) any later version. 14 | 15 | libquickmail is distributed in the hope that it will be useful, 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | GNU General Public License for more details. 19 | 20 | You should have received a copy of the GNU General Public License 21 | along with libquickmail. If not, see . 22 | */ 23 | 24 | #ifndef __INCLUDED_SMTPSOCKET_H 25 | #define __INCLUDED_SMTPSOCKET_H 26 | 27 | #ifdef ARDUINO 28 | //#define SOCKET Client* 29 | #define SOCKET void* 30 | #define INVALID_SOCKET (void*)-1 31 | #define SOCKET_ERROR (void*)-1 32 | #elif defined _WIN32 33 | #include 34 | #else 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #ifndef SOCKET 41 | #define SOCKET int 42 | #endif 43 | #ifndef INVALID_SOCKET 44 | #define INVALID_SOCKET -1 45 | #endif 46 | #ifndef SOCKET_ERROR 47 | #define SOCKET_ERROR -1 48 | #endif 49 | #endif 50 | #include 51 | #include 52 | 53 | #define READ_BUFFER_CHUNK_SIZE 128 54 | #define WRITE_BUFFER_CHUNK_SIZE 128 55 | 56 | #ifdef __cplusplus 57 | extern "C" { 58 | #endif 59 | 60 | /*! \brief connect network socket 61 | * \param smtpserver hostname or IP address of server 62 | * \param smtpport TCP port to connect to 63 | * \param errmsg optional pointer to where error message will be stored (must not be freed by caller) 64 | * \return open network socket or INVALID_SOCKET on error 65 | */ 66 | SOCKET socket_open (const char* smtpserver, unsigned int smtpport, char** errmsg); 67 | 68 | /*! \brief disconnect network socket 69 | * \param sock open network socket 70 | */ 71 | void socket_close (SOCKET sock); 72 | 73 | /*! \brief send data to a network socket 74 | * \param sock open network socket 75 | * \param buf buffer containing data 76 | * \param len size of buffer in bytes 77 | * \return number of bytes sent 78 | */ 79 | int socket_send (SOCKET sock, const char* buf, int len); 80 | 81 | /*! \brief check if data is waiting to be read from network socket 82 | * \param sock open network socket 83 | * \param timeoutseconds number of seconds to wait (0 to return immediately) 84 | * \return nonzero if data is waiting 85 | */ 86 | int socket_data_waiting (SOCKET sock, int timeoutseconds); 87 | 88 | /*! \brief read SMTP response from network socket 89 | * \param sock open network socket 90 | * \return null-terminated string containing received data (must be freed by caller), or NULL 91 | */ 92 | char* socket_receive_stmp (SOCKET sock); 93 | 94 | /*! \brief read SMTP response from network socket 95 | * \param sock open network socket 96 | * \param errmsg optional pointer to where error message will be stored (must be freed by caller) 97 | * \return SMTP status code (code >= 400 means error) 98 | */ 99 | int socket_get_smtp_code (SOCKET sock, char** errmsg); 100 | 101 | /*! \brief send SMTP command and return status code 102 | * \param sock open network socket 103 | * \param debuglog file handle to write debugging information to (NULL for no debugging) 104 | * \param template printf style formatting template 105 | * \return SMTP status code (code >= 400 means error) 106 | */ 107 | int socket_smtp_command (SOCKET sock, FILE* debuglog, const char* templ, ...); 108 | 109 | #ifdef __cplusplus 110 | } 111 | #endif 112 | 113 | #endif //__INCLUDED_SMTPSOCKET_H 114 | -------------------------------------------------------------------------------- /src/libquickmail/smtpsocket.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of libquickmail. 3 | 4 | libquickmail is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | libquickmail is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with libquickmail. If not, see . 16 | */ 17 | #ifdef ARDUINO 18 | #include 19 | #include 20 | #endif 21 | 22 | #include "smtpsocket.h" 23 | #include 24 | #include 25 | #include 26 | 27 | #include 28 | #if _MSC_VER 29 | #define va_copy(dst,src) ((dst) = (src)) 30 | #endif 31 | 32 | //////////////////////////////////////////////////////////////////////// 33 | 34 | #define DEBUG_ERROR(errmsg) 35 | static const char* ERRMSG_MEMORY_ALLOCATION_ERROR = "Memory allocation error"; 36 | 37 | //////////////////////////////////////////////////////////////////////// 38 | 39 | SOCKET socket_open (const char* smtpserver, unsigned int smtpport, char** errmsg) 40 | { 41 | #ifdef ARDUINO 42 | WiFiClient *cli = new WiFiClient(); 43 | SOCKET sock = (void*)cli; 44 | 45 | if (cli->connect(smtpserver, smtpport) == 0) 46 | { 47 | *errmsg = "Error connecting to SMTP server"; 48 | Serial.println(*errmsg); 49 | } 50 | #else 51 | struct in_addr ipv4addr; 52 | struct sockaddr_in remote_sock_addr; 53 | static const struct linger linger_option = {-1, 2}; //linger 2 seconds when disconnecting 54 | //determine IPv4 address of SMTP server 55 | ipv4addr.s_addr = inet_addr(smtpserver); 56 | if (ipv4addr.s_addr == INADDR_NONE) { 57 | struct hostent* addr; 58 | if ((addr = gethostbyname(smtpserver)) != NULL && (addr->h_addrtype == AF_INET && addr->h_length >= 1 && ((struct in_addr*)addr->h_addr)->s_addr != 0)) 59 | memcpy(&ipv4addr, addr->h_addr, sizeof(ipv4addr)); 60 | } 61 | if (ipv4addr.s_addr == INADDR_NONE) { 62 | if (errmsg) 63 | *errmsg = "Unable to resolve SMTP server host name"; 64 | return INVALID_SOCKET; 65 | } 66 | //create socket 67 | sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 68 | if (sock == INVALID_SOCKET) { 69 | if (errmsg) 70 | *errmsg = "Error creating socket for SMTP connection"; 71 | return INVALID_SOCKET; 72 | } 73 | //connect 74 | remote_sock_addr.sin_family = AF_INET; 75 | remote_sock_addr.sin_port = htons(smtpport); 76 | remote_sock_addr.sin_addr.s_addr = ipv4addr.s_addr; 77 | if (connect(sock, (struct sockaddr*)&remote_sock_addr, sizeof(remote_sock_addr)) != 0) { 78 | if (errmsg) 79 | *errmsg = "Error connecting to SMTP server"; 80 | socket_close(sock); 81 | return INVALID_SOCKET; 82 | } 83 | //set linger option 84 | setsockopt(sock, SOL_SOCKET, SO_LINGER, (const char*)&linger_option, sizeof(linger_option)); 85 | #endif 86 | return sock; 87 | } 88 | 89 | void socket_close (SOCKET sock) 90 | { 91 | #ifdef ARDUINO 92 | delete ((WiFiClient*)sock); 93 | #elif defined _WIN32 94 | shutdown(sock, 2); 95 | #else 96 | closesocket(sock); 97 | #endif 98 | } 99 | 100 | int socket_send (SOCKET sock, const char* buf, int len) 101 | { 102 | #ifdef ARDUINO 103 | int total_sent = 0; 104 | WiFiClient *cli = (WiFiClient*)sock; 105 | 106 | /*Serial.printf("socket_send: %d ", len); 107 | for (int i = 0; i < len; i++) 108 | { 109 | Serial.printf("%c", buf[i], len); 110 | } 111 | Serial.printf("\n");*/ 112 | total_sent = cli->write(buf, len); 113 | return total_sent; 114 | #else 115 | 116 | int total_sent = 0; 117 | int l = 0; 118 | if (sock == 0 || !buf) 119 | return 0; 120 | if (len < 0) 121 | len = strlen(buf); 122 | while (len > 0 && (l = send(sock, buf, len, 0)) < len) { 123 | if (l == SOCKET_ERROR || l > len) 124 | return (total_sent > 0 ? total_sent : -1); 125 | total_sent += l; 126 | buf += l; 127 | len -= l; 128 | } 129 | return total_sent + l; 130 | #endif 131 | } 132 | 133 | int socket_data_waiting (SOCKET sock, int timeoutseconds) 134 | { 135 | #ifdef ARDUINO 136 | WiFiClient *cli = (WiFiClient*)sock; 137 | int avaliable; 138 | int ms = millis(); 139 | 140 | do 141 | { 142 | avaliable = cli->available(); 143 | if (avaliable > 0) 144 | break; 145 | 146 | if ((millis() - ms) / 1000 > timeoutseconds) 147 | break; 148 | 149 | } while (1); 150 | 151 | //Serial.printf("socket_data_waiting: %d ", avaliable); 152 | 153 | return avaliable?1:0; 154 | 155 | #else 156 | fd_set rfds; 157 | struct timeval tv; 158 | if (sock == 0) 159 | return 0; 160 | //make a set with only this socket 161 | FD_ZERO(&rfds); 162 | FD_SET(sock, &rfds); 163 | //make a timeval with the supplied timeout 164 | tv.tv_sec = timeoutseconds; 165 | tv.tv_usec = 0; 166 | //check the socket 167 | return (select(1, &rfds, NULL, NULL, &tv) > 0); 168 | #endif 169 | } 170 | 171 | int socket_receive(SOCKET sock, char * buff, int len, int flags) 172 | { 173 | #ifdef ARDUINO 174 | int r; 175 | WiFiClient *cli = (WiFiClient*)sock; 176 | r = cli->read((unsigned char*)buff, len); 177 | //Serial.printf("socket_receive: %x %c %d r:%d\n", *buff, *buff, len, r); 178 | //Serial.printf("socket_receive avai: %d\n", cli->available()); 179 | return r; 180 | 181 | #else 182 | return recv(sock, buff, len, flags); 183 | #endif 184 | 185 | } 186 | 187 | char* socket_receive_smtp (SOCKET sock) 188 | { 189 | char* buf = NULL; 190 | int bufsize = READ_BUFFER_CHUNK_SIZE; 191 | int pos = 0; 192 | int linestart; 193 | int n; 194 | if ((buf = (char*)malloc(bufsize)) == NULL) { 195 | DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 196 | return NULL; 197 | } 198 | do { 199 | //insert line break if response is multiple lines 200 | if (pos > 0) { 201 | buf[pos++] = '\n'; 202 | if (pos >= bufsize) { 203 | char* newbuf; 204 | if ((newbuf = (char*)realloc(buf, bufsize + READ_BUFFER_CHUNK_SIZE)) == NULL) { 205 | free(buf); 206 | DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 207 | return NULL; 208 | } 209 | buf = newbuf; 210 | bufsize += READ_BUFFER_CHUNK_SIZE; 211 | } 212 | } 213 | //add each character read until it is a line break 214 | linestart = pos; 215 | while ((n = socket_receive(sock, buf + pos, 1, 0)) == 1) { 216 | //detect optional carriage return (CR) 217 | if (buf[pos] == '\r') 218 | if (socket_receive(sock, buf + pos, 1, 0) < 1) 219 | return NULL; 220 | //detect line feed (LF) 221 | if (buf[pos] == '\n') 222 | break; 223 | //enlarge buffer if necessary 224 | if (++pos >= bufsize) { 225 | char* newbuf; 226 | if ((newbuf = (char*)realloc(buf, bufsize + READ_BUFFER_CHUNK_SIZE)) == NULL) { 227 | free(buf); 228 | DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 229 | return NULL; 230 | } 231 | buf = newbuf; 232 | bufsize += READ_BUFFER_CHUNK_SIZE; 233 | } 234 | } 235 | //exit on error (e.g. if connection is closed) 236 | if (n < 1) 237 | return NULL; 238 | } while (!isdigit(buf[linestart]) || !isdigit(buf[linestart + 1]) || !isdigit(buf[linestart + 2]) || buf[linestart + 3] != ' '); 239 | buf[pos] = 0; 240 | return buf; 241 | } 242 | 243 | int socket_get_smtp_code (SOCKET sock, char** message) 244 | { 245 | int code; 246 | char* buf = socket_receive_smtp(sock); 247 | 248 | //Serial.printf("socket_get_smtp_code: %s\n", buf); 249 | 250 | if (!buf || strlen(buf) < 4 || (buf[3] != ' ' && buf[3] != '-')) { 251 | free(buf); 252 | return 999; 253 | } 254 | //get code 255 | buf[3] = 0; 256 | code = atoi(buf); 257 | //get error message (if needed) 258 | if (message /*&& code >= 400*/) 259 | *message = strdup(buf + 4); 260 | //clean up and return 261 | free(buf); 262 | return code; 263 | } 264 | 265 | int socket_smtp_command(SOCKET sock, FILE* debuglog, const char* templ, ...) 266 | { 267 | char* message; 268 | int statuscode; 269 | //send command (if one is supplied) 270 | if (templ) { 271 | va_list ap; 272 | //va_list aq; 273 | char tmp[260]; 274 | char* cmd = tmp; 275 | int cmdlen = 256; 276 | 277 | va_start(ap, templ); 278 | //construct command to send 279 | //va_copy(aq, ap); 280 | //cmdlen = vsnprintf(tmp, 256, templ, aq); 281 | //va_end(aq); 282 | //if ((cmd = (char*)malloc(cmdlen + 3)) == NULL) { 283 | // DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 284 | // if (debuglog) 285 | //#ifdef ARDUINO 286 | // Serial.printf("%s\n", ERRMSG_MEMORY_ALLOCATION_ERROR); 287 | //#else 288 | // fprintf(debuglog, "%s\n", ERRMSG_MEMORY_ALLOCATION_ERROR); 289 | //#endif 290 | 291 | //va_end(ap); 292 | //return 999; 293 | //} 294 | cmdlen = vsnprintf(cmd, cmdlen + 1, templ, ap); 295 | 296 | va_end(ap); 297 | //log command to send 298 | if (debuglog) 299 | #ifdef ARDUINO 300 | Serial.print("SMTP> "); 301 | Serial.print(cmd); 302 | Serial.print("\n"); 303 | #else 304 | fprintf(debuglog, "SMTP> %s\n", cmd); 305 | #endif 306 | //append CR+LF 307 | strcpy(cmd + cmdlen, "\r\n"); 308 | cmdlen += 2; 309 | //send command 310 | statuscode = socket_send(sock, cmd, cmdlen); 311 | //clean up 312 | //free(cmd); 313 | 314 | if (statuscode < 0) 315 | return 999; 316 | } 317 | 318 | socket_data_waiting(sock, 1); 319 | //receive result 320 | message = NULL; 321 | statuscode = socket_get_smtp_code(sock, &message); 322 | if (debuglog) 323 | #ifdef ARDUINO 324 | Serial.print("SMTP< "); 325 | Serial.print(statuscode); 326 | Serial.print(" "); 327 | Serial.print((message ? message : "")); 328 | Serial.print("\n"); 329 | #else 330 | fprintf(debuglog, "SMTP< %i %s\n", statuscode, (message ? message : "")); 331 | #endif 332 | 333 | free(message); 334 | return statuscode; 335 | } 336 | -------------------------------------------------------------------------------- /src/libquickmail/quickmail.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of libquickmail. 3 | 4 | libquickmail is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | libquickmail is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with libquickmail. If not, see . 16 | */ 17 | 18 | /*! \file quickmail.h 19 | * \brief header file for libquickmail 20 | * \author Brecht Sanders 21 | * \date 2012-2016 22 | * \copyright GPL 23 | */ 24 | 25 | #ifndef __INCLUDED_QUICKMAIL_H 26 | #define __INCLUDED_QUICKMAIL_H 27 | 28 | #include 29 | 30 | /*! \cond PRIVATE */ 31 | 32 | #ifdef ARDUINO 33 | #define DLL_EXPORT_LIBQUICKMAIL 34 | #include 35 | #include 36 | #elif defined _WIN32 37 | #if defined(BUILD_QUICKMAIL_DLL) 38 | #define DLL_EXPORT_LIBQUICKMAIL __declspec(dllexport) 39 | #elif !defined(STATIC) && !defined(BUILD_QUICKMAIL_STATIC) 40 | #define DLL_EXPORT_LIBQUICKMAIL __declspec(dllimport) 41 | #else 42 | #define DLL_EXPORT_LIBQUICKMAIL 43 | #endif 44 | #else 45 | #define DLL_EXPORT_LIBQUICKMAIL 46 | #endif 47 | /*! \endcond */ 48 | 49 | #ifdef __cplusplus 50 | extern "C" { 51 | #endif 52 | 53 | /*! \brief quickmail object type */ 54 | typedef struct email_info_struct* quickmail; 55 | 56 | 57 | 58 | /*! \brief type of pointer to function for opening attachment data 59 | * \param filedata custom data as passed to quickmail_add_body_custom/quickmail_add_attachment_custom 60 | * \return data structure to be used in calls to quickmail_attachment_read_fn and quickmail_attachment_close_fn functions 61 | * \sa quickmail_add_body_custom() 62 | * \sa quickmail_add_attachment_custom() 63 | */ 64 | typedef void* (*quickmail_attachment_open_fn)(void* filedata); 65 | 66 | /*! \brief type of pointer to function for reading attachment data 67 | * \param handle data structure obtained via the corresponding quickmail_attachment_open_fn function 68 | * \param buf buffer for receiving data 69 | * \param len size in bytes of buffer for receiving data 70 | * \return number of bytes read (zero on end of file) 71 | * \sa quickmail_add_body_custom() 72 | * \sa quickmail_add_attachment_custom() 73 | */ 74 | typedef size_t (*quickmail_attachment_read_fn)(void* handle, void* buf, size_t len); 75 | 76 | /*! \brief type of pointer to function for closing attachment data 77 | * \param handle data structure obtained via the corresponding quickmail_attachment_open_fn function 78 | * \sa quickmail_add_body_custom() 79 | * \sa quickmail_add_attachment_custom() 80 | */ 81 | typedef void (*quickmail_attachment_close_fn)(void* handle); 82 | 83 | /*! \brief type of pointer to function for cleaning up custom data in quickmail_destroy 84 | * \param filedata custom data as passed to quickmail_add_body_custom/quickmail_add_attachment_custom 85 | * \sa quickmail_add_body_custom() 86 | * \sa quickmail_add_attachment_custom() 87 | */ 88 | typedef void (*quickmail_attachment_free_filedata_fn)(void* filedata); 89 | 90 | /*! \brief type of pointer to function for cleaning up custom data in quickmail_destroy 91 | * \param mailobj quickmail object 92 | * \param filename attachment filename (same value as mimetype for mail body) 93 | * \param mimetype MIME type 94 | * \param attachment_data_open function for opening attachment data 95 | * \param attachment_data_read function for reading attachment data 96 | * \param attachment_data_close function for closing attachment data (optional, free() will be used if NULL) 97 | * \param callbackdata custom data passed to quickmail_list_attachments 98 | * \sa quickmail_list_bodies() 99 | * \sa quickmail_list_attachments() 100 | */ 101 | typedef void (*quickmail_list_attachment_callback_fn)(quickmail mailobj, const char* filename, const char* mimetype, quickmail_attachment_open_fn email_info_attachment_open, quickmail_attachment_read_fn email_info_attachment_read, quickmail_attachment_close_fn email_info_attachment_close, void* callbackdata); 102 | 103 | 104 | 105 | /*! \brief get version quickmail library 106 | * \return library version 107 | */ 108 | DLL_EXPORT_LIBQUICKMAIL const char* quickmail_get_version (); 109 | 110 | /*! \brief initialize quickmail library, call once at the beginning of the main thread of the application 111 | * \return zero on success 112 | */ 113 | DLL_EXPORT_LIBQUICKMAIL int quickmail_initialize (); 114 | 115 | /*! \brief clean up quickmail library, call once at the end of the main thread of the application 116 | * \return zero on success 117 | */ 118 | DLL_EXPORT_LIBQUICKMAIL int quickmail_cleanup (); 119 | 120 | /*! \brief create a new quickmail object 121 | * \param from sender e-mail address 122 | * \param subject e-mail subject 123 | * \return quickmail object or NULL on error 124 | */ 125 | DLL_EXPORT_LIBQUICKMAIL quickmail quickmail_create (const char* from, const char* subject); 126 | 127 | /*! \brief clean up a quickmail object 128 | * \param mailobj quickmail object 129 | */ 130 | DLL_EXPORT_LIBQUICKMAIL void quickmail_destroy (quickmail mailobj); 131 | 132 | /*! \brief set the sender (from) e-mail address of a quickmail object 133 | * \param mailobj quickmail object 134 | * \param from sender e-mail address 135 | */ 136 | DLL_EXPORT_LIBQUICKMAIL void quickmail_set_from (quickmail mailobj, const char* from); 137 | 138 | /*! \brief get the sender (from) e-mail address of a quickmail object 139 | * \param mailobj quickmail object 140 | * \return sender e-mail address 141 | */ 142 | DLL_EXPORT_LIBQUICKMAIL const char* quickmail_get_from (quickmail mailobj); 143 | 144 | /*! \brief add a recipient (to) e-mail address to a quickmail object 145 | * \param mailobj quickmail object 146 | * \param e-mail recipient e-mail address 147 | */ 148 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_to (quickmail mailobj, const char* email); 149 | 150 | /*! \brief add a carbon copy recipient (cc) e-mail address to a quickmail object 151 | * \param mailobj quickmail object 152 | * \param e-mail recipient e-mail address 153 | */ 154 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_cc (quickmail mailobj, const char* email); 155 | 156 | /*! \brief add a blind carbon copy recipient (bcc) e-mail address to a quickmail object 157 | * \param mailobj quickmail object 158 | * \param e-mail recipient e-mail address 159 | */ 160 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_bcc (quickmail mailobj, const char* email); 161 | 162 | /*! \brief set the subject of a quickmail object 163 | * \param mailobj quickmail object 164 | * \param subject e-mail subject 165 | */ 166 | DLL_EXPORT_LIBQUICKMAIL void quickmail_set_subject (quickmail mailobj, const char* subject); 167 | 168 | /*! \brief set the subject of a quickmail object 169 | * \param mailobj quickmail object 170 | * \return e-mail subject 171 | */ 172 | DLL_EXPORT_LIBQUICKMAIL const char* quickmail_get_subject (quickmail mailobj); 173 | 174 | /*! \brief add an e-mail header to a quickmail object 175 | * \param mailobj quickmail object 176 | * \param headerline header line to add 177 | */ 178 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_header (quickmail mailobj, const char* headerline); 179 | 180 | /*! \brief set the body of a quickmail object 181 | * \param mailobj quickmail object 182 | * \param body e-mail body 183 | */ 184 | DLL_EXPORT_LIBQUICKMAIL void quickmail_set_body (quickmail mailobj, const char* body); 185 | 186 | /*! \brief set the body of a quickmail object 187 | * any existing bodies will be removed and a single plain text body will be added 188 | * \param mailobj quickmail object 189 | * \return e-mail body or NULL on error (caller must free result) 190 | */ 191 | DLL_EXPORT_LIBQUICKMAIL char* quickmail_get_body (quickmail mailobj); 192 | 193 | /*! \brief add a body file to a quickmail object (deprecated) 194 | * \param mailobj quickmail object 195 | * \param mimetype MIME type (text/plain will be used if set to NULL) 196 | * \param path path of file with body data 197 | */ 198 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_body_file (quickmail mailobj, const char* mimetype, const char* path); 199 | 200 | /*! \brief add a body from memory to a quickmail object 201 | * \param mailobj quickmail object 202 | * \param mimetype MIME type (text/plain will be used if set to NULL) 203 | * \param data body content 204 | * \param datalen size of data in bytes 205 | * \param mustfree non-zero if data must be freed by quickmail_destroy 206 | */ 207 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_body_memory (quickmail mailobj, const char* mimetype, char* data, size_t datalen, int mustfree); 208 | 209 | /*! \brief add a body with custom read functions to a quickmail object 210 | * \param mailobj quickmail object 211 | * \param mimetype MIME type (text/plain will be used if set to NULL) 212 | * \param data custom data passed to attachment_data_open and attachment_data_filedata_free functions 213 | * \param attachment_data_open function for opening attachment data 214 | * \param attachment_data_read function for reading attachment data 215 | * \param attachment_data_close function for closing attachment data (optional, free() will be used if NULL) 216 | * \param attachment_data_filedata_free function for cleaning up custom data in quickmail_destroy (optional, free() will be used if NULL) 217 | */ 218 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_body_custom (quickmail mailobj, const char* mimetype, char* data, quickmail_attachment_open_fn attachment_data_open, quickmail_attachment_read_fn attachment_data_read, quickmail_attachment_close_fn attachment_data_close, quickmail_attachment_free_filedata_fn attachment_data_filedata_free); 219 | 220 | /*! \brief remove body from quickmail object 221 | * \param mailobj quickmail object 222 | * \param mimetype MIME type (text/plain will be used if set to NULL) 223 | * \return zero on success 224 | */ 225 | DLL_EXPORT_LIBQUICKMAIL int quickmail_remove_body (quickmail mailobj, const char* mimetype); 226 | 227 | /*! \brief list bodies by calling a callback function for each body 228 | * \param mailobj quickmail object 229 | * \param callback function to call for each attachment 230 | * \param callbackdata custom data to pass to the callback function 231 | * \sa quickmail_list_attachment_callback_fn 232 | */ 233 | DLL_EXPORT_LIBQUICKMAIL void quickmail_list_bodies (quickmail mailobj, quickmail_list_attachment_callback_fn callback, void* callbackdata); 234 | 235 | /*! \brief add a file attachment to a quickmail object 236 | * \param mailobj quickmail object 237 | * \param path path of file to attach 238 | * \param mimetype MIME type of file to attach (application/octet-stream will be used if set to NULL) 239 | */ 240 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_attachment_file (quickmail mailobj, const char* path, const char* mimetype); 241 | 242 | /*! \brief add an attachment from memory to a quickmail object 243 | * \param mailobj quickmail object 244 | * \param filename name of file to attach (must not include full path) 245 | * \param mimetype MIME type of file to attach (set to NULL for default binary file) 246 | * \param data data content 247 | * \param datalen size of data in bytes 248 | * \param mustfree non-zero if data must be freed by quickmail_destroy 249 | */ 250 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_attachment_memory (quickmail mailobj, const char* filename, const char* mimetype, char* data, size_t datalen, int mustfree); 251 | 252 | /*! \brief add an attachment with custom read functions to a quickmail object 253 | * \param mailobj quickmail object 254 | * \param filename name of file to attach (must not include full path) 255 | * \param mimetype MIME type of file to attach (set to NULL for default binary file) 256 | * \param data custom data passed to attachment_data_open and attachment_data_filedata_free functions 257 | * \param attachment_data_open function for opening attachment data 258 | * \param attachment_data_read function for reading attachment data 259 | * \param attachment_data_close function for closing attachment data (optional, free() will be used if NULL) 260 | * \param attachment_data_filedata_free function for cleaning up custom data in quickmail_destroy (optional, free() will be used if NULL) 261 | */ 262 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_attachment_custom (quickmail mailobj, const char* filename, const char* mimetype, char* data, quickmail_attachment_open_fn attachment_data_open, quickmail_attachment_read_fn attachment_data_read, quickmail_attachment_close_fn attachment_data_close, quickmail_attachment_free_filedata_fn attachment_data_filedata_free); 263 | 264 | /*! \brief remove attachment from quickmail object 265 | * \param mailobj quickmail object 266 | * \param filename name of file to attach (must not include full path) 267 | * \return zero on success 268 | */ 269 | DLL_EXPORT_LIBQUICKMAIL int quickmail_remove_attachment (quickmail mailobj, const char* filename); 270 | 271 | /*! \brief list attachments by calling a callback function for each attachment 272 | * \param mailobj quickmail object 273 | * \param callback function to call for each attachment 274 | * \param callbackdata custom data to pass to the callback function 275 | * \sa quickmail_list_attachment_callback_fn 276 | */ 277 | DLL_EXPORT_LIBQUICKMAIL void quickmail_list_attachments (quickmail mailobj, quickmail_list_attachment_callback_fn callback, void* callbackdata); 278 | 279 | /*! \brief set the debug logging destination of a quickmail object 280 | * \param mailobj quickmail object 281 | * \param filehandle file handle of logging destination (or NULL for no logging) 282 | */ 283 | DLL_EXPORT_LIBQUICKMAIL void quickmail_set_debug_log (quickmail mailobj, FILE* filehandle); 284 | 285 | /*! \brief save the generated e-mail to a file 286 | * \param mailobj quickmail object 287 | * \param filehandle file handle to write the e-mail contents to 288 | */ 289 | DLL_EXPORT_LIBQUICKMAIL void quickmail_fsave (quickmail mailobj, FILE* filehandle); 290 | 291 | /*! \brief read data the next data from the e-mail contents (can be used as CURLOPT_READFUNCTION callback function) 292 | * \param buffer buffer to copy data to (bust be able to hold size * nmemb bytes) 293 | * \param size record size 294 | * \param nmemb number of records to copy to \p buffer 295 | * \param mailobj quickmail object 296 | * \return number of bytes copied to \p buffer or 0 if at end 297 | */ 298 | DLL_EXPORT_LIBQUICKMAIL size_t quickmail_get_data (void* buffer, size_t size, size_t nmemb, void* mailobj); 299 | 300 | /*! \brief send the e-mail via SMTP 301 | * \param mailobj quickmail object 302 | * \param smtpserver IP address or hostname of SMTP server 303 | * \param smtpport SMTP port number (normally this is 25) 304 | * \param username username to use for authentication (or NULL if not needed) 305 | * \param password password to use for authentication (or NULL if not needed) 306 | * \return NULL on success or error message on error 307 | */ 308 | DLL_EXPORT_LIBQUICKMAIL const char* quickmail_send (quickmail mailobj, const char* smtpserver, unsigned int smtpport, const char* username, const char* password); 309 | 310 | /*! \brief send the e-mail via SMTPS 311 | * \param mailobj quickmail object 312 | * \param smtpserver IP address or hostname of SMTPS server 313 | * \param smtpport SMTPS port number (normally this is 465) 314 | * \param username username to use for authentication (or NULL if not needed) 315 | * \param password password to use for authentication (or NULL if not needed) 316 | * \return NULL on success or error message on error 317 | */ 318 | DLL_EXPORT_LIBQUICKMAIL const char* quickmail_send_secure (quickmail mailobj, const char* smtpserver, unsigned int smtpport, const char* username, const char* password); 319 | 320 | 321 | #ifdef __cplusplus 322 | } 323 | #endif 324 | 325 | #endif //__INCLUDED_QUICKMAIL_H 326 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /src/libquickmail/quickmail.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of libquickmail. 3 | 4 | libquickmail is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | libquickmail is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with libquickmail. If not, see . 16 | */ 17 | 18 | #define NOCURL 19 | 20 | #if defined(_WIN32) && defined(DLL_EXPORT) && !defined(BUILD_QUICKMAIL_DLL) 21 | #define BUILD_QUICKMAIL_DLL 22 | #endif 23 | #include "quickmail.h" 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #ifndef _WIN32 32 | #include 33 | #endif 34 | #if _MSC_VER 35 | #define snprintf _snprintf 36 | #define strdup _strdup 37 | #endif 38 | #ifndef NOCURL 39 | #if (defined(STATIC) || defined(BUILD_QUICKMAIL_STATIC)) && !defined(CURL_STATICLIB) 40 | #define CURL_STATICLIB 41 | #endif 42 | #include 43 | #else 44 | #include "smtpsocket.h" 45 | #endif 46 | 47 | #define LIBQUICKMAIL_VERSION_MAJOR 0 48 | #define LIBQUICKMAIL_VERSION_MINOR 1 49 | #define LIBQUICKMAIL_VERSION_MICRO 25 50 | 51 | #define VERSION_STRINGIZE_(major, minor, micro) #major"."#minor"."#micro 52 | #define VERSION_STRINGIZE(major, minor, micro) VERSION_STRINGIZE_(major, minor, micro) 53 | 54 | #define LIBQUICKMAIL_VERSION VERSION_STRINGIZE(LIBQUICKMAIL_VERSION_MAJOR,LIBQUICKMAIL_VERSION_MINOR,LIBQUICKMAIL_VERSION_MICRO) 55 | 56 | #define NEWLINE "\r\n" 57 | #define NEWLINELENGTH 2 58 | //#define NEWLINE "\n" 59 | //#define NEWLINELENGTH 1 60 | 61 | #define MIME_LINE_WIDTH 72 62 | #define BODY_BUFFER_SIZE 256 63 | 64 | //definitions of the different stages of generating the message data 65 | #define MAILPART_INITIALIZE 0 66 | #define MAILPART_HEADER 1 67 | #define MAILPART_BODY 2 68 | #define MAILPART_BODY_DONE 3 69 | #define MAILPART_ATTACHMENT 4 70 | #define MAILPART_END 5 71 | #define MAILPART_DONE 6 72 | 73 | static const char* default_mime_type = "text/plain"; 74 | 75 | //////////////////////////////////////////////////////////////////////// 76 | 77 | #define DEBUG_ERROR(errmsg) 78 | static const char* ERRMSG_MEMORY_ALLOCATION_ERROR = "Memory allocation error"; 79 | 80 | //////////////////////////////////////////////////////////////////////// 81 | 82 | char* randomize_zeros (char* data) 83 | { 84 | //replace all 0s with random digits 85 | char* p = data; 86 | while (*p) { 87 | if (*p == '0') 88 | *p = '0' + rand() % 10; 89 | p++; 90 | } 91 | return data; 92 | } 93 | 94 | char* str_append (char** data, const char* newdata) 95 | { 96 | //append a string to the end of an existing string 97 | char* p; 98 | int len = (*data ? strlen(*data) : 0); 99 | if ((p = (char*)realloc(*data, len + strlen(newdata) + 1)) == NULL) { 100 | free(p); 101 | DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 102 | return NULL; 103 | } 104 | *data = p; 105 | strcpy(*data + len, newdata); 106 | return *data; 107 | } 108 | 109 | //////////////////////////////////////////////////////////////////////// 110 | 111 | struct email_info_struct { 112 | int current; //must be zet to 0 113 | time_t timestamp; 114 | char* from; 115 | struct email_info_email_list_struct* to; 116 | struct email_info_email_list_struct* cc; 117 | struct email_info_email_list_struct* bcc; 118 | char* subject; 119 | char* header; 120 | struct email_info_attachment_list_struct* bodylist; 121 | struct email_info_attachment_list_struct* attachmentlist; 122 | char* buf; 123 | int buflen; 124 | char* mime_boundary_body; 125 | char* mime_boundary_part; 126 | struct email_info_attachment_list_struct* current_attachment; 127 | FILE* debuglog; 128 | char dtable[64]; 129 | }; 130 | 131 | //////////////////////////////////////////////////////////////////////// 132 | 133 | struct email_info_email_list_struct { 134 | char* data; 135 | struct email_info_email_list_struct* next; 136 | }; 137 | 138 | void email_info_string_list_add (struct email_info_email_list_struct** list, const char* data) 139 | { 140 | struct email_info_email_list_struct** p = list; 141 | while (*p) 142 | p = &(*p)->next; 143 | if ((*p = (struct email_info_email_list_struct*)malloc(sizeof(struct email_info_email_list_struct))) == NULL) { 144 | DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 145 | return; 146 | } 147 | (*p)->data = (data ? strdup(data) : NULL); 148 | (*p)->next = NULL; 149 | } 150 | 151 | void email_info_string_list_free (struct email_info_email_list_struct** list) 152 | { 153 | struct email_info_email_list_struct* p = *list; 154 | struct email_info_email_list_struct* current; 155 | while (p) { 156 | current = p; 157 | p = current->next; 158 | free(current->data); 159 | free(current); 160 | } 161 | *list = NULL; 162 | } 163 | 164 | char* email_info_string_list_concatenate (struct email_info_email_list_struct* list) 165 | { 166 | char* result = NULL; 167 | struct email_info_email_list_struct* listentry = list; 168 | while (listentry) { 169 | if (listentry->data && *listentry->data) { 170 | if (result) 171 | str_append(&result, "," NEWLINE "\t"); 172 | str_append(&result, "<"); 173 | str_append(&result, listentry->data); 174 | str_append(&result, ">"); 175 | } 176 | listentry = listentry->next; 177 | } 178 | return result; 179 | } 180 | 181 | //////////////////////////////////////////////////////////////////////// 182 | 183 | struct email_info_attachment_list_struct { 184 | char* filename; 185 | char* mimetype; 186 | void* filedata; 187 | void* handle; 188 | quickmail_attachment_open_fn email_info_attachment_open; 189 | quickmail_attachment_read_fn email_info_attachment_read; 190 | quickmail_attachment_close_fn email_info_attachment_close; 191 | quickmail_attachment_free_filedata_fn email_info_attachment_filedata_free; 192 | struct email_info_attachment_list_struct* next; 193 | }; 194 | 195 | struct email_info_attachment_list_struct* email_info_attachment_list_add (struct email_info_attachment_list_struct** list, const char* filename, const char* mimetype, void* filedata, quickmail_attachment_open_fn email_info_attachment_open, quickmail_attachment_read_fn email_info_attachment_read, quickmail_attachment_close_fn email_info_attachment_close, quickmail_attachment_free_filedata_fn email_info_attachment_filedata_free) 196 | { 197 | struct email_info_attachment_list_struct** p = list; 198 | while (*p) 199 | p = &(*p)->next; 200 | if ((*p = (struct email_info_attachment_list_struct*)malloc(sizeof(struct email_info_attachment_list_struct))) == NULL) { 201 | DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 202 | return NULL; 203 | } 204 | (*p)->filename = strdup(filename ? filename : "UNNAMED"); 205 | (*p)->mimetype = (mimetype ? strdup(mimetype) : NULL); 206 | (*p)->filedata = filedata; 207 | (*p)->handle = NULL; 208 | (*p)->email_info_attachment_open = email_info_attachment_open; 209 | (*p)->email_info_attachment_read = email_info_attachment_read; 210 | (*p)->email_info_attachment_close = email_info_attachment_close; 211 | (*p)->email_info_attachment_filedata_free = email_info_attachment_filedata_free; 212 | (*p)->next = NULL; 213 | return *p; 214 | } 215 | 216 | void email_info_attachment_list_free_entry (struct email_info_attachment_list_struct* current) 217 | { 218 | if (current->handle) { 219 | if (current->email_info_attachment_close) 220 | current->email_info_attachment_close(current->handle); 221 | //else 222 | // free(current->handle); 223 | current->handle = NULL; 224 | } 225 | if (current->filedata) { 226 | if (current->email_info_attachment_filedata_free) 227 | current->email_info_attachment_filedata_free(current->filedata); 228 | else 229 | free(current->filedata); 230 | } 231 | if (current->mimetype) 232 | free(current->mimetype); 233 | free(current->filename); 234 | free(current); 235 | } 236 | 237 | void email_info_attachment_list_free (struct email_info_attachment_list_struct** list) 238 | { 239 | struct email_info_attachment_list_struct* p = *list; 240 | struct email_info_attachment_list_struct* current; 241 | while (p) { 242 | current = p; 243 | p = current->next; 244 | email_info_attachment_list_free_entry(current); 245 | } 246 | *list = NULL; 247 | } 248 | 249 | int email_info_attachment_list_delete (struct email_info_attachment_list_struct** list, const char* filename) 250 | { 251 | struct email_info_attachment_list_struct** p = list; 252 | while (*p) { 253 | if (strcmp((*p)->filename, filename) == 0) { 254 | struct email_info_attachment_list_struct* current = *p; 255 | *p = current->next; 256 | email_info_attachment_list_free_entry(current); 257 | return 0; 258 | } 259 | p = &(*p)->next; 260 | } 261 | return -1; 262 | } 263 | 264 | void email_info_attachment_list_close_handles (struct email_info_attachment_list_struct* list) 265 | { 266 | struct email_info_attachment_list_struct* p = list; 267 | while (p) { 268 | if (p->handle) { 269 | if (p->email_info_attachment_close) 270 | p->email_info_attachment_close(p->handle); 271 | //else 272 | // free(p->handle); 273 | p->handle = NULL; 274 | } 275 | p = p->next; 276 | } 277 | } 278 | 279 | //dummy attachment functions 280 | 281 | void* email_info_attachment_open_dummy (void* filedata) 282 | { 283 | return (void*)&email_info_attachment_open_dummy; 284 | } 285 | 286 | size_t email_info_attachment_read_dummy (void* handle, void* buf, size_t len) 287 | { 288 | return 0; 289 | } 290 | 291 | struct email_info_attachment_list_struct* email_info_attachment_list_add_dummy (struct email_info_attachment_list_struct** list, const char* filename, const char* mimetype) 292 | { 293 | return email_info_attachment_list_add(list, filename, mimetype, NULL, email_info_attachment_open_dummy, email_info_attachment_read_dummy, NULL, NULL); 294 | } 295 | 296 | //file attachment functions 297 | 298 | void* email_info_attachment_open_file (void* filedata) 299 | { 300 | return (void*)fopen((char*)filedata, "rb"); 301 | } 302 | 303 | size_t email_info_attachment_read_file (void* handle, void* buf, size_t len) 304 | { 305 | return fread(buf, 1, len, (FILE*)handle); 306 | } 307 | 308 | void email_info_attachment_close_file (void* handle) 309 | { 310 | if (handle) 311 | fclose((FILE*)handle); 312 | } 313 | 314 | struct email_info_attachment_list_struct* email_info_attachment_list_add_file (struct email_info_attachment_list_struct** list, const char* path, const char* mimetype) 315 | { 316 | //determine base filename 317 | const char* basename = path + strlen(path); 318 | while (basename != path) { 319 | basename--; 320 | if (*basename == '/' 321 | #ifdef _WIN32 322 | || *basename == '\\' || *basename == ':' 323 | #endif 324 | ) { 325 | basename++; 326 | break; 327 | } 328 | } 329 | return email_info_attachment_list_add(list, basename, mimetype, (void*)strdup(path), email_info_attachment_open_file, email_info_attachment_read_file, email_info_attachment_close_file, NULL); 330 | } 331 | 332 | //memory attachment functions 333 | 334 | struct email_info_attachment_memory_filedata_struct { 335 | char* data; 336 | size_t datalen; 337 | int mustfree; 338 | }; 339 | 340 | struct email_info_attachment_memory_handle_struct { 341 | const char* data; 342 | size_t datalen; 343 | size_t pos; 344 | }; 345 | 346 | void* email_info_attachment_open_memory (void* filedata) 347 | { 348 | struct email_info_attachment_memory_filedata_struct* data; 349 | struct email_info_attachment_memory_handle_struct* result; 350 | data = ((struct email_info_attachment_memory_filedata_struct*)filedata); 351 | if (!data->data) 352 | return NULL; 353 | if ((result = (struct email_info_attachment_memory_handle_struct*)malloc(sizeof(struct email_info_attachment_memory_handle_struct))) == NULL) { 354 | DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 355 | return NULL; 356 | } 357 | result->data = data->data; 358 | result->datalen = data->datalen; 359 | result->pos = 0; 360 | return result; 361 | } 362 | 363 | size_t email_info_attachment_read_memory (void* handle, void* buf, size_t len) 364 | { 365 | struct email_info_attachment_memory_handle_struct* h = (struct email_info_attachment_memory_handle_struct*)handle; 366 | size_t n = (h->pos + len <= h->datalen ? len : h->datalen - h->pos); 367 | memcpy(buf, h->data + h->pos, n); 368 | h->pos += n; 369 | return n; 370 | } 371 | 372 | void email_info_attachment_close_memory (void* handle) 373 | { 374 | if (handle) 375 | free(handle); 376 | } 377 | 378 | void email_info_attachment_filedata_free_memory (void* filedata) 379 | { 380 | struct email_info_attachment_memory_filedata_struct* data = ((struct email_info_attachment_memory_filedata_struct*)filedata); 381 | if (data) { 382 | if (data->mustfree) 383 | free(data->data); 384 | free(data); 385 | } 386 | } 387 | 388 | struct email_info_attachment_list_struct* email_info_attachment_list_add_memory (struct email_info_attachment_list_struct** list, const char* filename, const char* mimetype, char* data, size_t datalen, int mustfree) 389 | { 390 | struct email_info_attachment_memory_filedata_struct* filedata; 391 | if ((filedata = (struct email_info_attachment_memory_filedata_struct*)malloc(sizeof(struct email_info_attachment_memory_filedata_struct))) == NULL) { 392 | DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 393 | return NULL; 394 | } 395 | filedata->data = data; 396 | filedata->datalen = datalen; 397 | filedata->mustfree = mustfree; 398 | return email_info_attachment_list_add(list, filename, mimetype, filedata, email_info_attachment_open_memory, email_info_attachment_read_memory, email_info_attachment_close_memory, email_info_attachment_filedata_free_memory); 399 | } 400 | 401 | //////////////////////////////////////////////////////////////////////// 402 | 403 | DLL_EXPORT_LIBQUICKMAIL const char* quickmail_get_version () 404 | { 405 | return VERSION_STRINGIZE(LIBQUICKMAIL_VERSION_MAJOR, LIBQUICKMAIL_VERSION_MINOR, LIBQUICKMAIL_VERSION_MICRO) 406 | #if defined(NOCURL) 407 | "-light" 408 | #endif 409 | ; 410 | } 411 | 412 | DLL_EXPORT_LIBQUICKMAIL int quickmail_initialize () 413 | { 414 | /* 415 | #if defined(NOCURL) && defined(_WIN32) 416 | static WSADATA wsaData; 417 | int wsaerr = WSAStartup(MAKEWORD(1, 0), &wsaData); 418 | if (wsaerr) 419 | return -1; 420 | atexit((void(*)())WSACleanup); 421 | #endif 422 | */ 423 | #ifndef NOCURL 424 | curl_global_init(CURL_GLOBAL_ALL); 425 | #endif 426 | return 0; 427 | } 428 | 429 | DLL_EXPORT_LIBQUICKMAIL int quickmail_cleanup () 430 | { 431 | #ifndef NOCURL 432 | curl_global_cleanup(); 433 | #endif 434 | return 0; 435 | } 436 | 437 | DLL_EXPORT_LIBQUICKMAIL quickmail quickmail_create (const char* from, const char* subject) 438 | { 439 | int i; 440 | struct email_info_struct* mailobj; 441 | if ((mailobj = (struct email_info_struct*)malloc(sizeof(struct email_info_struct))) == NULL) { 442 | DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 443 | return NULL; 444 | } 445 | mailobj->current = 0; 446 | mailobj->timestamp = time(NULL); 447 | mailobj->from = (from ? strdup(from) : NULL); 448 | mailobj->to = NULL; 449 | mailobj->cc = NULL; 450 | mailobj->bcc = NULL; 451 | mailobj->subject = (subject ? strdup(subject) : NULL); 452 | mailobj->header = NULL; 453 | mailobj->bodylist = NULL; 454 | mailobj->attachmentlist = NULL; 455 | mailobj->buf = NULL; 456 | mailobj->buflen = 0; 457 | mailobj->mime_boundary_body = NULL; 458 | mailobj->mime_boundary_part = NULL; 459 | mailobj->current_attachment = NULL; 460 | mailobj->debuglog = NULL; 461 | for (i = 0; i < 26; i++) { 462 | mailobj->dtable[i] = (char)('A' + i); 463 | mailobj->dtable[26 + i] = (char)('a' + i); 464 | } 465 | for (i = 0; i < 10; i++) { 466 | mailobj->dtable[52 + i] = (char)('0' + i); 467 | } 468 | mailobj->dtable[62] = '+'; 469 | mailobj->dtable[63] = '/'; 470 | srand(time(NULL)); 471 | return mailobj; 472 | } 473 | 474 | DLL_EXPORT_LIBQUICKMAIL void quickmail_destroy (quickmail mailobj) 475 | { 476 | free(mailobj->from); 477 | email_info_string_list_free(&mailobj->to); 478 | email_info_string_list_free(&mailobj->cc); 479 | email_info_string_list_free(&mailobj->bcc); 480 | free(mailobj->subject); 481 | free(mailobj->header); 482 | email_info_attachment_list_free(&mailobj->bodylist); 483 | email_info_attachment_list_free(&mailobj->attachmentlist); 484 | free(mailobj->buf); 485 | free(mailobj->mime_boundary_body); 486 | free(mailobj->mime_boundary_part); 487 | free(mailobj); 488 | } 489 | 490 | DLL_EXPORT_LIBQUICKMAIL void quickmail_set_from (quickmail mailobj, const char* from) 491 | { 492 | free(mailobj->from); 493 | mailobj->from = strdup(from); 494 | } 495 | 496 | DLL_EXPORT_LIBQUICKMAIL const char* quickmail_get_from (quickmail mailobj) 497 | { 498 | return mailobj->from; 499 | } 500 | 501 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_to (quickmail mailobj, const char* email) 502 | { 503 | email_info_string_list_add(&mailobj->to, email); 504 | } 505 | 506 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_cc (quickmail mailobj, const char* email) 507 | { 508 | email_info_string_list_add(&mailobj->cc, email); 509 | } 510 | 511 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_bcc (quickmail mailobj, const char* email) 512 | { 513 | email_info_string_list_add(&mailobj->bcc, email); 514 | } 515 | 516 | DLL_EXPORT_LIBQUICKMAIL void quickmail_set_subject (quickmail mailobj, const char* subject) 517 | { 518 | free(mailobj->subject); 519 | mailobj->subject = (subject ? strdup(subject) : NULL); 520 | } 521 | 522 | DLL_EXPORT_LIBQUICKMAIL const char* quickmail_get_subject (quickmail mailobj) 523 | { 524 | return mailobj->subject; 525 | } 526 | 527 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_header (quickmail mailobj, const char* headerline) 528 | { 529 | str_append(&mailobj->header, headerline); 530 | str_append(&mailobj->header, NEWLINE); 531 | } 532 | 533 | DLL_EXPORT_LIBQUICKMAIL void quickmail_set_body (quickmail mailobj, const char* body) 534 | { 535 | email_info_attachment_list_free(&mailobj->bodylist); 536 | if (body) 537 | email_info_attachment_list_add_memory(&mailobj->bodylist, default_mime_type, default_mime_type, strdup(body), strlen(body), 1); 538 | } 539 | 540 | DLL_EXPORT_LIBQUICKMAIL char* quickmail_get_body (quickmail mailobj) 541 | { 542 | size_t n; 543 | char* p; 544 | char* result = NULL; 545 | size_t resultlen = 0; 546 | if (mailobj->bodylist && (mailobj->bodylist->handle = mailobj->bodylist->email_info_attachment_open(mailobj->bodylist->filedata)) != NULL) { 547 | do { 548 | if ((p = (char*)realloc(result, resultlen + BODY_BUFFER_SIZE)) == NULL) { 549 | free(result); 550 | result = NULL; 551 | DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 552 | break; 553 | } 554 | result = p; 555 | if ((n = mailobj->bodylist->email_info_attachment_read(mailobj->bodylist->handle, result + resultlen, BODY_BUFFER_SIZE)) > 0) 556 | resultlen += n; 557 | } while (n > 0); 558 | if (mailobj->bodylist->email_info_attachment_close) 559 | mailobj->bodylist->email_info_attachment_close(mailobj->bodylist->handle); 560 | //else 561 | // free(mailobj->bodylist->handle); 562 | mailobj->bodylist->handle = NULL; 563 | } 564 | return result; 565 | } 566 | 567 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_body_file (quickmail mailobj, const char* mimetype, const char* path) 568 | { 569 | email_info_attachment_list_add(&mailobj->bodylist, NULL, (mimetype ? mimetype : default_mime_type), (void*)strdup(path), email_info_attachment_open_file, email_info_attachment_read_file, email_info_attachment_close_file, NULL); 570 | } 571 | 572 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_body_memory (quickmail mailobj, const char* mimetype, char* data, size_t datalen, int mustfree) 573 | { 574 | email_info_attachment_list_add_memory(&mailobj->bodylist, NULL, (mimetype ? mimetype : default_mime_type), data, datalen, mustfree); 575 | } 576 | 577 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_body_custom (quickmail mailobj, const char* mimetype, char* data, quickmail_attachment_open_fn attachment_data_open, quickmail_attachment_read_fn attachment_data_read, quickmail_attachment_close_fn attachment_data_close, quickmail_attachment_free_filedata_fn attachment_data_filedata_free) 578 | { 579 | email_info_attachment_list_add(&mailobj->bodylist, NULL, (mimetype ? mimetype : default_mime_type), data, (attachment_data_open ? attachment_data_open : email_info_attachment_open_dummy), (attachment_data_read ? attachment_data_read : email_info_attachment_read_dummy), attachment_data_close, attachment_data_filedata_free); 580 | } 581 | 582 | DLL_EXPORT_LIBQUICKMAIL int quickmail_remove_body (quickmail mailobj, const char* mimetype) 583 | { 584 | return email_info_attachment_list_delete(&mailobj->bodylist, mimetype); 585 | } 586 | 587 | DLL_EXPORT_LIBQUICKMAIL void quickmail_list_bodies (quickmail mailobj, quickmail_list_attachment_callback_fn callback, void* callbackdata) 588 | { 589 | struct email_info_attachment_list_struct* p = mailobj->bodylist; 590 | while (p) { 591 | callback(mailobj, p->filename, p->mimetype, p->email_info_attachment_open, p->email_info_attachment_read, p->email_info_attachment_close, callbackdata); 592 | p = p->next; 593 | } 594 | } 595 | 596 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_attachment_file (quickmail mailobj, const char* path, const char* mimetype) 597 | { 598 | email_info_attachment_list_add_file(&mailobj->attachmentlist, path, mimetype); 599 | } 600 | 601 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_attachment_memory (quickmail mailobj, const char* filename, const char* mimetype, char* data, size_t datalen, int mustfree) 602 | { 603 | email_info_attachment_list_add_memory(&mailobj->attachmentlist, filename, mimetype, data, datalen, mustfree); 604 | } 605 | 606 | DLL_EXPORT_LIBQUICKMAIL void quickmail_add_attachment_custom (quickmail mailobj, const char* filename, const char* mimetype, char* data, quickmail_attachment_open_fn attachment_data_open, quickmail_attachment_read_fn attachment_data_read, quickmail_attachment_close_fn attachment_data_close, quickmail_attachment_free_filedata_fn attachment_data_filedata_free) 607 | { 608 | email_info_attachment_list_add(&mailobj->attachmentlist, filename, mimetype, data, (attachment_data_open ? attachment_data_open : email_info_attachment_open_dummy), (attachment_data_read ? attachment_data_read : email_info_attachment_read_dummy), attachment_data_close, attachment_data_filedata_free); 609 | } 610 | 611 | DLL_EXPORT_LIBQUICKMAIL int quickmail_remove_attachment (quickmail mailobj, const char* filename) 612 | { 613 | return email_info_attachment_list_delete(&mailobj->attachmentlist, filename); 614 | } 615 | 616 | DLL_EXPORT_LIBQUICKMAIL void quickmail_list_attachments (quickmail mailobj, quickmail_list_attachment_callback_fn callback, void* callbackdata) 617 | { 618 | struct email_info_attachment_list_struct* p = mailobj->attachmentlist; 619 | while (p) { 620 | callback(mailobj, p->filename, p->mimetype, p->email_info_attachment_open, p->email_info_attachment_read, p->email_info_attachment_close, callbackdata); 621 | p = p->next; 622 | } 623 | } 624 | 625 | DLL_EXPORT_LIBQUICKMAIL void quickmail_set_debug_log (quickmail mailobj, FILE* filehandle) 626 | { 627 | mailobj->debuglog = filehandle; 628 | } 629 | 630 | DLL_EXPORT_LIBQUICKMAIL void quickmail_fsave (quickmail mailobj, FILE* filehandle) 631 | { 632 | int i; 633 | size_t n; 634 | char buf[80]; 635 | while ((n = quickmail_get_data(buf, sizeof(buf), 1, mailobj)) > 0) { 636 | for (i = 0; i < n; i++) 637 | fprintf(filehandle, "%c", buf[i]); 638 | } 639 | } 640 | 641 | DLL_EXPORT_LIBQUICKMAIL size_t quickmail_get_data (void* ptr, size_t size, size_t nmemb, void* userp) 642 | { 643 | struct email_info_struct* mailobj = (struct email_info_struct*)userp; 644 | 645 | //abort if no data is requested 646 | if (size * nmemb == 0) 647 | return 0; 648 | 649 | //initialize on first run 650 | if (mailobj->current == MAILPART_INITIALIZE) { 651 | free(mailobj->buf); 652 | mailobj->buf = NULL; 653 | mailobj->buflen = 0; 654 | free(mailobj->mime_boundary_body); 655 | mailobj->mime_boundary_body = NULL; 656 | free(mailobj->mime_boundary_part); 657 | mailobj->mime_boundary_part = NULL; 658 | mailobj->current_attachment = mailobj->bodylist; 659 | mailobj->current++; 660 | } 661 | 662 | //process current part of mail if no partial data is pending 663 | while (mailobj->buflen == 0) { 664 | if (mailobj->buflen == 0 && mailobj->current == MAILPART_HEADER) { 665 | char* s; 666 | //generate header part 667 | char** p = &mailobj->buf; 668 | str_append(p, "User-Agent: libquickmail v" LIBQUICKMAIL_VERSION NEWLINE); 669 | 670 | #ifndef ARDUINO 671 | if (mailobj->timestamp != 0) { 672 | char* oldlocale; 673 | char timestamptext[32]; 674 | //get original locale settings and switch to C locale (so date is in English) 675 | if ((oldlocale = setlocale(LC_TIME, NULL)) != NULL) { 676 | oldlocale = strdup(oldlocale); 677 | setlocale(LC_TIME, "C"); 678 | } 679 | //format timestamp 680 | if (strftime(timestamptext, sizeof(timestamptext), "%a, %d %b %Y %H:%M:%S %z", localtime(&mailobj->timestamp))) { 681 | str_append(p, "Date: "); 682 | str_append(p, timestamptext); 683 | str_append(p, NEWLINE); 684 | } 685 | #ifdef _WIN32 686 | //fallback method for Windows when %z (time zone offset) fails 687 | else if (strftime(timestamptext, sizeof(timestamptext), "%a, %d %b %Y %H:%M:%S", localtime(&mailobj->timestamp))) { 688 | TIME_ZONE_INFORMATION tzinfo; 689 | DWORD result; 690 | if ((result = GetTimeZoneInformation(&tzinfo)) != TIME_ZONE_ID_INVALID) { 691 | LONG bias = tzinfo.Bias + (result == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : tzinfo.StandardBias); 692 | sprintf(timestamptext + strlen(timestamptext), " %c%02i%02i", (bias > 0 ? '-' : '+'), (int)-bias / 60, (int)-bias % 60); 693 | } 694 | str_append(p, "Date: "); 695 | str_append(p, timestamptext); 696 | str_append(p, NEWLINE); 697 | } 698 | #endif 699 | //restore original locale 700 | if (oldlocale) { 701 | setlocale(LC_TIME, oldlocale); 702 | free(oldlocale); 703 | } 704 | } 705 | #endif 706 | if (mailobj->from && *mailobj->from) { 707 | str_append(p, "From: <"); 708 | str_append(p, mailobj->from); 709 | str_append(p, ">" NEWLINE); 710 | } 711 | if ((s = email_info_string_list_concatenate(mailobj->to)) != NULL) { 712 | str_append(p, "To: "); 713 | str_append(p, s); 714 | str_append(p, NEWLINE); 715 | free(s); 716 | } 717 | if ((s = email_info_string_list_concatenate(mailobj->cc)) != NULL) { 718 | str_append(p, "Cc: "); 719 | str_append(p, s); 720 | str_append(p, NEWLINE); 721 | free(s); 722 | } 723 | if (mailobj->subject) { 724 | str_append(p, "Subject: "); 725 | str_append(p, mailobj->subject); 726 | str_append(p, NEWLINE); 727 | } 728 | if (mailobj->header) { 729 | str_append(p, mailobj->header); 730 | } 731 | if (mailobj->attachmentlist) { 732 | str_append(p, "MIME-Version: 1.0" NEWLINE); 733 | } 734 | if (mailobj->attachmentlist) { 735 | mailobj->mime_boundary_part = randomize_zeros(strdup("=PART=SEPARATOR=_0000_0000_0000_0000_0000_0000_=")); 736 | str_append(p, "Content-Type: multipart/mixed; boundary=\""); 737 | str_append(p, mailobj->mime_boundary_part); 738 | str_append(p, "\"" NEWLINE NEWLINE "This is a multipart message in MIME format." NEWLINE NEWLINE "--"); 739 | str_append(p, mailobj->mime_boundary_part); 740 | str_append(p, NEWLINE); 741 | } 742 | if (mailobj->bodylist && mailobj->bodylist->next) { 743 | mailobj->mime_boundary_body = randomize_zeros(strdup("=BODY=SEPARATOR=_0000_0000_0000_0000_0000_0000_=")); 744 | str_append(p, "Content-Type: multipart/alternative; boundary=\""); 745 | str_append(p, mailobj->mime_boundary_body); 746 | str_append(p, "\"" NEWLINE); 747 | } 748 | mailobj->buflen = (mailobj->buf ? strlen(mailobj->buf) : 0); 749 | mailobj->current++; 750 | } 751 | if (mailobj->buflen == 0 && mailobj->current == MAILPART_BODY) { 752 | if (mailobj->current_attachment) { 753 | if (!mailobj->current_attachment->handle) { 754 | //open file with body data 755 | while (mailobj->current_attachment) { 756 | if ((mailobj->current_attachment->handle = mailobj->current_attachment->email_info_attachment_open(mailobj->current_attachment->filedata)) != NULL) { 757 | break; 758 | } 759 | /////to do: notify/log the file could not be opened 760 | mailobj->current_attachment = mailobj->current_attachment->next; 761 | } 762 | if (!mailobj->current_attachment) { 763 | mailobj->current_attachment = mailobj->attachmentlist; 764 | mailobj->current++; 765 | } 766 | //generate attachment header 767 | if (mailobj->current_attachment && mailobj->current_attachment->handle) { 768 | mailobj->buf = NULL; 769 | if (mailobj->mime_boundary_body) { 770 | mailobj->buf = str_append(&mailobj->buf, NEWLINE "--"); 771 | mailobj->buf = str_append(&mailobj->buf, mailobj->mime_boundary_body); 772 | mailobj->buf = str_append(&mailobj->buf, NEWLINE); 773 | } 774 | mailobj->buf = str_append(&mailobj->buf, "Content-Type: "); 775 | mailobj->buf = str_append(&mailobj->buf, (mailobj->bodylist && mailobj->current_attachment->mimetype ? mailobj->current_attachment->mimetype : default_mime_type)); 776 | mailobj->buf = str_append(&mailobj->buf, NEWLINE "Content-Transfer-Encoding: 8bit" NEWLINE "Content-Disposition: inline" NEWLINE NEWLINE); 777 | mailobj->buflen = (mailobj->buf ? strlen(mailobj->buf) : 0); 778 | } 779 | } 780 | if (mailobj->buflen == 0 && mailobj->current_attachment && mailobj->current_attachment->handle) { 781 | //read body data 782 | if ((mailobj->buf = (char*) malloc(BODY_BUFFER_SIZE)) == NULL) { 783 | DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 784 | } 785 | if (mailobj->buf == NULL || (mailobj->buflen = mailobj->current_attachment->email_info_attachment_read(mailobj->current_attachment->handle, mailobj->buf, BODY_BUFFER_SIZE)) <= 0) { 786 | //end of file 787 | free(mailobj->buf); 788 | mailobj->buflen = 0; 789 | if (mailobj->current_attachment->email_info_attachment_close) 790 | mailobj->current_attachment->email_info_attachment_close(mailobj->current_attachment->handle); 791 | //else 792 | // free(mailobj->current_attachment->handle); 793 | mailobj->current_attachment->handle = NULL; 794 | mailobj->current_attachment = mailobj->current_attachment->next; 795 | } 796 | } 797 | } else { 798 | mailobj->current_attachment = mailobj->attachmentlist; 799 | mailobj->current++; 800 | } 801 | } 802 | if (mailobj->buflen == 0 && mailobj->current == MAILPART_BODY_DONE) { 803 | mailobj->buf = NULL; 804 | if (mailobj->mime_boundary_body) { 805 | mailobj->buf = str_append(&mailobj->buf, NEWLINE "--"); 806 | mailobj->buf = str_append(&mailobj->buf, mailobj->mime_boundary_body); 807 | mailobj->buf = str_append(&mailobj->buf, "--" NEWLINE); 808 | mailobj->buflen = strlen(mailobj->buf); 809 | free(mailobj->mime_boundary_body); 810 | mailobj->mime_boundary_body = NULL; 811 | } 812 | mailobj->current++; 813 | } 814 | if (mailobj->buflen == 0 && mailobj->current == MAILPART_ATTACHMENT) { 815 | if (mailobj->current_attachment) { 816 | if (!mailobj->current_attachment->handle) { 817 | //open file to attach 818 | while (mailobj->current_attachment) { 819 | if ((mailobj->current_attachment->handle = mailobj->current_attachment->email_info_attachment_open(mailobj->current_attachment->filedata)) != NULL) { 820 | break; 821 | } 822 | mailobj->current_attachment = mailobj->current_attachment->next; 823 | } 824 | //generate attachment header 825 | if (mailobj->current_attachment && mailobj->current_attachment->handle) { 826 | mailobj->buf = NULL; 827 | if (mailobj->mime_boundary_part) { 828 | mailobj->buf = str_append(&mailobj->buf, NEWLINE "--"); 829 | mailobj->buf = str_append(&mailobj->buf, mailobj->mime_boundary_part); 830 | mailobj->buf = str_append(&mailobj->buf, NEWLINE); 831 | } 832 | mailobj->buf = str_append(&mailobj->buf, "Content-Type: "); 833 | mailobj->buf = str_append(&mailobj->buf, (mailobj->current_attachment->mimetype ? mailobj->current_attachment->mimetype : "application/octet-stream")); 834 | mailobj->buf = str_append(&mailobj->buf, "; Name=\""); 835 | mailobj->buf = str_append(&mailobj->buf, (mailobj->current_attachment->filename ? mailobj->current_attachment->filename : "ATTACHMENT")); 836 | mailobj->buf = str_append(&mailobj->buf, "\"" NEWLINE "Content-Disposition: attachment; filename=\""); 837 | mailobj->buf = str_append(&mailobj->buf, (mailobj->current_attachment->filename ? mailobj->current_attachment->filename : "ATTACHMENT")); 838 | mailobj->buf = str_append(&mailobj->buf, "\"" NEWLINE "Content-Transfer-Encoding: base64" NEWLINE NEWLINE); 839 | mailobj->buflen = strlen(mailobj->buf); 840 | } 841 | } else { 842 | //generate next line of attachment data 843 | size_t n = 0; 844 | int mimelinepos = 0; 845 | unsigned char igroup[3] = {0, 0, 0}; 846 | unsigned char ogroup[4]; 847 | mailobj->buflen = 0; 848 | if ((mailobj->buf = (char*)malloc(MIME_LINE_WIDTH + NEWLINELENGTH + 1)) == NULL) { 849 | DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 850 | n = 0; 851 | } else { 852 | while (mimelinepos < MIME_LINE_WIDTH && (n = mailobj->current_attachment->email_info_attachment_read(mailobj->current_attachment->handle, igroup, 3)) > 0) { 853 | //code data 854 | ogroup[0] = mailobj->dtable[igroup[0] >> 2]; 855 | ogroup[1] = mailobj->dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)]; 856 | ogroup[2] = mailobj->dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)]; 857 | ogroup[3] = mailobj->dtable[igroup[2] & 0x3F]; 858 | //padd with "=" characters if less than 3 characters were read 859 | if (n < 3) { 860 | ogroup[3] = '='; 861 | if (n < 2) 862 | ogroup[2] = '='; 863 | } 864 | memcpy(mailobj->buf + mimelinepos, ogroup, 4); 865 | mailobj->buflen += 4; 866 | mimelinepos += 4; 867 | } 868 | if (mimelinepos > 0) { 869 | memcpy(mailobj->buf + mimelinepos, NEWLINE, NEWLINELENGTH); 870 | mailobj->buflen += NEWLINELENGTH; 871 | } 872 | } 873 | if (n <= 0) { 874 | //end of file 875 | if (mailobj->current_attachment->email_info_attachment_close) 876 | mailobj->current_attachment->email_info_attachment_close(mailobj->current_attachment->handle); 877 | else 878 | free(mailobj->current_attachment->handle); 879 | mailobj->current_attachment->handle = NULL; 880 | mailobj->current_attachment = mailobj->current_attachment->next; 881 | } 882 | } 883 | } else { 884 | mailobj->current++; 885 | } 886 | } 887 | if (mailobj->buflen == 0 && mailobj->current == MAILPART_END) { 888 | mailobj->buf = NULL; 889 | mailobj->buflen = 0; 890 | if (mailobj->mime_boundary_part) { 891 | mailobj->buf = str_append(&mailobj->buf, NEWLINE "--"); 892 | mailobj->buf = str_append(&mailobj->buf, mailobj->mime_boundary_part); 893 | mailobj->buf = str_append(&mailobj->buf, "--" NEWLINE); 894 | mailobj->buflen = strlen(mailobj->buf); 895 | free(mailobj->mime_boundary_part); 896 | mailobj->mime_boundary_part = NULL; 897 | } 898 | //mailobj->buf = str_append(&mailobj->buf, NEWLINE "." NEWLINE); 899 | //mailobj->buflen = strlen(mailobj->buf); 900 | mailobj->current++; 901 | } 902 | if (mailobj->buflen == 0 && mailobj->current == MAILPART_DONE) { 903 | break; 904 | } 905 | } 906 | 907 | //flush pending data if any 908 | if (mailobj->buflen > 0) { 909 | int len = (mailobj->buflen > size * nmemb ? size * nmemb : mailobj->buflen); 910 | memcpy(ptr, mailobj->buf, len); 911 | if (len < mailobj->buflen) { 912 | mailobj->buf = (char*) memmove(mailobj->buf, mailobj->buf + len, mailobj->buflen - len); 913 | mailobj->buflen -= len; 914 | } else { 915 | free(mailobj->buf); 916 | mailobj->buf = NULL; 917 | mailobj->buflen = 0; 918 | } 919 | return len; 920 | } 921 | 922 | //if (mailobj->current != MAILPART_DONE) 923 | // ;//this should never be reached 924 | mailobj->current = 0; 925 | return 0; 926 | } 927 | 928 | #ifndef NOCURL 929 | char* add_angle_brackets (const char* data) 930 | { 931 | size_t datalen = strlen(data); 932 | char* result; 933 | if ((result = (char*)malloc(datalen + 3)) == NULL) { 934 | DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 935 | return NULL; 936 | } 937 | result[0] = '<'; 938 | memcpy(result + 1, data, datalen); 939 | result[datalen + 1] = '>'; 940 | result[datalen + 2] = 0; 941 | return result; 942 | } 943 | #endif 944 | 945 | #define QUICKMAIL_PROT_SMTP 1 946 | #define QUICKMAIL_PROT_SMTPS 2 947 | 948 | const char* quickmail_protocol_send (quickmail mailobj, const char* smtpserver, unsigned int smtpport, int protocol, const char* username, const char* password) 949 | { 950 | #ifndef NOCURL 951 | //libcurl based sending 952 | CURL *curl; 953 | CURLcode result = CURLE_FAILED_INIT; 954 | if ((curl = curl_easy_init()) != NULL) { 955 | struct curl_slist *recipients = NULL; 956 | struct email_info_email_list_struct* listentry; 957 | //set destination URL 958 | char* addr; 959 | size_t len = strlen(smtpserver) + 14; 960 | if ((addr = (char*)malloc(len)) == NULL) { 961 | DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 962 | return ERRMSG_MEMORY_ALLOCATION_ERROR; 963 | } 964 | snprintf(addr, len, "%s://%s:%u", (protocol == QUICKMAIL_PROT_SMTPS ? "smtps" : "smtp"), smtpserver, smtpport); 965 | curl_easy_setopt(curl, CURLOPT_URL, addr); 966 | free(addr); 967 | //try Transport Layer Security (TLS), but continue anyway if it fails 968 | curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_TRY); 969 | //don't fail if the TLS/SSL a certificate could not be verified 970 | //alternative: add the issuer certificate (or the host certificate if 971 | //the certificate is self-signed) to the set of certificates that are 972 | //known to libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH 973 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); 974 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); 975 | //set authentication credentials if provided 976 | if (username && *username) 977 | curl_easy_setopt(curl, CURLOPT_USERNAME, username); 978 | if (password) 979 | curl_easy_setopt(curl, CURLOPT_PASSWORD, password); 980 | //set from value for envelope reverse-path 981 | if (mailobj->from && *mailobj->from) { 982 | addr = add_angle_brackets(mailobj->from); 983 | curl_easy_setopt(curl, CURLOPT_MAIL_FROM, addr); 984 | free(addr); 985 | } 986 | //set recipients 987 | listentry = mailobj->to; 988 | while (listentry) { 989 | if (listentry->data && *listentry->data) { 990 | addr = add_angle_brackets(listentry->data); 991 | recipients = curl_slist_append(recipients, addr); 992 | free(addr); 993 | } 994 | listentry = listentry->next; 995 | } 996 | listentry = mailobj->cc; 997 | while (listentry) { 998 | if (listentry->data && *listentry->data) { 999 | addr = add_angle_brackets(listentry->data); 1000 | recipients = curl_slist_append(recipients, addr); 1001 | free(addr); 1002 | } 1003 | listentry = listentry->next; 1004 | } 1005 | listentry = mailobj->bcc; 1006 | while (listentry) { 1007 | if (listentry->data && *listentry->data) { 1008 | addr = add_angle_brackets(listentry->data); 1009 | recipients = curl_slist_append(recipients, addr); 1010 | free(addr); 1011 | } 1012 | listentry = listentry->next; 1013 | } 1014 | curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); 1015 | //set callback function for getting message body 1016 | curl_easy_setopt(curl, CURLOPT_READFUNCTION, quickmail_get_data); 1017 | curl_easy_setopt(curl, CURLOPT_READDATA, mailobj); 1018 | curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); //set CURLOPT_UPLOAD to 1 to not use VRFY and other unneeded commands 1019 | //enable debugging if requested 1020 | if (mailobj->debuglog) { 1021 | curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 1022 | curl_easy_setopt(curl, CURLOPT_STDERR, mailobj->debuglog); 1023 | } 1024 | //send the message 1025 | result = curl_easy_perform(curl); 1026 | //free the list of recipients and clean up 1027 | curl_slist_free_all(recipients); 1028 | curl_easy_cleanup(curl); 1029 | } 1030 | return (result == CURLE_OK ? NULL : curl_easy_strerror(result)); 1031 | #else 1032 | //minimal implementation without libcurl 1033 | SOCKET sock; 1034 | char* errmsg = NULL; 1035 | struct email_info_email_list_struct* listentry; 1036 | char local_hostname[64]; 1037 | int statuscode; 1038 | //SMTPS not supported without libcurl 1039 | if (protocol == QUICKMAIL_PROT_SMTPS) { 1040 | return "SMTPS not supported"; 1041 | } 1042 | //determine local host name 1043 | #ifndef ARDUINO 1044 | if (gethostname(local_hostname, sizeof(local_hostname)) != 0) 1045 | strcpy(local_hostname, "localhost"); 1046 | #else 1047 | strcpy(local_hostname, "localhost"); 1048 | #endif 1049 | //connect 1050 | if ((sock = socket_open(smtpserver, smtpport, &errmsg)) != INVALID_SOCKET) { 1051 | //talk with SMTP server 1052 | if ((statuscode = socket_smtp_command(sock, mailobj->debuglog, NULL)) >= 400) { 1053 | errmsg = "SMTP server returned an error on connection"; 1054 | } else { 1055 | size_t n; 1056 | char buf[WRITE_BUFFER_CHUNK_SIZE]; 1057 | do { 1058 | if ((statuscode = socket_smtp_command(sock, mailobj->debuglog, "EHLO %s", local_hostname)) >= 400) { 1059 | if ((statuscode = socket_smtp_command(sock, mailobj->debuglog, "HELO %s", local_hostname)) >= 400) { 1060 | errmsg = "SMTP EHLO/HELO returned error"; 1061 | break; 1062 | } 1063 | } 1064 | //authenticate if needed 1065 | if (username || password) { 1066 | int len; 1067 | int inpos = 0; 1068 | int outpos = 0; 1069 | size_t usernamelen = (username ? strlen(username) : 0); 1070 | size_t passwordlen = (password ? strlen(password) : 0); 1071 | char* auth; 1072 | char* base64auth; 1073 | if ((auth = (char*)malloc(usernamelen + passwordlen + 5 + 5)) == NULL) { 1074 | DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 1075 | return ERRMSG_MEMORY_ALLOCATION_ERROR; 1076 | } 1077 | if ((base64auth = (char*)malloc(((usernamelen + passwordlen + 2) + 2) / 3 * 4 + 1)) == NULL) { 1078 | DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR) 1079 | return ERRMSG_MEMORY_ALLOCATION_ERROR; 1080 | } 1081 | //leave the authorization identity empty to indicate it's the same the as authentication identity 1082 | auth[0] = 0; 1083 | len = 1; 1084 | //set the authentication identity 1085 | memcpy(auth + len, (username ? username : ""), usernamelen + 1); 1086 | len += usernamelen + 1; 1087 | //set the password 1088 | memcpy(auth + len, (password ? password : ""), passwordlen + 1); 1089 | len += passwordlen; 1090 | //padd with extra zeros so groups of 3 bytes can be read 1091 | auth[usernamelen + len + 1] = 0; 1092 | auth[usernamelen + len + 2] = 0; 1093 | //encode in base64 1094 | while (inpos < len) { 1095 | //encode data 1096 | base64auth[outpos + 0] = mailobj->dtable[auth[inpos + 0] >> 2]; 1097 | base64auth[outpos + 1] = mailobj->dtable[((auth[inpos + 0] & 3) << 4) | (auth[inpos + 1] >> 4)]; 1098 | base64auth[outpos + 2] = mailobj->dtable[((auth[inpos + 1] & 0xF) << 2) | (auth[inpos + 2] >> 6)]; 1099 | base64auth[outpos + 3] = mailobj->dtable[auth[inpos + 2] & 0x3F]; 1100 | //padd with "=" characters if less than 3 characters were read 1101 | if (inpos + 2 >= len) { 1102 | base64auth[outpos + 3] = '='; 1103 | if (inpos + 1 >= len) 1104 | base64auth[outpos + 2] = '='; 1105 | } 1106 | //advance to next position 1107 | inpos += 3; 1108 | outpos += 4; 1109 | } 1110 | base64auth[outpos] = 0; 1111 | //send originator e-mail address 1112 | statuscode = socket_smtp_command(sock, mailobj->debuglog, "AUTH PLAIN %s", base64auth); 1113 | //clean up 1114 | free(auth); 1115 | free(base64auth); 1116 | //error if authentication failed 1117 | if (statuscode >= 400) { 1118 | errmsg = "SMTP authentication failed"; 1119 | break; 1120 | } 1121 | } 1122 | //send originator e-mail address 1123 | if ((statuscode = socket_smtp_command(sock, mailobj->debuglog, "MAIL FROM:<%s>", mailobj->from)) >= 400) { 1124 | errmsg = "SMTP server did not accept sender"; 1125 | break; 1126 | } 1127 | //send recipient e-mail addresses 1128 | listentry = mailobj->to; 1129 | while (!errmsg && listentry) { 1130 | if (listentry->data && *listentry->data) { 1131 | if ((statuscode = socket_smtp_command(sock, mailobj->debuglog, "RCPT TO:<%s>", listentry->data)) >= 400) 1132 | errmsg = "SMTP server did not accept e-mail address (To)"; 1133 | } 1134 | listentry = listentry->next; 1135 | } 1136 | listentry = mailobj->cc; 1137 | while (!errmsg && listentry) { 1138 | if (listentry->data && *listentry->data) { 1139 | if ((statuscode = socket_smtp_command(sock, mailobj->debuglog, "RCPT TO:<%s>", listentry->data)) >= 400) 1140 | errmsg = "SMTP server did not accept e-mail address (CC)"; 1141 | } 1142 | listentry = listentry->next; 1143 | } 1144 | listentry = mailobj->bcc; 1145 | while (!errmsg && listentry) { 1146 | if (listentry->data && *listentry->data) { 1147 | if ((statuscode = socket_smtp_command(sock, mailobj->debuglog, "RCPT TO:<%s>", listentry->data)) >= 400) 1148 | errmsg = "SMTP server did not accept e-mail address (BCC)"; 1149 | } 1150 | listentry = listentry->next; 1151 | } 1152 | if (errmsg) 1153 | break; 1154 | //prepare to send mail body 1155 | if ((statuscode = socket_smtp_command(sock, mailobj->debuglog, "DATA")) >= 400) { 1156 | errmsg = "SMTP DATA returned error"; 1157 | break; 1158 | } 1159 | //send mail body data 1160 | while ((n = quickmail_get_data(buf, sizeof(buf), 1, mailobj)) > 0) { 1161 | socket_send(sock, buf, n); 1162 | } 1163 | //send end of data 1164 | if ((statuscode = socket_smtp_command(sock, mailobj->debuglog, "\r\n.")) >= 400) { 1165 | errmsg = "SMTP error after sending message data"; 1166 | break; 1167 | } 1168 | } while (0); 1169 | //log out 1170 | socket_smtp_command(sock, mailobj->debuglog, "QUIT"); 1171 | } 1172 | } 1173 | //close socket 1174 | socket_close(sock); 1175 | return errmsg; 1176 | #endif 1177 | } 1178 | 1179 | DLL_EXPORT_LIBQUICKMAIL const char* quickmail_send (quickmail mailobj, const char* smtpserver, unsigned int smtpport, const char* username, const char* password) 1180 | { 1181 | return quickmail_protocol_send(mailobj, smtpserver, smtpport, QUICKMAIL_PROT_SMTP, username, password); 1182 | } 1183 | DLL_EXPORT_LIBQUICKMAIL const char* quickmail_send_secure (quickmail mailobj, const char* smtpserver, unsigned int smtpport, const char* username, const char* password) 1184 | { 1185 | return quickmail_protocol_send(mailobj, smtpserver, smtpport, QUICKMAIL_PROT_SMTPS, username, password); 1186 | } 1187 | --------------------------------------------------------------------------------