Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

send email and socket programming

120 views
Skip to first unread message

alexo

unread,
Feb 14, 2016, 12:01:29 PM2/14/16
to
Hello group,

I've written for UNIX a little program [FLTK] that needs to send an
order through e-mail.

I'm absolutely new to socket programming so you are my first attempt.
In a couple of tutorials I've built a pair of toy client/server programs
that showed me how to use sockets. It seems not so hard to program them,
but I need some help and infos.

My program is intended to be a stand alone program with a send button.
When the user hits the send button I'd like it (the program) sends an
email to my email address with the order message in it.

I know that SMTP port is 25, but I don't know how to configure the
aspects required.

Could you please guide me through that, or could you link me to a clear
intro of the subject?

My program, as already said, is written for Linux, but I'll later
convert it to Windows where it is intended to run.
Conversion of the GUI part is not necessary since FLTK is a
cross-platform graphic library, but socket programming is dependent on
architecture. The functions to invoke are of course different, and I'll
appreciate it so much if you could explain how to send email from a
C/C++ program using TCP/IP o SMTP for both linux and windows.

thank you

alexo

JiiPee

unread,
Feb 14, 2016, 12:07:56 PM2/14/16
to
I did that in my email project back in 2000 in a software company I was
working. I used a ready made class for doing that. Why not use a
classes/libraries to do it, why invent the weel again? Or is this more
like a learning process? That class I used worked very nicely and might
be even better now.

JiiPee

unread,
Feb 14, 2016, 12:12:37 PM2/14/16
to
If I remember correctly, it was actually this one:
http://www.codesink.org/mimetic_mime_library.html#intro

anyway, that looks something I was using

Paavo Helde

unread,
Feb 14, 2016, 12:18:48 PM2/14/16
to
On 14.02.2016 19:01, alexo wrote:
> Hello group,
>
> I've written for UNIX a little program [FLTK] that needs to send an
> order through e-mail.
>
> I'm absolutely new to socket programming so you are my first attempt.
> In a couple of tutorials I've built a pair of toy client/server programs
> that showed me how to use sockets. It seems not so hard to program them,
> but I need some help and infos.

In C++, when working with sockets I would strongly recommend using a
cross-platform library or framework for that, otherwise you will never
get the hairy platform-specific tricks correct.

Boost asio is an example of such a good library, probably there are
others. ASIO mean async i/O, it might be you don't need its asynchronous
capabilities in the beginning, so you can ignore them, the rest of the
library still does much work for you. Just look up the examples and
build on top of them.


Öö Tiib

unread,
Feb 14, 2016, 12:40:10 PM2/14/16
to
On Sunday, 14 February 2016 19:01:29 UTC+2, alexo wrote:
> Hello group,
>
> I've written for UNIX a little program [FLTK] that needs to send an
> order through e-mail.
>
> I'm absolutely new to socket programming so you are my first attempt.
> In a couple of tutorials I've built a pair of toy client/server programs
> that showed me how to use sockets. It seems not so hard to program them,
> but I need some help and infos.
>
> My program is intended to be a stand alone program with a send button.
> When the user hits the send button I'd like it (the program) sends an
> email to my email address with the order message in it.
>
> I know that SMTP port is 25, but I don't know how to configure the
> aspects required.
>
> Could you please guide me through that, or could you link me to a clear
> intro of the subject?

Why you use e-mail? There can be SSL/TLS connection made between SMTP,
IMAP or POP3 server and client but lot of servers do not do even that,
instead ask for authentication as open text. There also are no guarantee
any further, so e-mail can travel between servers as open text anyway.
So it is basically only worthless spam that can be sent by e-mail.

alexo

unread,
Feb 14, 2016, 1:45:28 PM2/14/16
to
Il 14/02/2016 18:07, JiiPee ha scritto:
> Why not use a classes/libraries to do it, why invent the weel again? Or is this more
> like a learning process? That class I used worked very nicely and might
> be even better now.

I didn't know there were these opportunities.
I Think I'll study a bit how socket programming works behind the scenes.
Thank you


woodb...@gmail.com

