Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #40901 > unrolled thread

send email and socket programming

Started byalexo <alelvb@inwind.it>
First post2016-02-14 18:01 +0100
Last post2016-02-29 14:16 +0100
Articles 17 on this page of 57 — 14 participants

Back to article view | Back to comp.lang.c++


Contents

  send email and socket programming alexo <alelvb@inwind.it> - 2016-02-14 18:01 +0100
    Re: send email and socket programming JiiPee <no@notvalid.com> - 2016-02-14 17:07 +0000
      Re: send email and socket programming JiiPee <no@notvalid.com> - 2016-02-14 17:12 +0000
      Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-14 19:44 +0100
    Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-14 19:18 +0200
    Re: send email and socket programming Öö Tiib <ootiib@hot.ee> - 2016-02-14 09:39 -0800
      Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-14 20:23 +0100
    Re: send email and socket programming woodbrian77@gmail.com - 2016-02-14 10:47 -0800
    Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-14 21:19 +0200
      Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-14 20:29 +0100
        Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-14 22:29 +0200
          Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-14 22:45 +0100
            Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-15 00:15 +0200
              Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-15 00:39 +0200
                Re: send email and socket programming Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2016-02-14 23:01 +0000
                  Re: send email and socket programming Geoff <geoff@invalid.invalid> - 2016-02-14 15:22 -0800
                    Re: send email and socket programming Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-14 18:41 -0500
                    Re: send email and socket programming Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2016-02-14 23:43 +0000
                      Re: send email and socket programming Geoff <geoff@invalid.invalid> - 2016-02-14 17:33 -0800
                        Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-15 09:37 +0200
                        Re: send email and socket programming Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2016-02-15 10:04 +0000
                          Re: send email and socket programming Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2016-02-15 10:14 +0000
                            Re: send email and socket programming Geoff <geoff@invalid.invalid> - 2016-02-15 10:11 -0800
            Re: send email and socket programming Geoff <geoff@invalid.invalid> - 2016-02-14 15:16 -0800
    Re: send email and socket programming Ian Collins <ian-news@hotmail.com> - 2016-02-15 08:40 +1300
      Re: send email and socket programming "Chris M. Thomasson" <nospam@nospam.nospam> - 2016-02-14 12:09 -0800
    Re: send email and socket programming Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-14 16:04 -0500
    Re: send email and socket programming jt@toerring.de (Jens Thoms Toerring) - 2016-02-14 21:20 +0000
      Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-14 22:40 +0100
        Re: send email and socket programming Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2016-02-14 22:20 +0000
          Re: send email and socket programming Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-14 18:01 -0500
            Re: send email and socket programming Ian Collins <ian-news@hotmail.com> - 2016-02-15 12:09 +1300
              Re: send email and socket programming Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-14 18:37 -0500
                Re: send email and socket programming Ian Collins <ian-news@hotmail.com> - 2016-02-15 13:06 +1300
                  Re: send email and socket programming Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-14 21:40 -0500
            Re: send email and socket programming Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2016-02-14 23:35 +0000
              Re: send email and socket programming Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-14 21:43 -0500
        Re: send email and socket programming Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-14 18:05 -0500
    Re: send email and socket programming Christopher Pisz <nospam@notanaddress.com> - 2016-02-16 16:39 -0600
    Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-26 04:09 +0100
      Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-26 07:49 +0200
        Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-26 08:39 +0200
          Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-26 13:15 +0100
            Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-26 14:40 +0200
              Re: send email and socket programming Christopher Pisz <nospam@notanaddress.com> - 2016-02-26 16:41 -0600
                Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-27 13:38 +0100
                  Re: send email and socket programming Öö Tiib <ootiib@hot.ee> - 2016-02-27 05:15 -0800
                    Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-27 18:59 +0100
                      Re: send email and socket programming Öö Tiib <ootiib@hot.ee> - 2016-02-27 10:41 -0800
                      Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-27 21:46 +0200
                Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-27 13:42 +0100
                  Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-27 19:14 +0100
                    Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-27 19:22 +0100
                      Re: send email and socket programming guinness.tony@gmail.com - 2016-02-28 14:19 -0800
        Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-27 18:44 +0100
      Re: send email and socket programming Jorgen Grahn <grahn+nntp@snipabacken.se> - 2016-02-27 17:59 +0000
    Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-29 14:16 +0100

Page 3 of 3 — ← Prev page 1 2 [3]


#41247

FromPaavo Helde <myfirstname@osa.pri.ee>
Date2016-02-26 07:49 +0200
Message-ID<ZIKdnbrfja7DelLLnZ2dnUU78LudnZ2d@giganews.com>
In reply to#41246
On 26.02.2016 5:09, alexo wrote:
>
>> I've written for UNIX a little program [FLTK] that needs to send an
>> order through e-mail.
>>
>
> As JiiPee suggested, I've found a very powerful library named curl.
> The one he suggested, mimetic, has a bug.

Namely?

> I'm an absolute beginner on the field but adapting the example code, I
> was able to send with curl my first email from a C/C++ program.
> When I insert the code in my program the data transmission seems not
> going to an end. I use it inside a wrapper function that is called-back
> pressing a FLTK button.
>
> The specific failing function is curl_easy_perform() that correctly
> works on the toy test program.

Most likely there is a bug in your program.

[toc] | [prev] | [next] | [standalone]


#41248

FromPaavo Helde <myfirstname@osa.pri.ee>
Date2016-02-26 08:39 +0200
Message-ID<VJKdneIDx4C6blLLnZ2dnUU78X2dnZ2d@giganews.com>
In reply to#41247
On 26.02.2016 7:49, Paavo Helde wrote:
> On 26.02.2016 5:09, alexo wrote:

>> I'm an absolute beginner on the field but adapting the example code, I
>> was able to send with curl my first email from a C/C++ program.
>> When I insert the code in my program the data transmission seems not
>> going to an end. I use it inside a wrapper function that is called-back
>> pressing a FLTK button.
>>
>> The specific failing function is curl_easy_perform() that correctly
>> works on the toy test program.
>
> Most likely there is a bug in your program.

Or alternatively, some firewall is blocking your communication attempts.

[toc] | [prev] | [next] | [standalone]


#41251

Fromalexo <alelvb@inwind.it>
Date2016-02-26 13:15 +0100
Message-ID<napfkh$15em$1@gioia.aioe.org>
In reply to#41248
>> Most likely there is a bug in your program.
>
> Or alternatively, some firewall is blocking your communication attempts.

The firewall seems out of discussion because the toy
code sends the emails, while the same code cut and pasted in my program 
does not.
When I press the "send" button of my program the button remains in the 
pushed status and wireless connection starts an endless transmission of 
data.

[toc] | [prev] | [next] | [standalone]


#41252

FromPaavo Helde <myfirstname@osa.pri.ee>
Date2016-02-26 14:40 +0200
Message-ID<eM6dnbIKR64k2k3LnZ2dnUU78RmdnZ2d@giganews.com>
In reply to#41251
On 26.02.2016 14:15, alexo wrote:
>>> Most likely there is a bug in your program.
>>
>> Or alternatively, some firewall is blocking your communication attempts.
>
> The firewall seems out of discussion because the toy
> code sends the emails, while the same code cut and pasted in my program
> does not.

Well then it is simple, there is a bug in your non-toy program. My 
crystal ball tells me the bug is on line 247.

[toc] | [prev] | [next] | [standalone]


#41264

FromChristopher Pisz <nospam@notanaddress.com>
Date2016-02-26 16:41 -0600
Message-ID<naqk5o$rt3$1@dont-email.me>
In reply to#41252
On 2/26/2016 6:40 AM, Paavo Helde wrote:
> On 26.02.2016 14:15, alexo wrote:
>>>> Most likely there is a bug in your program.
>>>
>>> Or alternatively, some firewall is blocking your communication attempts.
>>
>> The firewall seems out of discussion because the toy
>> code sends the emails, while the same code cut and pasted in my program
>> does not.
>
> Well then it is simple, there is a bug in your non-toy program. My
> crystal ball tells me the bug is on line 247.
>

