libcurl: can't authenticate through gmail with smtp and oauth2 - c++

I'm trying to send an email through gmail using libcurl, smtp, and oauth2. I've successfully gotten an access_token and I've checked with
https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={access_token}
to make sure that it is actually a valid access_token. However, whenever I try and authenticate and send the email, it always complains with an "Authentication Failure: 334". The code works perfectly when I replace the oauth2 line with a password line.
Here is the code:
static const char *payload_text[] = {
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
"To: XXX#gmail.com\r\n",
"From: XXX#gmail.com\r\n",
"Subject: SMTP example message\r\n",
"\r\n", /* empty line to divide headers from body, see RFC5322 */
"The body of the message starts here.\r\n",
"\r\n",
"It could be a lot of lines, could be MIME encoded, whatever.\r\n",
"Check RFC5322.\r\n",
NULL
};
struct upload_status {
int lines_read;
};
static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp) {
struct upload_status *upload_ctx = (struct upload_status *)userp;
const char *data;
if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
return 0;
}
data = payload_text[upload_ctx->lines_read];
if(data) {
size_t len = strlen(data);
memcpy(ptr, data, len);
upload_ctx->lines_read++;
return len;
}
return 0;
}
void send_email() {
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *recipients = NULL;
struct upload_status upload_ctx;
upload_ctx.lines_read = 0;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_USERNAME, "XXX#gmail.com");
curl_easy_setopt(curl, CURLOPT_XOAUTH2_BEARER, access_token.c_str());
curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.gmail.com:587/");
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "XXX#gmail.com");
recipients = curl_slist_append(recipients, "XXXX#gmail.com");
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_slist_free_all(recipients);
curl_easy_cleanup(curl);
}
}
and here is the latter half of the error message (where it differs and shows the failure):
< 250-mx.google.com at your service, [108.200.141.166]
< 250-SIZE 35882577
< 250-8BITMIME
< 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
< 250-ENHANCEDSTATUSCODES
< 250 CHUNKING
> AUTH XOAUTH2
< 334
> dXNlcj1yY3dtY3Njcm9vZ2VAZ21haWwuY29tAWF1dGg9QmVhcmVyIHlhMjkuS2dCUVhCU1ZzSVpCQ1I4QUFBQzVucmdES2g3TzBQZ01JU25zRGlGSjc0c1M2N0Z5cUQycUJrVkgzdHFrV3cBAQ==
< 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
* Authentication failed: 334
* Closing connection 0
curl_easy_perform() failed: Login denied
Can anyone help explain what is going wrong here? Worse come to worse, I'll just end up using the username/password combination to log in, but I'd rather figure out what's messed up here (and I was under the impression that the oauth2 way was the preferred way to go).
Thanks in advance for any help.

Related

How to read the JSON Error response returned from Server using Libcurl

