简体   繁体   English

将字符串添加到文件缓冲区,以便cURL smtp的消息正文发送电子邮件

[英]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 . 我正在尝试修改工作的cURL电子邮件发送示例以添加消息正文 I'm unsure why all of the curl email-with-attachment examples I'm finding have no message body. 我不确定为什么我发现的所有带有附件的curl电子邮件示例都没有消息正文。

I'm needing to send short text emails with a PDF file attachment. 我需要发送带有PDF文件附件的短文本电子邮件。

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. 我知道消息正文应与主题之间用一个“ \\ r \\ n”(空行)分隔,但这不是正确的方法。

//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. 完整的项目代码在这里,请参阅解决方案7。

Any advice on how to accomplish this would be greatly appreciated. 任何有关如何完成此操作的建议将不胜感激。

[edit] Test using very simple cURL, produced 0 Byte attachment: [edit]使用非常简单的cURL进行测试,产生了0字节的附件:

#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. 您创建了2D字符串数组,并将其插入每个标头。 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. 然后简单地发送邮件header随后body您的留言。


With your updated payload_text it's not a surprise that you get 0-size attachments because you are sending empty file. 使用更新的payload_text ,发送零文件的附件并不奇怪,因为您发送的是空文件。 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: 发送payload_text您必须发送文件正文,然后在正文之后附加多部分结束后缀:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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