C code for mail sending in webform

Here is an example of C code that can be used to send an email from a web form using the libcurl library:

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

#define MAX_LENGTH 1024

int main() {
    CURL *curl;
    CURLcode res;
    char *url = "smtp://your-smtp-server.com:587"; // replace with your SMTP server
    char *username = "your-username"; // replace with your email username
    char *password = "your-password"; // replace with your email password
    char *from = "[email protected]"; // replace with the email address you want to send from
    char *to = "[email protected]"; // replace with the email address you want to send to
    char *subject = "Test Email"; // replace with the subject of the email
    char *body = "This is a test email sent from a web form"; // replace with the body of the email

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_USERNAME, username);
        curl_easy_setopt(curl, CURLOPT_PASSWORD, password);
        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
        curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, to);
        curl_easy_setopt(curl, CURLOPT_READDATA, body);
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, 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. You'll need to replace the placeholders (your-smtp-server.com, your-username, your-password, [email protected], [email protected], Test Email, and This is a test email sent from a web form) with your actual email server, username, password, from email address, to email address, subject, and body.

You'll also need to compile this code with the libcurl library. Here's an example of how you can do this:

gcc -o email_sender email_sender.c -lcurl

Once you've compiled the code, you can run it using the following command:

./email_sender

This will send an email using the SMTP protocol. Note that this code does not handle errors or exceptions, so you may want to add additional error handling code to make it more robust.

Also, keep in mind that this is just an example code, you should consider using a more secure way to store and handle your email credentials, and also consider using a more robust email sending library that can handle more complex email sending tasks.

It's also worth noting that this code is not suitable for sending emails from a web form, as it's a command-line program and not a web application. You would need to create a web application that uses a library like libcurl to send emails, and then use a web framework like Apache or Nginx to serve the web form and handle the email sending.

If you're looking for a more robust solution, you may want to consider using a PHP or Python script that uses a library like PHPMailer or smtplib to send emails. These libraries are designed specifically for sending emails and can handle more complex tasks like handling attachments and HTML emails.