Guest User

Untitled

a guest
Aug 19th, 2018
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. #include "curl/curl.h"
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #include <unistd.h>
  7. #include <memory.h>
  8. #include <string.h>
  9. #define GetCurrentDir getcwd
  10.  
  11. #include <vector>
  12. #include <fstream>
  13.  
  14. #define USERNAME "yaya@gmail.com"
  15. #define PASSWORD "yaya"
  16. #define SMTPSERVER "smtp.gmail.com"
  17. #define SMTPPORT ":587"
  18. #define RECIPIENT "<yaya@gmail.com>"
  19. #define MAILFROM "<yaya@gmail.com>"
  20.  
  21. #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
  22.  
  23. /* Note that you should include the actual meta data headers here as well if
  24. you want the mail to have a Subject, another From:, show a To: or whatever
  25. you think your mail should feature! */
  26. std::vector<std::string> text;
  27.  
  28. static int read_text(char* fname)
  29. {
  30. //Read the text file, add each line to the char array.
  31. std::ifstream myfile (fname);
  32.  
  33. std::string line;
  34. while (std::getline(myfile, line))
  35. text.push_back(line + '\n');
  36.  
  37. return 0;
  38. }
  39.  
  40. struct WriteThis {
  41. int counter;
  42. };
  43.  
  44. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
  45. {
  46. /* This was already in. */
  47. struct WriteThis *pooh = (struct WriteThis *)userp;
  48.  
  49. if(size*nmemb < 1)
  50. return 0;
  51.  
  52. if (pooh->counter < text.size())
  53. {
  54. const std::string& data = text[pooh->counter];
  55.  
  56. memcpy(ptr, data.data(), data.length());
  57. pooh->counter++; /* advance pointer */
  58. return data.length();
  59. }
  60. return 0; /* no more data left to deliver */
  61. }
  62.  
  63. static struct timeval tvnow(void)
  64. {
  65. /*
  66. ** time() returns the value of time in seconds since the Epoch.
  67. */
  68. struct timeval now;
  69. now.tv_sec = (long)time(NULL);
  70. now.tv_usec = 0;
  71. return now;
  72. }
  73.  
  74. static long tvdiff(struct timeval newer, struct timeval older)
  75. {
  76. return (newer.tv_sec-older.tv_sec)*1000+
  77. (newer.tv_usec-older.tv_usec)/1000;
  78. }
  79.  
  80. int main(int argc, char** argv)
  81. {
  82. if (argc<2)
  83. {
  84. printf("Usage: %s <email.eml>\n", argv[0]);
  85. exit(255);
  86. } else
  87. {
  88. printf("Reading email body from %s\n", argv[1]);
  89. if (0 != read_text(argv[1]))
  90. exit(254);
  91. }
  92.  
  93. CURL *curl;
  94. CURLM *mcurl;
  95. int still_running = 1;
  96. struct timeval mp_start;
  97. char mp_timedout = 0;
  98. struct WriteThis pooh;
  99. struct curl_slist* rcpt_list = NULL;
  100.  
  101. pooh.counter = 0;
  102.  
  103. curl_global_init(CURL_GLOBAL_DEFAULT);
  104.  
  105. curl = curl_easy_init();
  106. if(!curl)
  107. return 1;
  108.  
  109. mcurl = curl_multi_init();
  110. if(!mcurl)
  111. return 2;
  112.  
  113. rcpt_list = curl_slist_append(rcpt_list, RECIPIENT);
  114. /* more addresses can be added here
  115. rcpt_list = curl_slist_append(rcpt_list, "<others@example.com>");
  116. */
  117.  
  118. curl_easy_setopt(curl, CURLOPT_URL, "smtp://" SMTPSERVER SMTPPORT);
  119. curl_easy_setopt(curl, CURLOPT_USERNAME, USERNAME);
  120. curl_easy_setopt(curl, CURLOPT_PASSWORD, PASSWORD);
  121. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  122. curl_easy_setopt(curl, CURLOPT_MAIL_FROM, MAILFROM);
  123. curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt_list);
  124. curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
  125. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER,0);
  126. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
  127. curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
  128. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  129. curl_easy_setopt(curl, CURLOPT_SSLVERSION, 0);
  130. curl_easy_setopt(curl, CURLOPT_SSL_SESSIONID_CACHE, 0);
  131. curl_multi_add_handle(mcurl, curl);
  132.  
  133. mp_timedout = 0;
  134. mp_start = tvnow();
  135.  
  136. /* we start some action by calling perform right away */
  137. curl_multi_perform(mcurl, &still_running);
  138.  
  139. while(still_running) {
  140. struct timeval timeout;
  141. int rc; /* select() return code */
  142.  
  143. fd_set fdread;
  144. fd_set fdwrite;
  145. fd_set fdexcep;
  146. int maxfd = -1;
  147.  
  148. long curl_timeo = -1;
  149.  
  150. FD_ZERO(&fdread);
  151. FD_ZERO(&fdwrite);
  152. FD_ZERO(&fdexcep);
  153.  
  154. /* set a suitable timeout to play around with */
  155. timeout.tv_sec = 1;
  156. timeout.tv_usec = 0;
  157.  
  158. curl_multi_timeout(mcurl, &curl_timeo);
  159. if(curl_timeo >= 0) {
  160. timeout.tv_sec = curl_timeo / 1000;
  161. if(timeout.tv_sec > 1)
  162. timeout.tv_sec = 1;
  163. else
  164. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  165. }
  166.  
  167. /* get file descriptors from the transfers */
  168. curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
  169.  
  170. /* In a real-world program you OF COURSE check the return code of the
  171. function calls. On success, the value of maxfd is guaranteed to be
  172. greater or equal than -1. We call select(maxfd + 1, ...), specially in
  173. case of (maxfd == -1), we call select(0, ...), which is basically equal
  174. to sleep. */
  175.  
  176. //rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  177.  
  178. if (tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
  179. fprintf(stderr, "ABORTING TEST, since it seems "
  180. "that it would have run forever.\n");
  181. break;
  182. }
  183.  
  184. switch(rc) {
  185. case -1:
  186. /* select error */
  187. break;
  188. case 0: /* timeout */
  189. default: /* action */
  190. curl_multi_perform(mcurl, &still_running);
  191. break;
  192. }
  193. }
  194.  
  195. curl_slist_free_all(rcpt_list);
  196. curl_multi_remove_handle(mcurl, curl);
  197. curl_multi_cleanup(mcurl);
  198. curl_easy_cleanup(curl);
  199. curl_global_cleanup();
  200. return 0;
  201. }
Add Comment
Please, Sign In to add comment