Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I am using libcurl to send SMTP emails from a ubuntu server.
I have been asked to change the emails to have a HTML body from a temple stored in a
file.
Is libcurl able to send the HTML body and if so any advice or example on how to
achieve it would be appreciated.
Thank You
Regards

What I have tried:

I have tried putting the HTML in the message but it is not decoded
Posted
Updated 19-Jun-22 0:16am
Comments
Afzaal Ahmad Zeeshan 21-Nov-18 3:17am    
This thread seems to solve a similar problem as yours, https://stackoverflow.com/questions/37610573/how-to-set-mail-body-as-html-in-curl, please see that. Also visit this flag for more information, https://curl.haxx.se/libcurl/c/CURLOPT_READFUNCTION.html

This example[^] might help.
Curl fully support HTML in email's body.
 
Share this answer
 
Try this

char *msg = "To: bob@example.com\r\n"
            "From: alice@example.com\r\n"
            "Content-Type: text/html; charset=us-ascii\r\n"
            "Mime-version: 1.0\r\n"
            "\r\n"
            "<html><head>\r\n"
            "<meta http-equiv=\"Content-Type\" content="text/html; charset=us-ascii\">\r\n"
            "</head><body>\r\n"
            "<p>Hi Bob</p>\r\n"
            "</body></html>\r\n"

size_t callback(char *buffer, size_t size, size_t nitems, void *instream) {
    /* you actually need to check that buffer <= size * nitems */
    strcat(buffer, msg);
    return strlen(buffer);
}

curl_easy_setopt(curl, CURLOPT_READFUNCTION, callback);
curl_easy_perform(curl);
 
Share this answer
 
You must include MIME-Version as well as Content-Type, for example this will work, assuming every line ends with "\r\n".

C++
MIME-Version: 1.0
Date: Thu, 16 Jun 2022 14:12:05 +0000
To: Test <test@gmail.com>
From: Info <info@test.business>
Message-ID: <5083142354314465839@something>
Subject: Post Test 1
Content-Type: text/html; charset="UTF-8"

<h1>Header</h1><b>Bold</b><br><div style="color:#f00">Red text</div>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900