The simple method: /bin/mail


For simple applications, it may be sufficient to invoke mail
(usually `/bin/mail', but could be `/usr/bin/mail' on some
systems).



WARNING: Some versions of UCB Mail may execute commands
prefixed by `~!' or `~|' given in the message body even in
non-interactive mode. This can be a security risk.



Invoked as `mail -s 'subject' recipients...' it will take a message
body on standard input, and supply a default header (including the
specified subject), and pass the message to sendmail for
delivery.



This example mails a test message to root on the local system:




#include <stdio.h>

#define MAILPROG "/bin/mail"

int main()
{
FILE *mail = popen(MAILPROG " -s 'Test Message' root", "w");
if (!mail)
{
perror("popen");
exit(1);
}

fprintf(mail, "This is a test.\n");

if (pclose(mail))
{
fprintf(stderr, "mail failed!\n");
exit(1);
}
}



If the text to be sent is already in a file, then one can do:




system(MAILPROG " -s 'file contents' root </tmp/filename");



These methods can be extended to more complex cases, but there are many
pitfalls to watch out for:







[an error occurred while processing this directive]