Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Send HTML email with libcurl?
Qt 6.11 is out! See what's new in the release blog

Send HTML email with libcurl?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 939 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    filipdns
    wrote on last edited by
    #1

    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

    mailnotification.h:

    #ifndef MAILNOTIFICATION_H
    #define MAILNOTIFICATION_H
    
    #include <QDebug>
    #include <curl/curl.h>
    #include <iostream>
    #include <cstddef>
    
    using namespace std;
    
    struct upload_status {
        int lines_read;
    };
    
    
    class mailNotification : public QObject
    {
        Q_OBJECT
    
    public:
        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);
    
    private:
        QString username;
        QString password;
        static char *payload_text;
    };
    
    
    #endif // MAILNOTIFICATION_H
    
    

    mailnotification.cpp

    #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)
    _STD_BEGIN
    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;
    _STD_END
     #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();
        qDebug()<<now;
        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');
    
    
        if(data)
        {
            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);
                upload_ctx->lines_read++;
                payload_text = data + 1;
                //free(tt);
                return len;
            }
        }
    
        return 0;
    }
    
    void mailNotification::setUser(QString username, QString password)
    {
        this->username = username;
        this->password = password;
    }
    
    void mailNotification::setAllCurlOptions()
    {
        QString mailTo="xxx.gmail.com"
        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://mailex.ign.fr:25");
        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);
        changeFrom(from);
        changeCc("filipdns@gmail.com");
        changeSubject(subject);
        changeMessage(message);
        resetParameters();
        setPayloadText();
        setAllCurlOptions();
    
        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);
          status=curl_easy_strerror(this->res);//QString::number(response_code);
          qDebug()<<"status c++ "<<status;
    
        }
        curl_slist_free_all(this->recipients);
        curl_easy_cleanup(curl);
    }
    
    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: <dcd7cb36-11db-487a-9f3a-e652a9668efd@rfcpedant.example.org>\r\n") + subject_str + QString("\r\n") + message_str;
        }else{
            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());
    
    }
    

    man.cpp

    #include <QApplication>
    #include <QtSql>
    #include "mailnotification.h"
    
    
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        mailNotification email;
    
        static const char inline_html[] =
          "<html><body>\r\n"
          "<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>"
          "</body></html>\r\n";
        email.sendEmail("ident","password","mailFrom@gmail.com","Status",inline_html);
     return a.exec();
    
    K 1 Reply Last reply
    0
    • F filipdns

      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

      mailnotification.h:

      #ifndef MAILNOTIFICATION_H
      #define MAILNOTIFICATION_H
      
      #include <QDebug>
      #include <curl/curl.h>
      #include <iostream>
      #include <cstddef>
      
      using namespace std;
      
      struct upload_status {
          int lines_read;
      };
      
      
      class mailNotification : public QObject
      {
          Q_OBJECT
      
      public:
          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);
      
      private:
          QString username;
          QString password;
          static char *payload_text;
      };
      
      
      #endif // MAILNOTIFICATION_H
      
      

      mailnotification.cpp

      #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)
      _STD_BEGIN
      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;
      _STD_END
       #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();
          qDebug()<<now;
          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');
      
      
          if(data)
          {
              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);
                  upload_ctx->lines_read++;
                  payload_text = data + 1;
                  //free(tt);
                  return len;
              }
          }
      
          return 0;
      }
      
      void mailNotification::setUser(QString username, QString password)
      {
          this->username = username;
          this->password = password;
      }
      
      void mailNotification::setAllCurlOptions()
      {
          QString mailTo="xxx.gmail.com"
          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://mailex.ign.fr:25");
          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);
          changeFrom(from);
          changeCc("filipdns@gmail.com");
          changeSubject(subject);
          changeMessage(message);
          resetParameters();
          setPayloadText();
          setAllCurlOptions();
      
          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);
            status=curl_easy_strerror(this->res);//QString::number(response_code);
            qDebug()<<"status c++ "<<status;
      
          }
          curl_slist_free_all(this->recipients);
          curl_easy_cleanup(curl);
      }
      
      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: <dcd7cb36-11db-487a-9f3a-e652a9668efd@rfcpedant.example.org>\r\n") + subject_str + QString("\r\n") + message_str;
          }else{
              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());
      
      }
      

      man.cpp

      #include <QApplication>
      #include <QtSql>
      #include "mailnotification.h"
      
      
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          mailNotification email;
      
          static const char inline_html[] =
            "<html><body>\r\n"
            "<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>"
            "</body></html>\r\n";
          email.sendEmail("ident","password","mailFrom@gmail.com","Status",inline_html);
       return a.exec();
      
      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @filipdns

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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved