Important: Please read the Qt Code of Conduct -

Send HTML email with libcurl?

  • Hello,

    I have project to send mail using libcurl with SMTP and proxy (I have to) and plain text is working fine but I need to change some color of part of the text sent, then I have to send HTML email for that.

    I try with header parameters :

        headers = curl_slist_append(headers, "Accept-Charset: \"UTF-8\"");
        headers  = curl_slist_append(headers, "Content-Type: \"text/html\"");
        curl_easy_setopt(this->curl, CURLOPT_HTTPHEADER,headers);

    but nothing work, could you help me?

    Thank you very much


    #include <QDebug>
    #include <curl/curl.h>
    #include <iostream>
    #include <cstddef>
    using namespace std;
    struct upload_status {
        int lines_read;
    class mailNotification : public QObject
        QString from;
        QString to;
        QString cc;
        QString subject;
        char *message;
        QString date;
        QString mail;
        QString xxx;
        QString HtmlBody;
        long response_code;
        struct upload_status upload_ctx;
        QString status;
        CURL *curl;
        CURLcode res;
        struct curl_slist *recipients;
        char *msg;
        void resetParameters();
        void setPayloadText();
        void setDate();
        static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp);
        void setUser(QString username, QString password);
        void setAllCurlOptions();
        void sendEmail(QString  username, QString  password, QString  from, QString  subject, const char *message);
        void changeFrom(QString f);
        void changeTo(QString t);
        void changeCc(QString c);
        void changeSubject(QString s);
        void changeMessage(QString m);
        QString username;
        QString password;
        static char *payload_text;


    #include <QSqlDatabase>
    #include <QtSql>
    #include <QDateTime>
    #include <cstring>
    #include <yvals.h>
    #include "mailnotification.h"
    #pragma once
    #ifndef _CSTRING_
    #define _CSTRING_
    #include <yvals.h>
    #ifdef _STD_USING
     #undef _STD_USING
      #include <string.h>
     #define _STD_USING
    #else /* _STD_USING */
     #include <string.h>
    #endif /* _STD_USING */
     #if _GLOBAL_USING && !defined(RC_INVOKED)
    using _CSTD size_t; using _CSTD memchr; using _CSTD memcmp;
    using _CSTD memcpy; using _CSTD memmove; using _CSTD memset;
    using _CSTD strcat; using _CSTD strchr; using _CSTD strcmp;
    using _CSTD strcoll; using _CSTD strcpy; using _CSTD strcspn;
    using _CSTD strerror; using _CSTD strlen; using _CSTD strncat;
    using _CSTD strncmp; using _CSTD strncpy; using _CSTD strpbrk;
    using _CSTD strrchr; using _CSTD strspn; using _CSTD strstr;
    using _CSTD strtok; using _CSTD strxfrm;
     #endif /* _GLOBAL_USING */
    #endif /* _CSTRING_ */
    char* mailNotification::payload_text = NULL;
    void mailNotification::resetParameters()
        this->res = CURLE_OK;
        this->recipients = NULL;
        this->upload_ctx.lines_read = 0;
        //qDebug() << "init curl done";
    void mailNotification::setDate()
        QDateTime now = QDateTime::currentDateTime();
        this->date = "test";//now.toString("dd/MM/yyyy hh:mm");
        //qDebug() << "date";
    size_t mailNotification::payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
        struct upload_status *upload_ctx = (struct upload_status *)userp;
        char *data;
        char *tt;
        if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
            return 0;
        data = strchr(payload_text, '\n');
            size_t pSize = strlen(payload_text);
            size_t dSize = strlen(data);
            size_t diff = pSize-dSize;
            tt = (char*)malloc((diff + 1) * sizeof(char));
            strncpy(tt, payload_text, diff+1);
            tt[diff+1] = '\0';
            if(tt) {
                size_t len = strlen(tt);
                memcpy(ptr, tt, len);
                payload_text = data + 1;
                return len;
        return 0;
    void mailNotification::setUser(QString username, QString password)
        this->username = username;
        this->password = password;
    void mailNotification::setAllCurlOptions()
        QString mailTo=""
        this->recipients = curl_slist_append(this->recipients, this->mailTo.toStdString().c_str());
        curl_easy_setopt(this->curl, CURLOPT_HEADER, 1);
        curl_easy_setopt(this->curl, CURLOPT_VERBOSE, 1);
        curl_easy_setopt(this->curl, CURLOPT_USERNAME, this->username.toStdString().c_str());
        curl_easy_setopt(this->curl, CURLOPT_PASSWORD, this->password.toStdString().c_str());
        curl_easy_setopt(this->curl, CURLOPT_MAIL_FROM, this->from.toStdString().c_str());
        curl_easy_setopt(this->curl, CURLOPT_MAIL_RCPT, this->recipients);
        curl_easy_setopt(this->curl, CURLOPT_URL, "smtp://");
        curl_easy_setopt(this->curl, CURLOPT_UPLOAD, 1L);
        if(this->cc != ""){
            this->recipients = curl_slist_append(this->recipients, this->cc.toStdString().c_str());
        curl_easy_setopt(this->curl, CURLOPT_READFUNCTION, this->payload_source);
        curl_easy_setopt(this->curl, CURLOPT_READDATA, &upload_ctx);
        curl_easy_setopt(this->curl, CURLOPT_UPLOAD, 1L);
    void mailNotification::sendEmail(QString username, QString  password, QString from, QString subject, const char *message)
        this->curl = curl_easy_init();
        setUser(username, password);
        this->res = curl_easy_perform(this->curl);
        /* Check for errors */
        if(this->res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(this->res));
        if(res == CURLE_OK) {
          curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
          qDebug()<<"status c++ "<<status;
    void mailNotification::changeFrom(QString f)
        this->from = f;
    void mailNotification::changeTo(QString t)
        this->to = t;
    void mailNotification::changeCc(QString c)
        this->cc = c;
    void mailNotification::changeSubject(QString s)
        this->subject = s;
    void mailNotification::changeMessage(QString m)
        this->HtmlBody = m;
    void mailNotification::setPayloadText()
        payload_text = NULL;
        QString to_str = "To: <" +this->to+ ">\r\n";
        QString from_str = "From: <" +this->from+ ">\r\n";
        QString cc_str = "Cc: <" + this->cc + ">\r\n";
        QString date_str = "Date: " + this->date;
        QString subject_str = "Subject: " + this->subject +"\r\n";
        QString message_str = this->HtmlBody + "\r\n";
        if(this->cc == ""){
            xxx = date_str + to_str + from_str + QString("Message-ID: <>\r\n") + subject_str + QString("\r\n") + message_str;
            xxx = date_str + to_str + from_str + cc_str + subject_str + QString("\r\n") + message_str;
        payload_text = (char*)malloc(xxx.length()*sizeof(char*));
        strcpy(payload_text ,xxx.toStdString().c_str());


    #include <QApplication>
    #include <QtSql>
    #include "mailnotification.h"
    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        mailNotification email;
        static const char inline_html[] =
          "<p>This is the inline <b>HTML</b> message of the e-mail.</p>"
          "<br />\r\n"
          "<p>It could be a lot of HTML data that would be displayed by "
          "e-mail viewers able to handle HTML.</p>"
     return a.exec();

  • @filipdns

    That seems to be more suitable question for a curl forum. What you are using is not part of Qt libs.

Log in to reply