unread,
Feb 14, 2016, 1:47:47 PM2/14/16
to
I've written some software that supports sending messages
over sockets on Linux, Windows and other operating systems.
There's a library available here:

http://webEbenezer.net/build_integration.html

Feel free to use the library in your project.

I also have an on line code generator called the C++
Middleware Writer (CMW) that might be helpful. I'm
willing to donate 16 hours/week for six months to a
project that uses the CMW.

Also we'll pay $1,400 and give a $1,200 investment in the
company to someone who helps us find someone interested
in this offer. We'll pay the $1,400 after working for four
months on the project. Ebenezer Enterprises works to reward
investments to 3 times the original amount. So the
investment would result in between $0 and $3,600, depending
on how things go for the company.

Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net

Paavo Helde

unread,
Feb 14, 2016, 2:20:08 PM2/14/16
to
On 14.02.2016 19:01, alexo wrote:
> Hello group,
>
> I've written for UNIX a little program [FLTK] that needs to send an
> order through e-mail.
>
> I'm absolutely new to socket programming so you are my first attempt.
> In a couple of tutorials I've built a pair of toy client/server programs
> that showed me how to use sockets. It seems not so hard to program them,
> but I need some help and infos.
>
> My program is intended to be a stand alone program with a send button.
> When the user hits the send button I'd like it (the program) sends an
> email to my email address with the order message in it.

On a second thought, why do you need sockets at all? If you just want
the program to send you an order by e-mail, you could just open an URL like

mailto:your...@example.com?subject=Order&body=Order%20details%20here

In Windows, this should be passed to the ShellExecute SDK function. In
Linux I guess there is no 100% sure way, but xdg-email ought to work in
most environments.

This would also give the user an opportunity to review the order and add
additional comments.

alexo

unread,
Feb 14, 2016, 2:24:16 PM2/14/16
to
Il 14/02/2016 18:39, Öö Tiib ha scritto:

> Why you use e-mail?

This was the first and simplest idea I got. Otherwise I had to implement
a server and it is up to now out of my reach.

> There can be SSL/TLS connection made between SMTP,
> IMAP or POP3 server and client but lot of servers do not do even that,
> instead ask for authentication as open text. There also are no guarantee
> any further, so e-mail can travel between servers as open text anyway.

SSL/TLS is another thing should I have to learn? No thank you,
I'm not other than a hobbist.
That's too much for what I'm trying to do.

Thank you anyway.

alexo

unread,
Feb 14, 2016, 2:29:33 PM2/14/16
to
Il 14/02/2016 20:19, Paavo Helde ha scritto:

> On a second thought, why do you need sockets at all? If you just want
> the program to send you an order by e-mail, you could just open an URL like
>
> mailto:your...@example.com?subject=Order&body=Order%20details%20here

The software I intend to use formats the info of the order sent in a way
easy to intellect.
My clients are too lazy to compile a form from their own with all the
infos formatted in a readable way.


Ian Collins

unread,
Feb 14, 2016, 2:40:25 PM2/14/16
to
alexo wrote:
> Hello group,
>
> I've written for UNIX a little program [FLTK] that needs to send an
> order through e-mail.
>
> I'm absolutely new to socket programming so you are my first attempt.
> In a couple of tutorials I've built a pair of toy client/server programs
> that showed me how to use sockets. It seems not so hard to program them,
> but I need some help and infos.
>
> My program is intended to be a stand alone program with a send button.
> When the user hits the send button I'd like it (the program) sends an
> email to my email address with the order message in it.
>
> I know that SMTP port is 25, but I don't know how to configure the
> aspects required.
>
> Could you please guide me through that, or could you link me to a clear
> intro of the subject?

Your issue isn't socket programming, it is how to use SMTP! You need a
configured SMTP server to interact with and you need to know how to do this.

> My program, as already said, is written for Linux, but I'll later
> convert it to Windows where it is intended to run.
> Conversion of the GUI part is not necessary since FLTK is a
> cross-platform graphic library, but socket programming is dependent on
> architecture. The functions to invoke are of course different, and I'll
> appreciate it so much if you could explain how to send email from a
> C/C++ program using TCP/IP o SMTP for both linux and windows.

