I'm just wondering how can I send email using C? I Googled it a little bit, but could not find anything proper.
Use libcurl. It supports SMTP and also TLS, in case you need to authenticate for sending. They offer some example C code.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With