How to send a mail using c

In C, you can send an email using the sendmail command or by using a library like libcurl or libmail. Here are some examples:

Using sendmail command:

You can use the system function to execute the sendmail command and send an email. Here's an example:

#include <stdio.h>
#include <stdlib.h>

int main() {
    system("sendmail -t < email.txt");
    return 0;
}

In this example, email.txt is a file that contains the email message in the format of a mail message (e.g., From: [email protected], To: [email protected], Subject: My Subject, Body: This is the email body...).

Using libcurl library:

You can use the libcurl library to send an email by creating a CURL object and setting the email headers and body. Here's an example:

#include <curl/curl.h>
#include <stdio.h>

int main() {
    CURL *curl;
    CURLcode res;

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.example.com");
        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "[email protected]");
        curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, "[email protected]");
        curl_easy_setopt(curl, CURLOPT_READDATA, "email.txt");
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_email);
        res = curl_easy_perform(curl);
        if(res!= CURLE_OK) {
            fprintf(stderr, "cURL error: %s\n", curl_easy_strerror(res));
        }
    }
    curl_easy_cleanup(curl);
    curl_global_cleanup();
    return 0;
}

static size_t read_email(void *ptr, size_t size, size_t nmemb, void *data) {
    FILE *fp = (FILE *)data;
    size_t bytes_read = fread(ptr, 1, size * nmemb, fp);
    return bytes_read;
}

In this example, email.txt is a file that contains the email message in the format of a mail message (e.g., From: [email protected], To: [email protected], Subject: My Subject, Body: This is the email body...).

Using libmail library:

You can use the libmail library to send an email by creating a Mail object and setting the email headers and body. Here's an example:

#include <mail.h>
#include <stdio.h>

int main() {
    Mail *mail;
    mail = mail_new();
    if(mail) {
        mail_set_from(mail, "[email protected]");
        mail_set_to(mail, "[email protected]");
        mail_set_subject(mail, "My Subject");
        mail_set_body(mail, "This is the email body...");
        mail_send(mail);
        mail_free(mail);
    }
    return 0;
}

In this example, you need to install the libmail library and include the mail.h header file.

Note that these examples are just a starting point, and you may need to modify them to fit your specific use case. Additionally, you may need to handle errors and exceptions properly.