Send email using system( ) function

N

Nick Li

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" (e-mail address removed) < attached_file');
return 0;
}

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

Thanks in advance.

Nick
 
E

Emmanuel Delahaye

In said:
#include <stdio.h>
#include <stdio.h>

int main(void)
{
system('mailx -s "Hello" (e-mail address removed) < attached_file');

Try that:

system("mailx -s \"Hello\" (e-mail address removed) < attached_file");
 
M

Mike Wahler

Nick Li said:
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" (e-mail address removed) < 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
 
E

Eric Sosman

Alan said:
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" (e-mail address removed) <
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.
 
D

Default User

Alan said:
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" (e-mail address removed) <
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
 
K

Keith Thompson

Alan Balmer said:
[...]
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.
 
R

Richard Bos

Emmanuel Delahaye said:
Try that:

system("mailx -s \"Hello\" (e-mail address removed) < attached_file");

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

Richard
 
A

Alan Balmer

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

Peter Shaggy Haywood

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top