People are trying to tell you to use your debugger and find the source 
of the problem, try to deduce the cause, and then reproduce a simple 
minimal example that demonstrates it. Only then can you expect useful 
suggestions from others.

Have you made attempts at debugging?
Where does it fail?
How does it fail?
What's it doing at the time?


-- 
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---

[toc] | [prev] | [next] | [standalone]


#41272

Fromalexo <alelvb@inwind.it>
Date2016-02-27 13:38 +0100
Message-ID<nas5d2$15ah$1@gioia.aioe.org>
In reply to#41264
> People are trying to tell you to use your debugger and find the source
> of the problem, try to deduce the cause, and then reproduce a simple
> minimal example that demonstrates it. Only then can you expect useful
> suggestions from others.



> Have you made attempts at debugging?
> Where does it fail?
> How does it fail?
> What's it doing at the time?


The debugger doesn' stops on error. There is not a segfault or similar. 
My program simply continues to send data and "hangs".
If I close it with xkill transmission doesn't end. It continues to keep 
data transfer to I don't know who.


[toc] | [prev] | [next] | [standalone]


#41274

FromÖö Tiib <ootiib@hot.ee>
Date2016-02-27 05:15 -0800
Message-ID<fd392257-14cb-4b73-b15e-2772b77ec4cf@googlegroups.com>
In reply to#41272
On Saturday, 27 February 2016 14:39:24 UTC+2, alexo  wrote:

Restoring attributions, do not erase those. :(

> Christopher Pisz  wrote:

> > People are trying to tell you to use your debugger and find the source
> > of the problem, try to deduce the cause, and then reproduce a simple
> > minimal example that demonstrates it. Only then can you expect useful
> > suggestions from others.
> 
> 
> 
> > Have you made attempts at debugging?
> > Where does it fail?
> > How does it fail?
> > What's it doing at the time?
> 
> 
> The debugger doesn' stops on error. There is not a segfault or similar. 
> My program simply continues to send data and "hangs".
> If I close it with xkill transmission doesn't end. It continues to keep 
> data transfer to I don't know who.

You apparently did not understand what Christopher asked. Possibly you 
don't know what debugger is. Debugger is a tool. It does not "debug" on
its own. It helps you to find bugs. 

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"?

[toc] | [prev] | [next] | [standalone]


#41282

Fromalexo <alelvb@inwind.it>
Date2016-02-27 18:59 +0100
Message-ID<naso5m$594$1@gioia.aioe.org>
In reply to#41274
> 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, <daniel@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    "<alelvb@inwind.it>"
#define TO      "<alessandro.volturno@libero.it>"
//#define CC    "<alelvb@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, "xxxxxxxxx@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;
}

[toc] | [prev] | [next] | [standalone]


#41288

FromÖö Tiib <ootiib@hot.ee>
Date2016-02-27 10:41 -0800
Message-ID<00e2f34f-5f6c-4145-84b4-39d19e3dc903@googlegroups.com>
In reply to#41282
On Saturday, 27 February 2016 19:59:33 UTC+2, alexo  wrote:
> > 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);

So your program hangs inside of 'curl_easy_perform' of libcurl.

    Where does it "hang"?
    How does it "hang"?
    Why does it "hang"?
    What's it doing at the time of "hanging"?

[toc] | [prev] | [next] | [standalone]


#41292

FromPaavo Helde <myfirstname@osa.pri.ee>
Date2016-02-27 21:46 +0200
Message-ID<RaKdndlH2siqYEzLnZ2dnUU78SWdnZ2d@giganews.com>
In reply to#41282
On 27.02.2016 19:59, alexo wrote:
>> 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.

Obviously your two programs differ in some sense, as one is working as 
expected and the other does not. Obviously you have made changes when 
pasting the code to your real program (otherwise you would have two 
main() functions which would not compile).

You have to figure out what is the crucial difference. And no, posting 
the code for the *working* program is not very helpful. Instead, you 
should extract the minimal *non-working* program from your real app.

