> Now can you please break the program with debugger and look:
> Where does it "hang"?
> How does it "hang"?
> Why does it "hang"?
> What's it doing at the time of "hanging"?
>
This is the source code of the toy program. It is executed correctly
but for the login you obviously have to type in your username password
and smtp server address.
when I insert this code in my program,
This code being called by pressing a button on the GUI transmission goes
forever.
the line that fails is this:
res = curl_easy_perform(curl);
It starts the transmission of the data and should return.
This is not what happens. The funclion locks the wireless transmission
and after closing the software with xkill, data transmission still
continues, but no mail starts.
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2016, Daniel Stenberg, <
dan...@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at
https://curl.haxx.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
OF ANY
* KIND, either express or implied.
*
***************************************************************************/
/* <DESC>
* SMTP example showing how to send e-mails
* </DESC>
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <cstdlib>
#include <curl/curl.h>
/* This is a simple example showing how to send mail using libcurl's SMTP
* capabilities. For an example of using the multi interface please see
* smtp-multi.c.
*
* Note that this example requires libcurl 7.20.0 or above.
*/
#define FROM "<
ale...@inwind.it>"
#define TO "<
alessandr...@libero.it>"
//#define CC "<
ale...@inwind.it>"
char *current_date();
char *date = current_date();
static const char *payload_text[] = {
"To: " TO "\r\n",
"From: " /*FROM*/ "Stilottica\r\n",
//"Cc: " CC "Another example User\r\n",
"Subject: ordine lenti\r\n", // subject of tha message
"\r\n", /* empty line to divide headers from body, see RFC5322 */
"Ordine del ", //The body of the message starts here.
// more strings go here
date,
"\r\nA long text email can be very tedious to read."
"\r\n-- end of the message --",
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;
}
int main(void)
{
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();
/* replace xxx with your credentials */
if(curl) {
/* This is the URL for your mailserver */
curl_easy_setopt(curl, CURLOPT_USERNAME, "xxxx...@xxxxxx.xx"); //
curl_easy_setopt(curl, CURLOPT_PASSWORD, "xxxxxxxx");
curl_easy_setopt(curl, CURLOPT_URL, "xxxxxxxx");
/* Note that this option isn't strictly required, omitting it will
result
* in libcurl sending the MAIL FROM command with empty sender data. All
* autoresponses should have an empty reverse-path, and should be
directed
* to the address in the reverse-path which triggered them. Otherwise,
* they could cause an endless loop. See RFC 5321 Section 4.5.5 for
more
* details.
*/
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, TO);
//recipients = curl_slist_append(recipients, CC);
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
/* We're using a callback function to specify the payload (the
headers and
* body of the message). 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);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 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));
}
else
{
printf("message sent!");
}
/* Free the list of recipients */
curl_slist_free_all(recipients);
/* curl won't send the QUIT command until you call cleanup, so you
should
* be able to re-use this connection for additional messages (setting
* CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and calling
* curl_easy_perform() again. It may not be a good idea to keep the
* connection open for a very long time though (more than a few minutes
* may result in the server timing out the connection), and you do
want to
* clean up in the end.
*/
curl_easy_cleanup(curl);
}
return (int)res;
}
char *current_date()
{
time_t rawtime;
struct tm *timeinfo = NULL;
char *buf = new char[35];
if(buf == NULL)
{
printf("string allocation error\n");
exit(1);
}
time(&rawtime);
if ((timeinfo = localtime(&rawtime)) == NULL)
{
printf("data generation error");
exit(1);
}
sprintf(buf,"%.2d/%.2d/%4d ore %.2d:%.2d",
timeinfo->tm_mday,
1 + timeinfo->tm_mon,
1900 + timeinfo->tm_year,
timeinfo->tm_hour,
timeinfo->tm_min
);
return buf;
}