Quantcast
Channel: User trojanfoe - Stack Overflow
Viewing all articles
Browse latest Browse all 226

Answer by trojanfoe for sending an email from a C/C++ program in linux

$
0
0

You could invoke your local MTA directly using popen() and feed it RFC822-compliant text.

#include <stdio.h>#include <string.h>#include <errno.h>int sendmail(const char *to, const char *from, const char *subject, const char *message){    int retval = -1;    FILE *mailpipe = popen("/usr/lib/sendmail -t", "w");    if (mailpipe != NULL) {        fprintf(mailpipe, "To: %s\n", to);        fprintf(mailpipe, "From: %s\n", from);        fprintf(mailpipe, "Subject: %s\n\n", subject);        fwrite(message, 1, strlen(message), mailpipe);        fwrite(".\n", 1, 2, mailpipe);        pclose(mailpipe);        retval = 0;     }     else {         perror("Failed to invoke sendmail");     }     return retval;}main(int argc, char** argv){    if (argc == 5) {        sendmail(argv[1], argv[2], argv[3], argv[4]);    }}

Viewing all articles
Browse latest Browse all 226

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>