I have a requirement where I have to read the error response from backend server which returns 500 Internal Server error. The error response is in JSON Format.
Below is the code snippet used in our application
INT CCurlHTTP::HTTPSPost(const CString& endPointUrl, const CString& urlparam,const CString& cookie){
CURL *curl;
CURLcode res;
struct curl_slist *headers=NULL;
char errbuf[CURL_ERROR_SIZE];
curl = curl_easy_init();
get_request req;
req.buffer =0;
req.len =0;
req.buflen =0;
if(curl)
{
//add url, headers, and parameters to the request
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, endPointUrl);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
headers = curl_slist_append(headers, m_httpHeadAccept);
headers = curl_slist_append(headers, m_httpContentType);
//callback function used to save response
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_String);
req.buffer = (unsigned char*) malloc(CHUNK_SIZE);
req.buflen = CHUNK_SIZE;
req.len = 0;
curl_easy_setopt(curl,CURLOPT_WRITEDATA, (void *)&req);
if (!cookie.IsEmpty())
{
headers = curl_slist_append(headers, m_DBAuthCertficate); //What is difference between this and line no 118?
CString pCookie = "DBAuthTicket=" + cookie;
curl_easy_setopt(curl,CURLOPT_COOKIE, pCookie);
}
else
{
headers = curl_slist_append(headers, m_OAuthToken);
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, urlparam);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
errbuf[0] = 0;
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 512000);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
{
/* if errors have occured, tell us wath's wrong with 'result'*/
m_response.Format("%s", curl_easy_strerror(res));
return res;
}
m_response = (char*)req.buffer;
m_errDescription = errbuf;
len = req.len;
buflen = req.buflen;
curl_easy_cleanup(curl);
free(req.buffer);
}
return res;
}
/****************************************************************************
Function: CurlWrite_CallbackFunc_String
Description: Read data from the connected URL
Return: String of data and size
****************************************************************************/
size_t CCurlHTTP::CurlWrite_CallbackFunc_String(void *contents, size_t size, size_t nmemb, void *userdata)
{
size_t rLen = size*nmemb;
get_request* req = (get_request*)userdata;
while(req->buflen < req->len + rLen + 1)
{
req->buffer = (unsigned char*)realloc(req->buffer,req->buflen + CHUNK_SIZE);
req->buflen += CHUNK_SIZE;
}
memcpy(&req->buffer[req->len], contents, rLen);
req->len += rLen;
req->buffer[req->len] = 0;
return rLen;
}
The above code works fine for the Success 200 OK Requests. It reads the JSON Response just fine. However, when I get a 500 Internal Server error, it does not read the JSON Error response that comes along with it. How do I read the JSON response in this scenario?
By setting the CURLOPT_FAILONERROR option to TRUE, you are telling curl_easy_perform() to fail immediately with CURLE_HTTP_RETURNED_ERROR on any HTTP response >= 400. It will not call the CURLOPT_WRITEFUNCTION callback, as it will simply close the connection and not even attempt to read the rest of the response.
To get the response data you want, simply remove the CURLOPT_FAILONERROR option. Curl's default behavior is to deliver the response data to you regardless of the HTTP response code. In which case, curl_easy_perform() will return CURLE_OK, and you can then retrieve the response code using curl_easy_getinfo(CURLINFO_RESPONSE_CODE) to check if the HTTP request was successful or not.
On a side note, since the code shown is written in C++, I would strongly advise you NOT to use a dynamic char[] buffer for get_request::buffer. Not only because you are not handling malloc()/realloc() failures at all, but also because manual memory management should be avoided in C++ in general. Use std::string or std::vector<char> instead, in which case you can eliminate get_request in this code altogether, eg:
INT CCurlHTTP::HTTPSPost(const CString& endPointUrl, const CString& urlparam,const CString& cookie){
...
std::string resp;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp);
...
//curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
/* if errors have occured, tell us what's wrong with 'result'*/
m_response.Format("%s", curl_easy_strerror(res));
curl_easy_cleanup(curl);
return res;
}
m_response = resp.c_str();
m_errDescription = errbuf;
len = resp.size();
buflen = resp.capacity();
curl_easy_cleanup(curl);
return res;
}
size_t CCurlHTTP::CurlWrite_CallbackFunc_String(void *contents, size_t size, size_t nmemb, void *userdata)
{
size_t rLen = size * nmemb;
static_cast<std::string*>(userdata)->append(static_cast<char*>(contents), rLen);
return rLen;
}

Libcurl C++ Ldap Request

i am new to cUrl.
What i need todo is a curl request to an ldap server.
So far the request works fine over the command line:
$curl ldap://XXXXXXXXX:389/DC=XXXXXXXXX,DC=DE?mail?sub?(sAMAccountName=XXXXXXXXX) --user s_ad_XXXXXXXXX:Password
After trying to get this done in my C++ CMake Project. I issued this error Code from
libcurl: libcurl: (39) LDAP local: ldap_search_ext Bad search filter
My code as snippet:
std::string readBuffer;
CURL* curl = curl_easy_init();
if (curl) {
CURLcode res;
char errbuf[CURL_ERROR_SIZE];
curl_easy_setopt(curl, CURLOPT_URL, "ldap://XXXXXXXXX:389/DC=XXXXXXXXX,DC=DE?mail?sub?(sAMAccountName=XXXXXXXXX) --user s_ad_XXXXXXXXX:Password");
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
errbuf[0] = 0;
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // this line prints into console, what curl is trying to do
curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1L); // closes the connection, if not here the connection can be used more than once
// curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
std::cout << res << std::endl;
curl_easy_cleanup(curl);
std::cout << readBuffer << std::endl;
if (res != CURLE_OK) {
size_t len = strlen(errbuf);
fprintf(stderr, "\nlibcurl: (%d) ", res);
if (len)
fprintf(stderr, "%s%s", errbuf,
((errbuf[len - 1] != '\n') ? "\n" : ""));
else
fprintf(stderr, "%s\n", curl_easy_strerror(res));
}
}
My Output:
Test curl library
* Trying XXXXXXXXXXXXXX
* TCP_NODELAY set
* Connected to XXXXXXXXXXXXXX
* LDAP local: ldap://XXXXXXXXX:389/DC=XXXXXXXXX,DC=DE?mail?sub?(sAMAccountName=XXXXXXXXX) --user s_ad_XXXXXXXXX:Password
* LDAP local: ldap_search_ext Bad search filter
* Closing connection 0
39
libcurl: (39) LDAP local: ldap_search_ext Bad search filter
I appricate every help! Thank you for taking the time reading this.
You cannot just bung everything into the CURLOPT_OPT_URL field. The --user bit should be separated into a separate parameter:
curl_easy_setopt(curl, CURLOPT_URL, "ldap://XXXXXXXXX:389/DC=XXXXXXXXX,DC=DE?mail?sub?(sAMAccountName=XXXXXXXXX)");
curl_easy_setopt(curl, CURLOPT_USERPWD, "s_ad_XXXXXXXXX:Password");

Authentication on a smtp server fails using libcurl

I try to send an email using the following code :
#include "optimnet_mail.h"
#include <cstring>
struct upload_status {
int lines_read;
};
static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp){
char *data = static_cast<char*>(userp);
if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
return 0;
}
size_t len = strlen(data);
memcpy(ptr, data, len);
return len;
}
std::string mailText(const std::string destination, const std::string content){
const std::string stringResult =
"To: " + destination +
"\r\nFrom: " + FROM +
"\r\nSubject: Solver has finished\r\n" +
"\r\n" + content + "\r\n";
return stringResult;
}
int sendMail(const std::string destination, const std::string content){
const std::string text = mailText(destination, content);
char *data = new char [text.length()+1];
std::strcpy (data,text.c_str());
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *recipients = NULL;
struct upload_status upload_ctx;
upload_ctx.lines_read = 0;
curl = curl_easy_init();
if(curl) {
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, FROM);
curl_easy_setopt(curl, CURLOPT_PASSWORD, PASS);
//using smtps fails instantly
curl_easy_setopt(curl, CURLOPT_URL, "smtps://" HOST ":" PORT);
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
/* If your server doesn't have a valid certificate, then you can disable
* part of the Transport Layer Security protection by setting the
* CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false).
*/
//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
//curl_easy_setopt(curl, CURLOPT_CAINFO, "~/.ssh/known_hosts");
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
// Add two recipients
recipients = curl_slist_append(recipients, destination.c_str());
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
//upload data
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(curl, CURLOPT_READDATA, data);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* Send the message */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Free the list of recipients */
curl_slist_free_all(recipients);
/* Always cleanup */
curl_easy_cleanup(curl);
}
delete data;
return (int)res;
}
int main(){
return sendMail(std::string("myemail#mycompany.com"),std::string("Test2"));
}
Unfortunately, executing this code gives me the following log :
* Trying 83.166.143.44:465...
* TCP_NODELAY set
* Connected to mail.infomaniak.ch (83.166.143.44) port 465 (#0)
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: /etc/ssl/certs
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* Server certificate:
* subject: CN=mail.infomaniak.ch
* start date: Feb 17 22:44:47 2021 GMT
* expire date: May 18 22:44:47 2021 GMT
* subjectAltName: host "mail.infomaniak.ch" matched cert's "mail.infomaniak.ch"
* issuer: C=US; O=Let's Encrypt; CN=R3
* SSL certificate verify ok.
< 220 mail.infomaniak.com ESMTP ready
> EHLO conway
< 250-mail.infomaniak.com
< 250-PIPELINING
< 250-SIZE
< 250-ETRN
< 250-ENHANCEDSTATUSCODES
< 250-8BITMIME
< 250-DSN
< 250 AUTH PLAIN LOGIN
> AUTH PLAIN
< 334
> Some hash
< 535 5.7.0 Invalid login or password
* Closing connection 0
curl_easy_perform() failed: Login denied
On the other hand, when using this curl command :
curl --ssl-reqd --url 'smtps://mail.infomaniak.ch' --user 'user#example.com:password' --mail-from 'user#example.com' --mail-rcpt 'myemail#mycompany.com' --upload-file mail.txt, the email is indeed sent.
Is there something I missed in my code? I don't understand why using the exact same login and password would make the authentication fail.
Edit : thanks to rustyx and Sam Varshavchik, I corrected a problem of port and a problem of memory leak.
The problem was that there is a difference between the host, that looks like <myemail#mycompany.com> and the user, that looks like myemail#mycompany.com. This is what was causing authentication failure.

Libcurl won't send SMTP quit command

#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#define server "smtps://smtp.gmail.com"
#define to "email#gmail.com"
#define from "email#gmail.com"
#define pwd "****"
typedef struct
{
char** mem;
size_t size;
} payload_data;
size_t reader(void *ptr, size_t size, size_t nmemb, void *userp)
{
char* str = NULL;
payload_data *data = (payload_data *)userp;
if((size == 0) || (nmemb == 0) || ((size * nmemb) < 1))
return 0;
str = data->mem[data->size];
if (str)
{
size_t len = strlen(str);
memcpy(ptr, str, len);
++data->size;
return len;
}
return 0;
}
void TestCurl()
{
static const char *info[] =
{
"To: <"to">\r\n"
"From: <"from">\r\n",
"Subject: TestCurl\r\n",
"\r\n",
"Messaging with libcurl..\r\n",
"\r\n",//"\r\n.\r\nQUIT\r\n",
NULL
};
CURLcode res = CURLE_OK;
payload_data data = {0};
CURL* curl = curl_easy_init();
struct curl_slist* recipients = NULL;
if (curl)
{
data.mem = info;
curl_easy_setopt(curl, CURLOPT_URL, server);
curl_easy_setopt(curl, CURLOPT_USERNAME, from);
curl_easy_setopt(curl, CURLOPT_PASSWORD, pwd);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
recipients = curl_slist_append(recipients, to);
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, reader);
curl_easy_setopt(curl, CURLOPT_READDATA, &data);
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); //DOES NOT WORK WITHOUT THIS!
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
curl_slist_free_all(recipients);
curl_easy_cleanup(curl);
}
}
and the result is:
* Rebuilt URL to: smtps://smtp.gmail.com/
* timeout on name lookup is not supported
* Hostname was NOT found in DNS cache
* Trying 74.125.69.108...
* Connected to smtp.gmail.com (74.125.69.108) port 465 (#0)
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* Server certificate:
* subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=smtp.gm
ail.com
* start date: 2014-07-15 08:40:38 GMT
* expire date: 2015-04-04 15:15:55 GMT
* subjectAltName: smtp.gmail.com matched
* issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
* SSL certificate verify result: unable to get local issuer certificate (
20), continuing anyway.
< 220 mx.google.com ESMTP 141sm5490110ioz.39 - gsmtp
> EHLO Kira
< 250-mx.google.com at your service, [my_ip_address]
< 250-SIZE 35882577
< 250-8BITMIME
< 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER
< 250-ENHANCEDSTATUSCODES
< 250-PIPELINING
< 250-CHUNKING
< 250 SMTPUTF8
> AUTH LOGIN
< 334 VXNlcm5hbWU6
> encrypted_username
< 334 UGFzc3dvcmQ6
> encrypted_password
< 235 2.7.0 Accepted
> MAIL FROM:<email#gmail.com>
< 250 2.1.0 OK 141sm5490110ioz.39 - gsmtp
> RCPT TO:<email#gmail.com>
< 250 2.1.5 OK 141sm5490110ioz.39 - gsmtp
> DATA
< 354 Go ahead 141sm5490110ioz.39 - gsmtp
< 250 2.0.0 OK 1413861121 141sm5490110ioz.39 - gsmtp
* Connection #0 to host smtp.gmail.com left intact
Process returned 0 (0x0) execution time : 1.524 s
Press any key to continue.
I can't get it to send QUIT. I tried adding QUIT to the "info" array but it just adds it as part of the message. I tried terminating with \r\n\r\n.\r\n but no cigar..
The email gets sent successfully.. I just can't get it to quit. Every time I send a new email, it increases the "Connection #X to host smtp.gmail.com left intact".. Even though I did the curl cleanup..
Any ideas how I can get it to quit and stop leaving the host intact? Is it leaking somewhere? Why does it do this?
First, it actually closes the connection when you close the easy handle - since that's where the connection cache is kept when using the easy interface. Depending on your libcurl version it may not send a QUIT and you may not see it in the verbose output (since the closing is done after the handle in which you set CURLOPT_VERBOSE to TRUE in is already gone).
The libcurl way to force a closure of a connection after the specific transfer is done, is to set CURLOPT_FORBID_REUSE. In a similar spirit you can set CURLOPT_FRESH_CONNECT to force using a new connection instead of re-using an old.

How to null the output of smtp

I am using the following code for smtp:
When it invokes sendEmail, the output will display in the terminal. How can I send it to dev/null or not output it? Running in background doesn't help.
Partial Output:
* About to connect() to smtp.gmail.com port 587 (#0)
* Trying 173.194.79.109... * connected
< 220 mx.google.com ESMTP nv6sm10641402pbc.42
> EHLO ubuntu
< 250-mx.google.com at your service, [222.164.82.97]
< 250-SIZE 35882577
< 250-8BITMIME
< 250-STARTTLS
< 250 ENHANCEDSTATUSCODES
> STARTTLS
< 220 2.0.0 Ready to start TLS
* successfully set certificate verify locations:
* CAfile: none
Code:
#include <stdio.h>
#include <string>
#include <cstring>
#include <curl/curl.h>
using namespace std;
/* This is a simple example showing how to send mail using libcurl's SMTP
* capabilities. It builds on the simplesmtp.c example, adding some
* authentication and transport security.
*/
#define FROM "<asktheprogramer2#gmail.com>"
#define CC "<asktheprogramer2#gmail.com>"
string ToAddress,strSubject, strMessage;
struct upload_status {
int lines_read;
};
static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
{
const char *payload_text[]={ToAddress.c_str(),"From: " FROM "\n","Cc: " CC "\n",strSubject.c_str(),"\n", /* empty line to divide headers from body, see RFC5322 */
strMessage.c_str(),NULL};
struct upload_status *upload_ctx = (struct upload_status *)userp;
const char *data;
if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
return 0;
}
data = payload_text[upload_ctx->lines_read];
if (data) {
size_t len = strlen(data);
memcpy(ptr, data, len);
upload_ctx->lines_read ++;
return len;
}
return 0;
}
void sendMail(string strRecipent, string strAubject, string strBessage){
ToAddress="To: ";
ToAddress+=strRecipent;
ToAddress+="\n";
strSubject="Subject: ";
strSubject+=strAubject;
strSubject+="\n";
strMessage=strBessage;
CURL *curl;
CURLcode res;
struct curl_slist *recipients = NULL;
struct upload_status upload_ctx;
upload_ctx.lines_read = 0;
curl = curl_easy_init();
if (curl) {
/* This is the URL for your mailserver. Note the use of port 587 here,
* instead of the normal SMTP port (25). Port 587 is commonly used for
* secure mail submission (see RFC4403), but you should use whatever
* matches your server configuration. */
curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.gmail.com:500");
/* In this example, we'll start with a plain text connection, and upgrade
* to Transport Layer Security (TLS) using the STARTTLS command. Be careful
* of using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer
* will continue anyway - see the security discussion in the libcurl
* tutorial for more details. */
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
/* If your server doesn't have a valid certificate, then you can disable
* part of the Transport Layer Security protection by setting the
* CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false).
* curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
* curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
* That is, in general, a bad idea. It is still better than sending your
* authentication details in plain text though.
* Instead, you should get the issuer certificate (or the host certificate
* if the certificate is self-signed) and add it to the set of certificates
* that are known to libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH. See
* docs/SSLCERTS for more information.
*/
//curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
/* A common reason for requiring transport security is to protect
* authentication details (user names and passwords) from being "snooped"
* on the network. Here is how the user name and password are provided: */
curl_easy_setopt(curl, CURLOPT_USERNAME, "sim.dssprojects#gmail.com");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "SIMpassword");
/* value for envelope reverse-path */
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
/* Add two recipients, in this particular case they correspond to the
* To: and Cc: addressees in the header, but they could be any kind of
* recipient. */
recipients = curl_slist_append(recipients, strRecipent.c_str());
recipients = curl_slist_append(recipients, CC);
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
/* In this case, we're using a callback function to specify the data. You
* could just use the CURLOPT_READDATA option to specify a FILE pointer to
* read from.
*/
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
/* Since the traffic will be encrypted, it is very useful to turn on debug
* information within libcurl to see what is happening during the transfer.
*/
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* send the message (including headers) */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* free the list of recipients and clean up */
curl_slist_free_all(recipients);
curl_easy_cleanup(curl);
}
}
int main(void)
{
int pid = fork();
if (pid == 0)
{
for(int i=0;i<2;i++)
{
sleep(10);
sendMail("<asktheprogramer#gmail.com>","ABE Reminder","You have an appt in 15 mins");
}
return 0;
}
else
{
sleep(30);
}
return 0;
}
A possibility would be to redirect the standard output and standard error. I am unsure if there is an equivalent mechanism for achieving this via C++ streams but you could use freopen():
freopen("stdout.txt", "w", stdout);
freopen("stderr.txt", "w", stderr);
You can redirect the output of your program when running it. Like this (assuming *nix environment):
./<yourprogram> > /dev/null
for the standard output.
./<yourprogram> &> /dev/null
for standard and error outputs.
Delete or comment out this line from the code:
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);