Please,
don't use the sample code above.
It has severe security flaws, like allowing for buffers overflows and shell escape sequences attacks.
To send mail using C, try this (untested) code snippet:
Code:
#define SENDMAIL "/usr/sbin/sendmail -i -t"
FILE *sm;
sm = popen(SENDMAIL, "w");
if ( !sm )
{
your_errno_handling_routine();
return 1;
}
fprintf(sm, "From: %s\n", from);
fprintf(sm, "To: %s\n", to);
fprintf(sm, "Subject: %s\n", subject);
/* print other headers here */
fputc('\n', sm);
fputs(message, sm);
if ( pclose(sm) == -1 )
{
your_errno_handling_routine();
return 1;
}
The trickiest part is to find the correct value for the SENDMAIL macro constant. There's is
no standard command line to run the system mailer, though calling /usr/sbin/sendmail and a subset of its parameters is becoming a
de facto standard (even other mailers, such as Postfix, install a somewhat compatible /usr/sbin/sendmail).
If you want to write portable programs, use autoconf to discover a sane value for SENDMAIL.