Using winsock on Windows is almost identical to using sockets on a POSIX
system.

--
Ian Collins

Chris M. Thomasson

unread,
Feb 14, 2016, 3:09:59 PM2/14/16
to
> "Ian Collins" wrote in message news:dic3gq...@mid.individual.net...

[...]

> Using winsock on Windows is almost identical to using sockets on a POSIX
> system.

FWIW, on Windows wrt winsock I use IOCP via "batched" dequeuing:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa364988(v=vs.85).aspx

Is there a direct Linux alternative?

I guess Kernel async io:

http://lse.sourceforge.net/io/aio.html

or (epoll):

http://davmac.org/davpage/linux/async-io.html#epoll

?

Paavo Helde

unread,
Feb 14, 2016, 3:29:47 PM2/14/16
to
I guess you overlooked the body field in the URL.

Jerry Stuckle

unread,
Feb 14, 2016, 4:05:12 PM2/14/16
to
Just beware that nowadays many ISP's are blocking outbound connections
to port 25 to prevent spam (often due to viruses and/or trojan horses.
This is especially true for residential connections.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstu...@attglobal.net
==================

Jens Thoms Toerring

unread,
Feb 14, 2016, 4:20:50 PM2/14/16
to
alexo <ale...@inwind.it> wrote:
> I've written for UNIX a little program [FLTK] that needs to send an
> order through e-mail.

> I'm absolutely new to socket programming so you are my first attempt.
> In a couple of tutorials I've built a pair of toy client/server programs
> that showed me how to use sockets. It seems not so hard to program them,
> but I need some help and infos.

> My program is intended to be a stand alone program with a send button.
> When the user hits the send button I'd like it (the program) sends an
> email to my email address with the order message in it.

> I know that SMTP port is 25, but I don't know how to configure the
> aspects required.

> Could you please guide me through that, or could you link me to a clear
> intro of the subject?

This rather likely won't work, even if you get everything right.
There are the following issues:

a) You need to figure out the mail server for the address
you want to send the email to. Many machines don't accept
connections on port 25 (or there's a firewall that prevents
accessing the port from the outside). You need to know the
mail server for the recipients domain. To get that you must
do an DNS query for the MX server of the destination domain.
b) Once you got the socket opened to that mail server you
need to understand the SMTP protocol to talk correctly
to the server and transfer the message.
c) While a and b can be done (though it requires quite a bit
of reading the corresponding RFCs) the killer is that most
mail servers nowadays don't accept mail from random machines
but only from the dedicated mail servers from the domain the
email is coming from. So unless the machine sending the mail
is marked in DNS as a mail server for the domain (i.e., has
an MX record) the mail won't get accepted.
d) And, if you want to talk to your own mail server to get it
to pass on the email on, many don't accept emails anymore
without encryption, so you also beed to know how to deal with
SSL/TLS.

On a UNIX system you'd better install and properly configure a
MTA (mail transport agent) that passes on the email to the
mail server for the senders domain that then forwards it to
the correct mail server on the receiving end. This typically
is done by spawning the 'sendmail' program (but which often
is a symbolic link to the actual MTA installed on the machine)
and let it deal with the details of handling the mail.

The nice old times when you could send out an email to your boss,
looking to the uninitiated as coming from 'god@heaven', are, thanks
to spam, over since a number of years.

If there's no usable MTA on the machine you intend to send mails
from you're screwed. And, sorry, no idea how to do it under
Windows...
Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

alexo

unread,
Feb 14, 2016, 4:40:50 PM2/14/16
to
Oh!
Really send a message to port 25 is so difficult?
So it seems my only chance is to build the server that listens to my
client program. But who assure me that the firewall doesn't stop that
incoming transmission?


alexo

unread,
Feb 14, 2016, 4:45:35 PM2/14/16
to
Probably I have not well understood your technique.
Say that I push the send data button in m program to build the message
how can I send it via mail using the mailto string you gave me?

Paavo Helde

unread,
Feb 14, 2016, 5:15:29 PM2/14/16
to
You run xdg-email with the URL as an argument, or invoke
ShellExecute("open", ...) with this URL, depending on the platform. This
should bring up a new mail window with the default e-mail app configured
for the system, with the pre-filled address, subject and body. The user
just needs to click the send button.

When reading other responses about firewalls etc, I'm starting to think
this might be the most robust approach after all.

Chris Vine

unread,
Feb 14, 2016, 5:20:18 PM2/14/16
to
On Sun, 14 Feb 2016 22:40:37 +0100
alexo <ale...@inwind.it> wrote:
[snip]
> Oh!
> Really send a message to port 25 is so difficult?
> So it seems my only chance is to build the server that listens to my
> client program. But who assure me that the firewall doesn't stop that
> incoming transmission?

You are missing the point. Sending a message to port 25 is not
difficult. There are also many implementations of SMTP servers out
there and it would be ridiculous to code your own: if you need one I
recommend postfix.

However, your putative user probably does not have a mail server
running on port 25 on localhost available to them for outgoing mail.
Most consumer users set up their mail client to forward mail directly
to their ISP's SMTP mail relay, and in such cases you would have to
provide some means of allowing your users to do the same (that is, enter
particulars of their mail relay), and then your program can call it up
on port 25 in the ordinary way.

If your user does have a mail server running on port 25 on localhost,
with any unix-like system you can get your program to fire up the user's
mailx program (this is the POSIX mail utility).

Chris

Paavo Helde

unread,
Feb 14, 2016, 5:39:45 PM2/14/16
to
On 15.02.2016 0:15, Paavo Helde wrote:
> On 14.02.2016 23:45, alexo wrote:
>> Il 14/02/2016 21:29, Paavo Helde ha scritto:
>>> On 14.02.2016 21:29, alexo wrote:
>>>> Il 14/02/2016 20:19, Paavo Helde ha scritto:
>>>>
>>>>> On a second thought, why do you need sockets at all? If you just want
>>>>> the program to send you an order by e-mail, you could just open an URL
>>>>> like
>>>>>
>>>>> mailto:your...@example.com?subject=Order&body=Order%20details%20here
>>>>
>>>> The software I intend to use formats the info of the order sent in a
>>>> way
>>>> easy to intellect.
>>>> My clients are too lazy to compile a form from their own with all the
>>>> infos formatted in a readable way.
>>>
>>> I guess you overlooked the body field in the URL.
>>>
>>
>> Probably I have not well understood your technique.
>> Say that I push the send data button in m program to build the message
>> how can I send it via mail using the mailto string you gave me?
>
> You run xdg-email with the URL as an argument,

Oh, and on Linux of course there should be the ancient 'mail' program
which can be automated even better and does not require user interaction
or X-Windows. I guess from a C or C++ program this can be best launched
by the popen() system call.




Chris Vine

unread,
Feb 14, 2016, 6:01:50 PM2/14/16
to
These days its mailx, according to IEEE Std 1003.1, 2013. However, it
won't help. First, ordinary users do not have mail servers running on
localhost, nor do they normally have sendmail or postfix installed.
Secondly, if they do, mail sent via them will probably be blocked, as
most spam blockers these days will reject mail not coming via a
recognised ISP's mail relay. It so happens that for my own reasons I
do have a mail server running on localhost on my laptop, but it just
forwards on to a recognised mail relay for that reason.

The OP's problem is that he does not understand how email works. What
he needs to do is obtain particulars of the user's ISP's mail relay,
or if the user is operating from business premises, of the business's
mail relay. On windows, in cases where a user has Outlook installed
there may well be a system call to obtain the address previously
entered by the user (or by the system administrator) for the purposes of
email she sends via Outlook (I don't know if there is or there isn't,
but it would not surprise me if there is). But that would only work
for users who do have Outlook installed, and there is no equivalent for
the general run of unix-like OSes. My mail client for example requires
me to enter the mail relay into a configuration dialog when I first
start up the program. Microsoft's consumer products do the same.

