Search code examples
c++emailcurlsend

How to send email using c++ (curl)


How to add Subject to this email?

    #include <windows.h>

int main(void){
    char* command = "curl smtp://smtp.gmail.com:587 -v --mail-from \"[email protected]\" --mail-rcpt \"[email protected]\" --ssl -u [email protected]:PASSWORD -T \"ATTACHMENT.FILE\" -k --anyauth";
    WinExec(command, SW_HIDE);
    return 0;
}

Solution

  • There are 2 ways to send mail with subject in cURL: Command Line and From C++ code.

    Command Line:

    The subject can be specified in email data text file "email.txt"

    curl smtp://mail.example.com --mail-from [email protected] --mail-rcpt
    [email protected] --upload-file email.txt
    

    Here is the tutorial: cURL_SMTP_Command_Line

    From C++ code:

    In this case you specify Subject in payload_text.

      static const char *payload_text[] = {
      "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
      "To: " TO_MAIL "\r\n",
      "From: " FROM_MAIL "\r\n",
      "Cc: " CC_MAIL "\r\n",
      "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
      "rfcpedant.example.org>\r\n",
      "Subject: SMTP example message\r\n",
      "\r\n", /* empty line to divide headers from body, see RFC5322 */ 
      "The body of the message starts here.\r\n",
      "\r\n",
      "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
      "Check RFC5322.\r\n",
      NULL
    };
    

    Here is the example: cURL_SMTP_From_Code