[英]Add strings to a file buffer, for message body of cURL smtp send email
我正在尝试修改工作的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]);
完整的项目代码在这里,请参阅解决方案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
};
不清楚如何发送所有这些东西。 您创建了2D字符串数组,并将其插入每个标头。 它们都有填充字节,它们会破坏您的消息。
尝试简化它?
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
,发送零文件的附件并不奇怪,因为您发送的是空文件。 发送payload_text
您必须发送文件正文,然后在正文之后附加多部分结束后缀:
"\r\n--------------030203080101020302070708--"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.