Advertisement
Guest User

curl - send email

a guest
Sep 3rd, 2016
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.72 KB | None | 0 0
  1. #include <ctime>
  2. #include <cstring>
  3. #include <curl/curl.h>
  4.  
  5. std::string dateTimeNow();
  6. std::string generateMessageId();
  7.  
  8. std::string setPayloadText(const std::string &to,
  9.                            const std::string &from,
  10.                            const std::string &cc,
  11.                            const std::string &nameFrom,
  12.                            const std::string &subject,
  13.                            const std::string &body)
  14. {
  15.     std::string ret;
  16.  
  17.     ret += "Date: "  + dateTimeNow() + ">\r\n";
  18.     ret += "To: <"   + to   + ">\r\n";
  19.     ret += "From: <" + from + "> (" + nameFrom + ")\r\n";
  20.     ret += "Cc: <"   + cc   + "> (" + nameFrom + ")\r\n";
  21.     ret += "Message-ID: <"  + generateMessageId() + "@" + from.substr(from.find('@') + 1) + ">\r\n";
  22.     ret += "Subject: "      + subject + "\r\n";
  23.     ret += "\r\n";
  24.     ret += body + "\r\n";
  25.     ret += "\r\n";
  26.     ret += "\r\n"; // "It could be a lot of lines, could be MIME encoded, whatever.\r\n";
  27.     ret += "\r\n"; // "Check RFC5322.\r\n";
  28.  
  29.     return ret;
  30. }
  31.  
  32. std::string dateTimeNow()
  33. {
  34.     static const char *DAY_NAMES  [] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  35.     static const char *MONTH_NAMES[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  36.  
  37.     const int RFC1123_TIME_LEN = 29;
  38.     time_t t;
  39.     struct tm tm;
  40.  
  41.     std::string ret;
  42.     ret.resize(RFC1123_TIME_LEN);
  43.  
  44.     time(&t);
  45.     gmtime_s(&tm, &t);
  46.  
  47.     strftime(&ret[0], RFC1123_TIME_LEN + 1, "---, %d --- %Y %H:%M:%S GMT", &tm);
  48.     memcpy(&ret[0], DAY_NAMES  [tm.tm_wday], 3);
  49.     memcpy(&ret[8], MONTH_NAMES[tm.tm_mon],  3);
  50.  
  51.     return ret;
  52. }
  53.  
  54. std::string generateMessageId()
  55. {
  56.     const int MESSAGE_ID_LEN = 37;
  57.     time_t t;
  58.     struct tm tm;
  59.  
  60.     std::string ret;
  61.     ret.resize(15);
  62.  
  63.     time(&t);
  64.     gmtime_s(&tm, &t);
  65.  
  66.     strftime(const_cast<char *>(ret.c_str()),
  67.              MESSAGE_ID_LEN,
  68.              "%Y%m%d%H%M%S.",
  69.              &tm);
  70.  
  71.     ret.reserve(MESSAGE_ID_LEN);
  72.  
  73.     static const char alphanum[] =
  74.             "0123456789"
  75.             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  76.             "abcdefghijklmnopqrstuvwxyz";
  77.  
  78.     while (ret.size() < MESSAGE_ID_LEN) {
  79.         ret += alphanum[rand() % (sizeof(alphanum) - 1)];
  80.     }
  81.  
  82.     return ret;
  83. }
  84.  
  85. struct uploadStatus { int linesRead; };
  86.  
  87. struct StringData
  88. {
  89.     std::string msg;
  90.     size_t bytesleft;
  91.  
  92.     StringData(std::string &&m) : msg{ m }, bytesleft{ msg.size() } {}
  93.     StringData(std::string  &m) = delete;
  94. };
  95.  
  96. size_t payloadSource(void *ptr, size_t size, size_t nmemb, void *userp)
  97. {
  98.     StringData *text = reinterpret_cast<StringData *>(userp);
  99.  
  100.     if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1) || (text->bytesleft == 0)) {
  101.         return 0;
  102.     }
  103.  
  104.     if ((nmemb * size) >= text->msg.size()) {
  105.         text->bytesleft = 0;
  106.         return text->msg.copy(reinterpret_cast<char *>(ptr), text->msg.size());
  107.     }
  108.  
  109.     return 0;
  110. }
  111.  
  112.  
  113. CURLcode sendEmail(const std::string &to,
  114.                    const std::string &from,
  115.                    const std::string &cc,
  116.                    const std::string &nameFrom,
  117.                    const std::string &subject,
  118.                    const std::string &body,
  119.                    const std::string &url,
  120.                    const std::string &password)
  121. {
  122.     CURLcode ret = CURLE_OK;
  123.  
  124.     struct curl_slist *recipients = NULL;
  125.  
  126.     CURL *curl = curl_easy_init();
  127.  
  128.     StringData textData { setPayloadText(to, from, cc, nameFrom, subject, body) };
  129.  
  130.     if (curl) {
  131.         curl_easy_setopt(curl, CURLOPT_USERNAME,     from    .c_str());
  132.         curl_easy_setopt(curl, CURLOPT_PASSWORD,     password.c_str());
  133.         curl_easy_setopt(curl, CURLOPT_URL,          url     .c_str());
  134.  
  135.         curl_easy_setopt(curl, CURLOPT_USE_SSL,      (long)CURLUSESSL_ALL);
  136.         //curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
  137.  
  138.         curl_easy_setopt(curl, CURLOPT_MAIL_FROM,    ("<" + from + ">").c_str());
  139.         recipients = curl_slist_append(recipients,   ("<" + to   + ">").c_str());
  140.         recipients = curl_slist_append(recipients,   ("<" + cc   + ">").c_str());
  141.  
  142.         curl_easy_setopt(curl, CURLOPT_MAIL_RCPT,    recipients);
  143.         curl_easy_setopt(curl, CURLOPT_READFUNCTION, payloadSource);
  144.         curl_easy_setopt(curl, CURLOPT_READDATA,     &textData);
  145.         curl_easy_setopt(curl, CURLOPT_UPLOAD,       1L);
  146.         curl_easy_setopt(curl, CURLOPT_VERBOSE,      1L);
  147.  
  148.         ret = curl_easy_perform(curl);
  149.  
  150.         if (ret != CURLE_OK) {
  151.             fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(ret));
  152.         }
  153.  
  154.         curl_slist_free_all(recipients);
  155.         curl_easy_cleanup(curl);
  156.     }
  157.  
  158.     return ret;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement