Libcurl won't send SMTP quit command - c++

#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.

Related

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.

Rate Beer API : Receiving 500 Code giving "POST body missing" when the data has actually been sent off

I am using libcurl with C++ in order send a POST request off to query some information off of RateBeer on a particular beer. It says the data has been sent off but I am getting a "POST fields empty" error (which is from the graphQL side I assume).
The following is a dump of the current result (I have a print of the raw post data as well)
* Trying 13.249.142.63...
* TCP_NODELAY set
* Connected to api.r8.beer (13.249.142.63) port 443 (#0)
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: none
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: CN=*.r8.beer
* start date: Aug 14 00:00:00 2018 GMT
* expire date: Sep 14 12:00:00 2019 GMT
* subjectAltName: host "api.r8.beer" matched cert's "*.r8.beer"
* issuer: C=US; O=Amazon; OU=Server CA 1B; CN=Amazon
* SSL certificate verify ok.
> POST /v1/api/graphql/ HTTP/1.1
Host: api.r8.beer
User-Agent: libcurl-agent/1.0
Content-Type:application/json
Accept:application/json
x-api-[key:Removed for privacy]
Transfer-Encoding: chunked
cb
* upload completely sent off: 210 out of 203 bytes
< HTTP/1.1 500 Internal Server Error
< Content-Type: application/json
< Content-Length: 61
< Connection: keep-alive
< Date: Thu, 31 Jan 2019 16:07:38 GMT
< x-amzn-RequestId: 547d7bc7-2572-11e9-b411-af48978c268e
< CF-RAY: 4a1d6eb288bda66b-DUB
< Access-Control-Allow-Origin: *
< Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
< Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, x-api-key, x-api-auth
< x-amzn-Remapped-Connection: keep-alive
< X-Request-Id: 54b37fa0-2572-11e9-ac7f-9d4fc71779d1
< Set-Cookie: __cfduid=d84de3db6937ad5bef73abca5ae68e43a1548950858; expires=Fri, 31-Jan-20 16:07:38 GMT; path=/; domain=.r8.beer; HttpOnly; Secure
< x-amz-apigw-id: UYGDpFkeDoEFR2w=
< x-amzn-Remapped-Server: cloudflare
< X-Powered-By: Express
< x-amzn-Remapped-Date: Thu, 31 Jan 2019 16:07:38 GMT
< X-Cache: Error from cloudfront
< Via: 1.1 feeead777aa6b11f4775062f1953fdc4.cloudfront.net (CloudFront)
< X-Amz-Cf-Id: kg0xraCi9C9zdmNff3iEZXOBpTMKPwNSQa6nzV-tyXqCdVCFFOP2Jg==
<
* Connection #0 to host api.r8.beer left intact
{"query":"query{\nTruth:beerSearch(query: \"Truth\", first : 5) {\n...beerfields\n}\n}\nfragment beerfields on BeerList{\nitems{\nname\nbrewer{\nname\n}\ndescription\nratingCount\n}\n}","variables":"{}"}
Response: POST body missing. Did you forget use body-parser middleware Response End
Some debugging I have tried in this conquest:
Changing the code to use a readback callback instead of using the POST_FIELDS default, same result
Messing with the capitalization of headers
Changed the transfer method to using chunked
Trying to POST to the sandbox API and the production API
Here is the query function:
NOTE: m_api is just a member with the api key and the api base URL
void cURLWrapperRateBeer::QueryBeerList(const CBeerList& inBeerList)
{
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl)
{
// Construct the header struct
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Content-Type:application/json");
headers = curl_slist_append(headers, "Accept:application/json");
std::string keyPhrase = std::string("x-api-key:").append(m_api.second);
headers = curl_slist_append(headers, keyPhrase.c_str());
headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
// Set the URL and the data we want
curl_easy_setopt(curl, CURLOPT_URL, m_api.first.c_str());
// Signify our intention to post data
curl_easy_setopt(curl, CURLOPT_POST, 1L);
// Verbose Output
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
// Give it a user agent
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
std::string data = CreateRequestURL(inBeerList);
data.append("\0");
// Set up the data container for our push request
const char* c_data = data.c_str();
std::cout << c_data;
struct WriteStatus data_container;
data_container.p = c_data;
data_container.remaining = (long)strlen(c_data);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data_container.p);
/*
Define the buffer for writing memory and what the callback function will
use for its buffer
*/
//curl_easy_setopt(curl, CURLOPT_READFUNCTION, &cURLWrapperRateBeer::ReadData);
//curl_easy_setopt(curl, CURLOPT_READDATA, &data_container);
// Size of the data
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)data_container.remaining);
// Handler for reading response
std::string response;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &cURLWrapperRateBeer::WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
// Set our custom set of headers
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
if(res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
//std::cout << "DUMP : " << data_container.p;
}
#ifdef SKIP_PEER_VERIFICATION
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
#ifdef SKIP_HOSTNAME_VERIFICATION
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
std::cout << "Response: " << response;
std:: cout << "Response End";
/* Check for errors */
if(res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
}
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
}

CURL_TIMECOND_NONE doesn't work: How to remove if-modified-since header

I use libcurl to do RTSP request. I set curl options as seen below:
FILE *tmpListDownloadFile;
tmpListDownloadFile = fopen(tmp.c_str(), "w");
if(tmpListDownloadFile != NULL)
{
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
char *p = curl_easy_unescape(curl, UriP, 0, NULL);
string s = p;
curl_free(p);
string uri = url + "/?" + s;
printf("%s uri:%s\n",__FUNCTION__,uri.c_str());
curl_version_info_data *d = curl_version_info(CURLVERSION_NOW);
printf("curl version:%s\n",d->version);
curl_easy_setopt(curl, CURLOPT_RTSP_STREAM_URI, uri.c_str());
curl_easy_setopt(curl, CURLOPT_RTSP_CLIENT_CSEQ, stRtspInfo.CSeq);
curl_easy_setopt(curl, CURLOPT_RTSP_TRANSPORT, transport.c_str());
curl_easy_setopt(curl, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_SETUP);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, (void *)tmpListDownloadFile);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15);
curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
CURLcode curlResult = curl_easy_perform(curl);
char* session_id;
if(curl_easy_getinfo(curl, CURLINFO_RTSP_SESSION_ID, &session_id) == CURLE_OK)
{
printf("%s session_id:%s\n",__FUNCTION__, session_id);
if(NULL != session_id)
{
ostringstream ss;
ss << session_id;
stRtspInfo.sessionID = ss.str();
bIsSessionEstablished = true;
}
}
else
{
printf("%s getting session id failed\n",__FUNCTION__);
}
curl_easy_cleanup(curl);
curl_global_cleanup();
fclose(tmpListDownloadFile);
if (curlResult == CURLE_OK && bIsSessionEstablished == true)
{
...
}
else
{
printf("Setup failed\n");
}
}
And log messages are as below.
RtspSetup url:rtsp://10.134.158.71
RtspSetup transport:RTP/AVP;unicast;client_port=45636-45637
RtspSetup uri:rtsp://10.134.158.71/?src=1&freq=11054&sr=30000&pol=v&msys=dvbs2&mtype=8psk&fec=34&ro=0.35&plts=off&pids=0
curl version:7.21.3
* About to connect() to 10.134.158.71 port 554 (#0)
* Trying 10.134.158.71... * connected
* Connected to 10.134.158.71 (10.134.158.71) port 554 (#0)
> SETUP rtsp://10.134.158.71/?src=1&freq=11054&sr=30000&pol=v&msys=dvbs2&mtype=8psk&fec=34&ro=0.35&plts=off&pids=0 RTSP/1.0
CSeq: 1
Transport: RTP/AVP;unicast;client_port=45636-45637
If-Modified-Since: Thu, 01 Jan 1970 00:00:00 GMT
* HTTP 1.1 or later with persistent connection, pipelining supported
< RTSP/1.0 200 OK
< CSeq: 1
< Date: Sat, Jan 01 2000 00:14:11 GMT
< Transport: RTP/AVP;unicast;destination=10.134.158.14;source=10.134.158.71;client_port=45636-45637;server_port=6970-6971
< Session: E3C33231;timeout=60
< com.ses.streamID: 3
<
* Connection #0 to host 10.134.158.71 left intact
RtspSetup session_id:E3C33231
Altough I set Timecondition as none, if-modified-since are added to the header. I want to remove if-modified-since header. How to do this?
That's a bug in libcurl (that happens for some RTSP requests). Just now fixed in this commit. To be included in the upcoming next release: 7.46.0.

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

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.

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);