[英]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.