How can I send e-mail in C?

c, email

Solution

On Unix like systems you can use `system` and `sendmail` as follows:

#include <stdio.h>
#include <string.h>

int main() {

        char cmd[100];  // to hold the command.
        char to[] = "sample@example.com"; // email id of the recepient.
        char body[] = "SO rocks";    // email body.
        char tempFile[100];     // name of tempfile.

        strcpy(tempFile,tempnam("/tmp","sendmail")); // generate temp file name.

        FILE *fp = fopen(tempFile,"w"); // open it for writing.
        fprintf(fp,"%s\n",body);        // write body to it.
        fclose(fp);             // close it.

        sprintf(cmd,"sendmail %s < %s",to,tempFile); // prepare command.
        system(cmd);     // execute it.

        return 0;
}

I know its ugly and there are several better ways to do it...but it works :)

Problem

I'm just wondering how can I send email using C? I Googled it a little bit, but could not find anything proper.

Original source

Related problems