Advertisement
Guest User

curl mail

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