According to your claims the program is sending infinite amount of data. 
However, the data is provided by your own callback function 
(payload_source()), so you could easily put a breakpoint there and find 
out how much and what data it is sending.


[toc] | [prev] | [next] | [standalone]


#41273

Fromalexo <alelvb@inwind.it>
Date2016-02-27 13:42 +0100
Message-ID<nas5js$15jg$1@gioia.aioe.org>
In reply to#41264
> People are trying to tell you to use your debugger and find the source
> of the problem, try to deduce the cause, and then reproduce a simple
> minimal example that demonstrates it. Only then can you expect useful
> suggestions from others.



> Have you made attempts at debugging?
> Where does it fail?
> How does it fail?
> What's it doing at the time?


The debugger doesn't stops on error. There is not a segfault or similar. 
My program simply continues to send data and "hangs".
If I close it with xkill data transmission doesn't end.



[toc] | [prev] | [next] | [standalone]


#41284

Fromalexo <alelvb@inwind.it>
Date2016-02-27 19:14 +0100
Message-ID<nasp20$6qh$1@gioia.aioe.org>
In reply to#41273
Il 27/02/2016 13:42, alexo ha scritto:

>> Have you made attempts at debugging?

yes

>> Where does it fail?

on the line that starts data transmission namely,

res = curl_easy_perform(curl);

>> How does it fail?

transmission starts and never ends. Even force closing of the program 
wireless activity doesn't stop and always stays on, locking internet 
communication.





code of the toy program follows:
in order to make it working you must fill in your username, password and 
smtp server address for the outgoing mails.


here in the code:

curl_easy_setopt(curl, CURLOPT_USERNAME, " ");
curl_easy_setopt(curl, CURLOPT_PASSWORD, " ");
curl_easy_setopt(curl, CURLOPT_URL, " ");



