【问题标题】:Add strings to a file buffer, for message body of cURL smtp send email将字符串添加到文件缓冲区,用于 cURL smtp 发送电子邮件的消息正文
【发布时间】:2017-10-20 06:25:31
【问题描述】:

我正在尝试修改有效的 cURL 电子邮件发送示例以添加邮件正文。 我不确定为什么我发现的所有带有附件的 curl 电子邮件示例都没有消息正文。

我需要发送带有 PDF 文件附件的短文本电子邮件。

这是我迄今为止尝试过的,未注释行,它编译并运行,但无法发送。我了解邮件正文应与主题分开一个“\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]);

完整的项目代码是here 参见解决方案7。

任何关于如何实现这一点的建议将不胜感激。

[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
};

【问题讨论】:

    标签: c++ linux email curl


    【解决方案1】:

    不清楚你是如何发送所有这些东西的。您创建了二维字符串数组,并将 strcpy 放入每个标题中。它们都有填充字节,会损坏您的消息。

    可以尝试简化吗?

    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";
    

    然后只需发送您的消息header,然后发送您的消息的body


    使用您更新的payload_text,您收到 0 大小的附件并不奇怪,因为您发送的是空文件。发送 payload_text 后,您必须发送文件正文,然后在正文之后附加多部分结束后缀:

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

    【讨论】:

    • 我在一个基本的 cURL 示例上测试了这个简单的方法,但是附件包含 0 个字节。见上面的编辑。
    • 谢谢 Pavel,“在你发送你的 payload_text 之后,你必须发送你的文件正文,然后在正文之后你需要附加多部分结束后缀:” 我不是由于缺少代码而发送文件正文?我发送一个空文件是因为我的代码只缺少这个结束后缀吗? (没有区别)"\r\n--------------030203080101020302070708--"我是否缺少其他必要的代码?我没有在 curl.haxx.se 上找到包含文本消息和附件的有效负载公式的详细信息。细节赞赏。
    猜你喜欢
    • 2013-02-18
    • 2021-02-06
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    相关资源
    最近更新 更多