The other thing is that this has nothing to do with C++. The OP would
have the same problem if he were coding with Python, Java, C or any
other language.

Chris

Jerry Stuckle

unread,
Feb 14, 2016, 6:02:04 PM2/14/16
to
You are missing another point. Even if I do have an smtp server running
on my machine, I can't send email from it. Outbound connections to port
25 are blocked by my ISP. That is true for many ISPs - at least in the U.S.

And even if I could use port 25, many MTAs are configured to reject
email from hosts using dynamic IP addresses.

I use port 587 to connect to *my* MTA, which is in a data center and has
a static IP address. It also requires authentication and an encrypted
connection.

Jerry Stuckle

unread,
Feb 14, 2016, 6:05:25 PM2/14/16
to
No one does. The ISP can block it any time they wish.

In addition, you do NOT want to allow people to relay to other domains
through your server. It won't take long for spammers to find and start
using it, also. If you set up such a server, you must ensure you have
proper authentication in place.

Ian Collins

unread,
Feb 14, 2016, 6:09:31 PM2/14/16
to
Jerry Stuckle wrote:
> On 2/14/2016 5:20 PM, Chris Vine wrote:
>>
>> However, your putative user probably does not have a mail server
>> running on port 25 on localhost available to them for outgoing mail.
>> Most consumer users set up their mail client to forward mail directly
>> to their ISP's SMTP mail relay, and in such cases you would have to
>> provide some means of allowing your users to do the same (that is, enter
>> particulars of their mail relay), and then your program can call it up
>> on port 25 in the ordinary way.
>>
>> If your user does have a mail server running on port 25 on localhost,
>> with any unix-like system you can get your program to fire up the user's
>> mailx program (this is the POSIX mail utility).
>
> You are missing another point. Even if I do have an smtp server running
> on my machine, I can't send email from it. Outbound connections to port
> 25 are blocked by my ISP. That is true for many ISPs - at least in the U.S.

As Chris said, "Most consumer users set up their mail client to forward
mail directly to their ISP's SMTP mail relay".

> And even if I could use port 25, many MTAs are configured to reject
> email from hosts using dynamic IP addresses.

Except those to their mail relay, otherwise customers wouldn't be able
to send mail from a regular mail client.

--
Ian Collins

Geoff