/***************************************************************************
  *                                  _   _ ____  _
  *  Project                     ___| | | |  _ \| |
  *                             / __| | | | |_) | |
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@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    "<alelvb@inwind.it>"
#define TO      "<alessandro.volturno@libero.it>"
//#define CC    "<alelvb@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();

     if(curl) {
     /* This is the URL for your mailserver */
     curl_easy_setopt(curl, CURLOPT_USERNAME, " ");
     curl_easy_setopt(curl, CURLOPT_PASSWORD, " ");
     curl_easy_setopt(curl, CURLOPT_URL, " ");


     /* 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;
}

[toc] | [prev] | [next] | [standalone]


#41286

Fromalexo <alelvb@inwind.it>
Date2016-02-27 19:22 +0100
Message-ID<nasph9$7e4$1@gioia.aioe.org>
In reply to#41284
Il 27/02/2016 19:14, alexo ha scritto:
> Il 27/02/2016 13:42, alexo ha scritto:
>
>>> Have you made attempts at debugging?
>
> yes
>
>>> Where does it fail?
>
> on the line that starts data transmission namely,
>
> res = curl_easy_perform(curl);
>
>>> How does it fail?
>
> transmission starts and never ends. Even force closing of the program
> wireless activity doesn't stop and always stays on, locking internet
> communication.
>
>
>
>
>
> code of the toy program follows:
> in order to make it working you must fill in your username, password and
> smtp server address for the outgoing mails.
>
>
> here in the code:
>
> curl_easy_setopt(curl, CURLOPT_USERNAME, " ");
> curl_easy_setopt(curl, CURLOPT_PASSWORD, " ");
> curl_easy_setopt(curl, CURLOPT_URL, " ");

I forgot to specify that it is not the code here reported that fails, 
but the same code when I insert it in a callback function to be called 
on button pressing.

any help is  welcome and appreciated

thank you

[toc] | [prev] | [next] | [standalone]


#41329

Fromguinness.tony@gmail.com
Date2016-02-28 14:19 -0800
Message-ID<3c761fb0-e0e4-45bc-a18d-8699191dc1cb@googlegroups.com>
In reply to#41286
On Saturday, 27 February 2016 18:22:44 UTC, alexo  wrote:
> Il 27/02/2016 19:14, alexo ha scritto:
> > Il 27/02/2016 13:42, alexo ha scritto:
> >
> >>> Have you made attempts at debugging?
> >
> > yes
> >
> >>> Where does it fail?
> >
> > on the line that starts data transmission namely,
> >
> > res = curl_easy_perform(curl);
> >
> >>> How does it fail?
> >
> > transmission starts and never ends. Even force closing of the program
> > wireless activity doesn't stop and always stays on, locking internet
> > communication.
> >
> >
> >
> >
> >
> > code of the toy program follows:
> > in order to make it working you must fill in your username, password and
> > smtp server address for the outgoing mails.
> >
> >
> > here in the code:
> >
> > curl_easy_setopt(curl, CURLOPT_USERNAME, " ");
> > curl_easy_setopt(curl, CURLOPT_PASSWORD, " ");
> > curl_easy_setopt(curl, CURLOPT_URL, " ");
> 
> I forgot to specify that it is not the code here reported that fails, 
> but the same code when I insert it in a callback function to be called 
> on button pressing.
> 
> any help is  welcome and appreciated
> 
> thank you

It seems that your program runs in an event-driven framework
(which the toy program did not).

In such an environment, you almost certainly need to be using
the curl_multi_xxxx() function set: this allows the transfer to
be made asynchronously, eventually notifying you of the final
success/failure status (through an event or callback function)
without holding up your original event handler.

In the last project in which we used libcurl, I seem to recall
comments in the code to the effect that curl_easy_perform()
must only be called from the same thread that called
curl_easy_init(), otherwise unexpected behaviour would ensue.
[I've not recently checked the libcurl documentation to
determine the validity of those warnings.]
In an event-driven runtime framework, this same-thread
guarantee cannot always, if ever, be met.

I advise looking into using libcurl-multi.

https://curl.haxx.se/libcurl/c/libcurl-multi.html

[toc] | [prev] | [next] | [standalone]


#41280

Fromalexo <alelvb@inwind.it>
Date2016-02-27 18:44 +0100
Message-ID<nasn8v$3ql$1@gioia.aioe.org>
In reply to#41247
>> As JiiPee suggested, I've found a very powerful library named curl.
>> The one he suggested, mimetic, has a bug.
>
> Namely?

after ./configure
make fails due to errors in the source files

[toc] | [prev] | [next] | [standalone]


#41283

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2016-02-27 17:59 +0000
Message-ID<slrnnd3k7j.5q5.grahn+nntp@frailea.sa.invalid>
In reply to#41246
On Fri, 2016-02-26, alexo wrote:
>
>> I've written for UNIX a little program [FLTK] that needs to send an
>> order through e-mail.
>>
>
> As JiiPee suggested, I've found a very powerful library named curl.
> The one he suggested, mimetic, has a bug.
> I'm an absolute beginner on the field but adapting the example code, I 
> was able to send with curl my first email from a C/C++ program.

Note that the traditional, safe way of sending an email on Unix is to
pipe it into /usr/lib/sendmail.

But perhaps people don't configure this aspect of their Unix systems
properly these days.

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

[toc] | [prev] | [next] | [standalone]


#41340

Fromalexo <alelvb@inwind.it>
Date2016-02-29 14:16 +0100
Message-ID<nb1gbb$192k$1@gioia.aioe.org>
In reply to#40901
Il 14/02/2016 18:01, alexo ha scritto:
> Hello group,
>
> I've written for UNIX a little program [FLTK] that needs to send an
> order through e-mail.

[...]

I've found the bug!

PEBKAC !!

cutting and pasting the payload source function I had forgot to select a 
part of it. The compiler told me I had left an unmatched brace so I 
added it leaving a mangled function that was doing nothing. And so the 
infinite apparent data transmission.

All is well that ends well

Thank you all for your help and patience.


[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

Back to top | Article view | comp.lang.c++


csiph-web