C programming mail

A classic topic!

In C programming, 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:

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

int main() {
    FILE *fp;
    char buffer[1024];

    fp = popen("sendmail -t", "w");
    if (fp == NULL) {
        printf("Error opening pipe\n");
        return 1;
    }

    // Write the email message
    fprintf(fp, "To: [email protected]\n");
    fprintf(fp, "From: [email protected]\n");
    fprintf(fp, "Subject: Test email\n");
    fprintf(fp, "Hello, this is a test email.\n");
    fprintf(fp, "\n");

    // Close the pipe
    pclose(fp);
    return 0;
}

This code uses the popen function to open a pipe to the sendmail command, writes the email message to the pipe, and then closes the pipe using pclose.

Using libcurl library:

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

int main() {
    CURL *curl;
    CURLcode res;
    char buffer[1024];

    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, "Hello, this is a test email.\n");
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
        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;
}

This code uses the libcurl library to send an email using the SMTP protocol. It sets up a CURL object, specifies the SMTP server, sender, recipient, and email message, and then performs the email send using curl_easy_perform.

Using libmail library:

#include <libmail.h>
#include <stdio.h>

int main() {
    Mail *mail;
    MailMessage *message;
    MailRecipient *recipient;
    MailSender *sender;

    mail = mail_new();
    if (mail == NULL) {
        printf("Error creating mail object\n");
        return 1;
    }

    message = mail_message_new(mail);
    if (message == NULL) {
        printf("Error creating message\n");
        return 1;
    }

    recipient = mail_recipient_new(message, "[email protected]");
    if (recipient == NULL) {
        printf("Error creating recipient\n");
        return 1;
    }

    sender = mail_sender_new(message, "[email protected]");
    if (sender == NULL) {
        printf("Error creating sender\n");
        return 1;
    }

    mail_message_set_subject(message, "Test email");
    mail_message_set_body(message, "Hello, this is a test email.\n");

    if (mail_send(mail)!= 0) {
        printf("Error sending email\n");
        return 1;
    }

    mail_free(mail);
    return 0;
}

This code uses the libmail library to send an email. It creates a Mail object, a MailMessage object, and sets up the sender, recipient, subject, and body of the email. Finally, it sends the email using mail_send.

Note that these examples are just a starting point, and you may need to modify them to suit your specific requirements. Additionally, you may need to install additional libraries or dependencies to use these examples.