Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send e-mail in C?

Tags:

c

email

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

like image 979
mehmet6parmak Avatar asked Mar 02 '10 12:03

mehmet6parmak


2 Answers

Use libcurl. It supports SMTP and also TLS, in case you need to authenticate for sending. They offer some example C code.

like image 77
Marc Kline Avatar answered Sep 28 '22 00:09

Marc Kline


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[] = "[email protected]"; // 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 :)

like image 21
codaddict Avatar answered Sep 28 '22 00:09

codaddict