unread,
Feb 14, 2016, 6:16:48 PM2/14/16
to
Paavo's suggestion is the best. You create your order text and create
the URL from it. The mailto: action causes the user's system to invoke
his mail program with the content you create. He can then edit it if
he likes (you'll need to allow for that at your end) and then he
transmits it from his own email account and you never touch TCP within
your application.

This has many advantages:
1. You don't have to reinvent the wheel.
2. You never touch a socket or need to learn SMTP.
3. You need not worry about port 25, port 465, SMTP, TCP or SSL.
4. It sends the message via the user's own defined email client and
his authenticated server.
5. You don't have to violate local firewall policies that might
prevent your program from making a connection. This reduces your
support workload when dealing with customers and problems with
your application.

... there are more but I'll stop here.

Geoff

unread,
Feb 14, 2016, 6:22:49 PM2/14/16
to
On Sun, 14 Feb 2016 23:01:31 +0000, Chris Vine
<chris@cvine--nospam--.freeserve.co.uk> wrote:

>hese days its mailx, according to IEEE Std 1003.1, 2013. However, it
>won't help. First, ordinary users do not have mail servers running on
>localhost, nor do they normally have sendmail or postfix installed.

I think you misunderstood. The mailto:: URL invokes the users default
mail client, not a server. There is no local server involved and those
services need not exist on the localhost, nor does the mail client
communicate with a localhost socket, it communicates with the user's
own mail server using his own client under his control and under his
own credentials.

Chris Vine

unread,
Feb 14, 2016, 6:35:16 PM2/14/16
to
Having just discussed it with myself, I can detect no signs of having
missed that. But the point is not that ISPs stop port 25 traffic
leaking out of their system (I have not come across that), but that the
spam blocker of the next ISP in the link will do so.

> I use port 587 to connect to *my* MTA, which is in a data center and
> has a static IP address. It also requires authentication and an
> encrypted connection.

That is great, but irrelevant to the OP's issue. I do encrypted
tunnelling from my house in the country (when I am there) to my house
in town, to dispatch my mail remotely.

I think you need to resist the temptation to show you are clever.

Chris

Jerry Stuckle

unread,
Feb 14, 2016, 6:37:57 PM2/14/16
to
On 2/14/2016 6:09 PM, Ian Collins wrote:
> Jerry Stuckle wrote:
>> On 2/14/2016 5:20 PM, Chris Vine wrote:
>>>
>>> However, your putative user probably does not have a mail server
>>> running on port 25 on localhost available to them for outgoing mail.
>>> Most consumer users set up their mail client to forward mail directly
>>> to their ISP's SMTP mail relay, and in such cases you would have to
>>> provide some means of allowing your users to do the same (that is, enter
>>> particulars of their mail relay), and then your program can call it up
>>> on port 25 in the ordinary way.
>>>
>>> If your user does have a mail server running on port 25 on localhost,
>>> with any unix-like system you can get your program to fire up the user's
>>> mailx program (this is the POSIX mail utility).
>>
>> You are missing another point. Even if I do have an smtp server running
>> on my machine, I can't send email from it. Outbound connections to port
>> 25 are blocked by my ISP. That is true for many ISPs - at least in
>> the U.S.
>
> As Chris said, "Most consumer users set up their mail client to forward
> mail directly to their ISP's SMTP mail relay".
>

I know what Chris said. I was addressing his point of using a mail
program on localhost. It will not work on many ISP's.

>> And even if I could use port 25, many MTAs are configured to reject
>> email from hosts using dynamic IP addresses.
>
> Except those to their mail relay, otherwise customers wouldn't be able
> to send mail from a regular mail client.
>

Incorrect. Many MTAs are configured to accept mail from other MTAs with
static IPs, but reject mail from dynamic IPs. It has nothing to do with
being able to send mail from a regular mail client.

--

Jerry Stuckle

unread,
Feb 14, 2016, 6:42:07 PM2/14/16
to
mailto: only works in a program which understands it (i.e. a browser).
Unless he builds support for it into his program, it will not work.

He will have to determine what the default mail client is and invoke it
from his program.

Chris Vine

unread,
Feb 14, 2016, 6:43:35 PM2/14/16
to
Yes, I agree that if they have that mimetype set up, that would indeed
work for the purpose of bringing up the mail client.

The OP's problem is getting his text form (the order message he
referred to) into the client. Is there a standard means of doing that?
(I am not saying there isn't, but I am not sure I have seen that when I
have sent emails that way myself).

Chris

Ian Collins

unread,
Feb 14, 2016, 7:07:09 PM2/14/16
to
I don't know about your part of the world, but here most customers are
on a dynamic IP. ISPs will accept incoming SMTP traffic from their
dynamic IP range. If they didn't, customers wouldn't be able to send
mail, would they?

--
Ian Collins

Geoff

unread,
Feb 14, 2016, 8:33:20 PM2/14/16
to
On Sun, 14 Feb 2016 23:43:22 +0000, Chris Vine
In Windows, I'd put the URL into a dialog box, such as a Help dialog
or in an Order Something dialog opened from the Help menu item. I'd do
it the same in OSX and the equivalent in Linux dialogs. The OP hasn't
stated his target OS but I suspect it's Linux.

A standard way to do it? I'm not so sure. I know this works in
Windows:
system("start

mailto:your...@example.com?subject=Order&body=Order%20details%20here");

I think the equivalent is
system("exec

mailto:your...@example.com?subject=Order&body=Order%20details%20here");

but I haven't tested this.

Jerry Stuckle

unread,
Feb 14, 2016, 9:40:31 PM2/14/16
to
That's right. Same here. However, ISPs will accept traffic from
*AUTHORIZED* users. They do not accept traffic from non-authorized
users which are not for their domain(s). And MTAs from other domains
are NOT authorized users.

You obviously have no idea how to properly configure an MTA. I hope you
never do - your concept is known as a spam relay.

But coming from you, that doesn't surprise me in the least.

Jerry Stuckle

unread,
Feb 14, 2016, 9:43:29 PM2/14/16
to
It is not unusual in the U.S. - and from what I understand, many other
parts of the world.

>> I use port 587 to connect to *my* MTA, which is in a data center and
>> has a static IP address. It also requires authentication and an
>> encrypted connection.
>
> That is great, but irrelevant to the OP's issue. I do encrypted
> tunnelling from my house in the country (when I am there) to my house
> in town, to dispatch my mail remotely.
>
> I think you need to resist the temptation to show you are clever.
>
> Chris
>

It is *totally* relevant. If he tries to use his code on my (or any of
hundreds of other ISP's in the U.S.), his code will fail miserably. It
may not cause a problem for *you* - but *you* are not every user (or
even a majority of users, if you're in the U.S.) trying to use his code.

Paavo Helde

unread,
Feb 15, 2016, 2:38:14 AM2/15/16
to
Yes, ability to open things in a default way (including mail URL-s) is
built into Windows and can be invoked via the start command in the CMD
shell. However, from another program one should prefer ShellExecute()
instead of system(), to avoid a nasty black DOS box on the screen, and
to avoid nasty quoting problems (for example, the '&' in the above URL
must be escaped as '^&' when used in the CMD shell).

>
> I think the equivalent is
> system("exec
>
> mailto:your...@example.com?subject=Order&body=Order%20details%20here");
>
> but I haven't tested this.

I haven't either, but the man page looks like this is the way in
X-Windows world:

XDG-EMAIL(1)
NAME
xdg-email - command line tool for sending mail using the user's
preferred e-mail composer.

And yes, this requires that the user has the ability to send e-mail from
this computer in the first place and the computer has been set up
properly. If there is a doubt the user cannot do this, I would display
the URL and tell the user to copy-paste it into a browser on a machine
where he can send e-mails.






Chris Vine

unread,
Feb 15, 2016, 5:05:10 AM2/15/16
to
On Sun, 14 Feb 2016 17:33:02 -0800
OK that does seem to be the way to do it. Excellent. 'xdg-email
--help' gives me this:

xdg-email -- command line tool for sending mail using the
user's preferred e-mail composer

Synopsis

xdg-email [--utf8] [--cc address] [--bcc address] [--subject
text] [--body text] [--attach file] [ mailto-uri | address(es)]

So this on a unix-like system would do what he wants:

xdg-email --subject Order --body "My order is attached" \
--attach "order-file.txt" or...@supplier.net

From a cursory look at it I could not get my email client to accept
line breaks in the message body, so attaching a file might work better
for the OP.

Chris

Chris Vine

unread,
Feb 15, 2016, 5:14:41 AM2/15/16
to
On Mon, 15 Feb 2016 10:04:44 +0000
Chris Vine <chris@cvine--nospam--.freeserve.co.uk> wrote:
[snip]
> From a cursory look at it I could not get my email client to accept
> line breaks in the message body, so attaching a file might work better
> for the OP.

Ah, it does accept line breaks, it's the shell that's problematic. If
he executes it via 'exec' and cognates rather than 'system' he will
probably be OK.

Geoff

unread,
Feb 15, 2016, 1:11:48 PM2/15/16
to
Thanks for testing that.

Ultimately, I'd probably still prefer just planting a URL mailto: or
just a link to a web server for feedback. An http: link has much
greater utility since the form and cgi is under the vendor's control
and doesn't require any support from inside the application, it's
completely portable and can be redirected if necessary to whatever
server the vendor desires. If, as the OP has hinted at, it's some kind
of order where money or a credit card could be involved, it can be
securely transacted as a web form over SSL rather than an email,
especially where the user's identifiable information is going to be
transmitted. This gives the vendor complete flexibility on the web
server to control the form fields or change its purpose. Web servers
are cheap and easily obtained these days, any serious application
would have a web presence an users are inclined to expect it. Email
addresses are transitory and coding one into an application is
probably unwise.

Christopher Pisz

unread,
Feb 16, 2016, 5:39:20 PM2/16/16
to
On 2/14/2016 11:01 AM, alexo wrote:
> Hello group,
>
> I've written for UNIX a little program [FLTK] that needs to send an
> order through e-mail.
>
> I'm absolutely new to socket programming so you are my first attempt.
> In a couple of tutorials I've built a pair of toy client/server programs
> that showed me how to use sockets. It seems not so hard to program them,
> but I need some help and infos.
>
> My program is intended to be a stand alone program with a send button.
> When the user hits the send button I'd like it (the program) sends an
> email to my email address with the order message in it.
>
> I know that SMTP port is 25, but I don't know how to configure the
> aspects required.
>
> Could you please guide me through that, or could you link me to a clear
> intro of the subject?
>
> My program, as already said, is written for Linux, but I'll later
> convert it to Windows where it is intended to run.
> Conversion of the GUI part is not necessary since FLTK is a
> cross-platform graphic library, but socket programming is dependent on
> architecture. The functions to invoke are of course different, and I'll
> appreciate it so much if you could explain how to send email from a
> C/C++ program using TCP/IP o SMTP for both linux and windows.
>
> thank you
>
> alexo


If you want to do it the old school way then you learn how to program
sockets and then you learn the protocol you want to use. In this case SMTP.

In linux, you would use Berkely Sockets.
In Windows, you would use Winsock.

Both have a plethora of tutorials and books that all go through the same
thing.

The guide I used some decades ago:
http://beej.us/guide/bgnet/output/html/multipage/index.html

If you want something more modern, after you learn how they actually
work, then you would use boost::asio or some 3rd party library.
boost::asio is much easier to understand after you know what you are
doing with Berkley sockets.

After you can program sockets sufficiently, you would just google up the
RFC for whatever protocol you want to use. In this case SMTP:

http://www.rfc-base.org/txt/rfc-2821.txt

But, again, with something as popular as SMTP, someone else has probably
already created a library that will save you the hassle of learning
every significant detail.




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

alexo

unread,
Feb 25, 2016, 10:09:19 PM2/25/16
to

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

any help?

thank you

Paavo Helde

unread,
Feb 26, 2016, 12:49:38 AM2/26/16
to
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.

Paavo Helde

unread,
Feb 26, 2016, 1:39:55 AM2/26/16
to
Or alternatively, some firewall is blocking your communication attempts.


alexo

unread,
Feb 26, 2016, 7:15:42 AM2/26/16
to
>> 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.

Paavo Helde

unread,
Feb 26, 2016, 7:40:44 AM2/26/16
to
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.

Christopher Pisz

unread,
Feb 26, 2016, 5:41:57 PM2/26/16
to
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?

alexo

unread,
Feb 27, 2016, 7:39:24 AM2/27/16
to
> 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.



alexo

unread,
Feb 27, 2016, 7:42:44 AM2/27/16
to
> 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.




Öö Tiib

unread,
Feb 27, 2016, 8:16:08 AM2/27/16
to
On Saturday, 27 February 2016 14:39:24 UTC+2, alexo wrote:

Restoring attributions, do not erase those. :(
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"?

alexo

unread,
Feb 27, 2016, 12:44:28 PM2/27/16
to
>> 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


alexo

unread,
Feb 27, 2016, 12:59:33 PM2/27/16
to
> 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;
}

Jorgen Grahn

unread,
Feb 27, 2016, 1:00:02 PM2/27/16
to
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 .

alexo

unread,
Feb 27, 2016, 1:14:38 PM2/27/16
to
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, " ");
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, " ");

alexo

unread,
Feb 27, 2016, 1:22:44 PM2/27/16
to
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

Öö Tiib

unread,
Feb 27, 2016, 1:42:02 PM2/27/16
to
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.

Paavo Helde

unread,
Feb 27, 2016, 2:47:17 PM2/27/16
to
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.



guinne...@gmail.com

unread,
Feb 28, 2016, 5:20:29 PM2/28/16
to
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

alexo

unread,
Feb 29, 2016, 8:16:50 AM2/29/16
to
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.



0 new messages