简体   繁体   中英

Add strings to a file buffer, for message body of cURL smtp send email

I'm trying to modify a working cURL email send example to add a message body . I'm unsure why all of the curl email-with-attachment examples I'm finding have no message body.

I'm needing to send short text emails with a PDF file attachment.

This is what I have tried so far, with the lines un-commented, it compiles and runs, but fails to send. I understand that the message body should be separated from the Subject by one "\\r\\n" (blank line), but this isn't the correct method.

//Create structure of email to be sent
    fileBuf = new char[ADD_SIZE + no_of_rows][CHARS];  //ADD_SIZE for TO,FROM,SUBJECT,CONTENT-TYPE,CONTENT-TRANSFER-
                                                       //ENCODING,CONETNT-DISPOSITION and \r\n
    strcpy(fileBuf[len++],"To: " TO "\r\n");
    buffer_size += strlen(fileBuf[len-1]);
    strcpy(fileBuf[len++],"From: " FROM "\r\n");
    buffer_size += strlen(fileBuf[len-1]);
    strcpy(fileBuf[len++],"Subject: SMTP TLS example message\r\n");
    buffer_size += strlen(fileBuf[len-1]);

    //strcpy(fileBuf[len++],"\r\n");
    //buffer_size += strlen(fileBuf[len-1]);
    //strcpy(fileBuf[len++],"Message goes here, hopefully...\r\n");
    //buffer_size += strlen(fileBuf[len-1]);

    strcpy(fileBuf[len++],"Content-Type: application/x-msdownload; name=\"" FILENAME "\"\r\n");
    buffer_size += strlen(fileBuf[len-1]);
    strcpy(fileBuf[len++],"Content-Transfer-Encoding: base64\r\n");
    buffer_size += strlen(fileBuf[len-1]);
    strcpy(fileBuf[len++],"Content-Disposition: attachment; filename=\"" FILENAME "\"\r\n");
    buffer_size += strlen(fileBuf[len-1]);
    strcpy(fileBuf[len++],"\r\n");
    buffer_size += strlen(fileBuf[len-1]);

The full project code is here See Solution 7.

Any advice on how to accomplish this would be greatly appreciated.

[edit] Test using very simple cURL, produced 0 Byte attachment:

#define FILENAME  "Rpt05162017.pdf"
static const char *payload_text[] = {
  "To: " TO "\r\n",
  "From: " FROM "(Example User)\r\n",
  //"Cc: " CC "(Another example User)\r\n",
  "Subject: SMTPS Example\r\n",
  "Date: 17-May-2017\r\n",
  "User-Agent: My eMail Client\r\n",
  "MIME-Version: 1.0\r\n",
  "Content-Type: multipart/mixed;\r\n",
   " boundary=\"------------030203080101020302070708\"\r\n",
  "\r\nThis is a multi-part message in MIME format.\r\n",
  "--------------030203080101020302070708\r\n",
  "Content-Type: text/plain; charset=utf-8; format=flowed\r\n",
  "Content-Transfer-Encoding: 7bit\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\r\n",
  "--------------030203080101020302070708\r\n",
  "Content-Type: application/x-msdownload; name=\"" FILENAME "\"\r\n",
  "Content-Transfer-Encoding: base64\r\n",
  "Content-Disposition: attachment; filename=\"" FILENAME "\"\r\n",
  "\r\n--------------030203080101020302070708--",
  NULL
};

It's not clear how you send all that stuff. You created 2d-array of strings and you strcpy into each your headers. All of them have padding bytes that will corrupt your message.

Try to simplify it maybe?

std:string header =
"To: " TO "\r\n"
"From: " FROM "\r\n"
"Subject: SMTP TLS example message\r\n"
"Content-Type: application/x-msdownload; name=\"" FILENAME "\"\r\n"
"Content-Transfer-Encoding: base64\r\n"
"Content-Disposition: attachment; filename=\"" FILENAME "\"\r\n"
"\r\n";

and then simply send your message header followed by the body of your message.


With your updated payload_text it's not a surprise that you get 0-size attachments because you are sending empty file. After you send your payload_text you have to send your body of the file and then after the body you need to append multipart closing suffix:

"\r\n--------------030203080101020302070708--"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM