By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,364 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,364 IT Pros & Developers. It's quick & easy.

Send email using system( ) function

P: n/a
Hi,

I am trying to writing a c program to send email using system()
function call on Unix(Sun Solaris). I tried the following:

#include <stdio.h>
#include <stdio.h>

int main(void)
{
system('mailx -s "Hello" js****@hotmail.com < attached_file');
return 0;
}

This doesn't seem to work. I would appreciate any advice.

Thanks in advance.

Nick
Nov 13 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
In 'comp.lang.c', ni********@hotmail.com (Nick Li) wrote:
#include <stdio.h>
#include <stdio.h>

int main(void)
{
system('mailx -s "Hello" js****@hotmail.com < attached_file');
Try that:

system("mailx -s \"Hello\" js****@hotmail.com < attached_file");

return 0;
}

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #2

P: n/a

Nick Li <ni********@hotmail.com> wrote in message
news:bf**************************@posting.google.c om...
Hi,

I am trying to writing a c program to send email using system()
function call on Unix(Sun Solaris). I tried the following:

#include <stdio.h>
#include <stdio.h>

int main(void)
{
system('mailx -s "Hello" js****@hotmail.com < attached_file');
return 0;
}

This doesn't seem to work. I would appreciate any advice.
'system()'s argument must be a 'C-style' string
(zero-terminated array of characters.) C-style
string literals are expressed by putting them
between a pair of double quotes ("). That's not
what you have above.

system("something");

If your string itself needs to contain double quotes,
use the corresponding escape sequence:

system("abc\"def\"ghi"); /* sends the string abd"def"ghi */
/* to the command processor */

-Mike


Thanks in advance.

Nick

Nov 13 '05 #3

P: n/a
Alan Balmer wrote:

Your immediate problem is answered else-thread. A minor point - check
the return value from system(). If it's actually the last statement in
the program, you can use

return (system('mailx -s "Hello" js****@hotmail.com <
attached_file'));

If it's used in a script, the script can check the success of the
call.


The value returned by system() is implementation-defined,
and may or may not have anything to do with the exit status
of the invoked program.

On Solaris, system() happens to return an encoding of the
exit status of the invoked shell -- but this behavior isn't
guaranteed for all C implementations, and besides the encoding
isn't itself immediately meaningful as an exit status.

--
Er*********@sun.com
Nov 13 '05 #4

P: n/a


Alan Balmer wrote:
Your immediate problem is answered else-thread. A minor point - check
the return value from system(). If it's actually the last statement in
the program, you can use

return (system('mailx -s "Hello" js****@hotmail.com <
attached_file'));

If it's used in a script, the script can check the success of the
call.


Maybe, maybe not. There's no guarantee on what if any return there is
from system() other than when the argument is NULL to check for the
presence of a command processor.


Brian Rodenborn
Nov 13 '05 #5

P: n/a
Alan Balmer <al******@att.net> writes:
On Mon, 07 Jul 2003 18:24:13 -0400, Eric Sosman <Er*********@sun.com>
wrote: [...]
The value returned by system() is implementation-defined,
and may or may not have anything to do with the exit status
of the invoked program.

Implementation defined, yes, but defined by the same implementation on
which the program is being executed,


Agreed.
thus likely to be consistent with
any shell implemented on the same system.


Not necessarily. In particular,

<OT>
you might want to try the following on your Solaris system:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int result = system("/bin/false");
printf("result = %d\n", result);
return result;
}

"/bin/false" does an "exit 255". The value returned by system() is
65280, or 255 << 8. When you return this value from the main program,
all but the low-order 8 bits are stripped off, leaving 0.
</OT>

The details are, of course, implementation-defined; the point is that
they can be implementation-defined in surprising ways, even on systems
you're familiar with.

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #6

P: n/a
Emmanuel Delahaye <em**********@noos.fr> wrote:
In 'comp.lang.c', ni********@hotmail.com (Nick Li) wrote:
#include <stdio.h>
#include <stdio.h>

int main(void)
{
system('mailx -s "Hello" js****@hotmail.com < attached_file');


Try that:

system("mailx -s \"Hello\" js****@hotmail.com < attached_file");


And for completeness, #include <stdlib.h> instead of #including
<stdio.h> - twice!

Richard
Nov 13 '05 #7

P: n/a
On Tue, 08 Jul 2003 07:24:11 GMT, Keith Thompson <ks*@cts.com> wrote:
Alan Balmer <al******@att.net> writes:
On Mon, 07 Jul 2003 18:24:13 -0400, Eric Sosman <Er*********@sun.com>
wrote:[...]

<snip><OT>
you might want to try the following on your Solaris system:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int result = system("/bin/false");
printf("result = %d\n", result);
return result;
}

"/bin/false" does an "exit 255". The value returned by system() is
65280, or 255 << 8. When you return this value from the main program,
all but the low-order 8 bits are stripped off, leaving 0.
</OT>

The details are, of course, implementation-defined; the point is that
they can be implementation-defined in surprising ways, even on systems
you're familiar with.


Actually, I don't have a Solaris system :-) Believe it or not, I
don't find the result surprising - I would expect some such. It's been
a while since I read the (several pages of) POSIX spec on this
subject, but I seem to remember that only 8 bits are returned from
main, but the system call would return (for success) 0 in the lower 8
bits and further information in the upper bits, which can always be
decoded by the standard macros. I have a local campaign going to get
maintenance programmers to change the "exit(-1)" our code is liberally
sprinkled with.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 13 '05 #8

P: n/a
Groovy hepcat Nick Li was jivin' on 7 Jul 2003 14:14:28 -0700 in
comp.lang.c.
Send email using system( ) function's a cool scene! Dig it!
I am trying to writing a c program to send email using system()
function call on Unix(Sun Solaris). I tried the following:

#include <stdio.h>
#include <stdio.h>


One more point others seem to have missed: you have included stdio.h
twice. You need to include stdlib.h for the system() function.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 13 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.