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:
-
If using system() or popen(), you must be very careful about quoting
arguments to protect them from filename expansion or word splitting
-
Constructing command lines from user-specified data is a common source
of buffer-overrun errors and other security holes
-
This method does not allow for CC: or BCC: recipients to be specified
(some versions of /bin/mail may allow this, some do not)
Home | FAQ |