C send mail with attachemebt

A classic!

To send an email with an attachment using C, you'll need to use a library that provides SMTP (Simple Mail Transfer Protocol) functionality. One popular option is libcurl.

Here's an example code snippet that demonstrates how to send an email with an attachment using libcurl:

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

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

    // Set up the email details
    const char *from = "[email protected]";
    const char *to = "[email protected]";
    const char *subject = "Test email with attachment";
    const char *body = "This is a test email with an attachment.";
    const char *attachment = "path/to/attachment.txt";

    // Initialize curl
    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    if(curl) {
        // Set the email details
        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
        curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, to);
        curl_easy_setopt(curl, CURLOPT_MAIL_SUBJECT, subject);
        curl_easy_setopt(curl, CURLOPT_MAIL_BODY, body);

        // Add the attachment
        curl_easy_setopt(curl, CURLOPT_MAIL_ATTACH, attachment);

        // Set the SMTP server details
        curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.example.com");
        curl_easy_setopt(curl, CURLOPT_USERNAME, "username");
        curl_easy_setopt(curl, CURLOPT_PASSWORD, "password");

        // Send the email
        res = curl_easy_perform(curl);
        if(res!= CURLE_OK) {
            fprintf(stderr, "cURL error: %s\n", curl_easy_strerror(res));
        }

        // Clean up
        curl_easy_cleanup(curl);
    }

    curl_global_cleanup();
    return 0;
}

This code uses libcurl to send an email with an attachment to the specified recipient. You'll need to replace the placeholders ([email protected], [email protected], path/to/attachment.txt, smtp.example.com, username, and password) with your actual email details.

Note that this is just a basic example, and you may need to modify the code to suit your specific requirements. Additionally, you'll need to ensure that you have libcurl installed and configured on your system.

If you're using a Linux system, you can install libcurl using your package manager (e.g., apt-get or yum). On Windows, you can download the libcurl binaries from the official website and add them to your project's dependencies.