如何在curl中将邮件正文设置为html?

Bin*_*abu 2 c++ curl http

我需要将 html 表作为电子邮件正文发送。我只是做了下面提到的内容类型为 html,但它没有工作。

headers = curl_slist_append(headers, "Content-Type: text/html");
/* pass our list of custom made headers */
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
Run Code Online (Sandbox Code Playgroud)

我在图书馆网站上找不到示例。

gro*_*mal 5

CURLOPT_HTTPHEADER不适用于任何 SMTP 选项(CURLOPT_MAIL_FROMCURLOPT_MAIL_RCPTCURLOPT_MAIL_AUTH)。相反,您需要使用CURLOPT_READFUNCTION.

/* Disclaimer: untested code */
char *msg = "To: bob@example.com\r\n"
            "From: alice@example.com\r\n"
            "Content-Type: text/html; charset=us-ascii\r\n"
            "Mime-version: 1.0\r\n"
            "\r\n"
            "<html><head>\r\n"
            "<meta http-equiv=\"Content-Type\" content="text/html; charset=us-ascii\">\r\n"
            "</head><body>\r\n"
            "<p>Hi Bob</p>\r\n"
            "</body></html>\r\n"

size_t callback(char *buffer, size_t size, size_t nitems, void *instream) {
    /* you actually need to check that buffer <= size * nitems */
    strcat(buffer, msg);
    return strlen(buffer);
}

curl_easy_setopt(curl, CURLOPT_READFUNCTION, callback);
curl_easy_perform(curl);
Run Code Online (Sandbox Code Playgroud)

文档CURLOPT_READFUNCTION有更多信息。虽然,如果您已经在发送纯文本电子邮件,那么您已经在那里了。

在通过sameerkn 链接的Curl 发送邮件中没有介绍的唯一真正的“技巧”是您只需将标题转储到电子邮件缓冲区中。没有像 HTTP 那样巧妙的标头设置。Content-Type


另外:我不确定您是否需要Content-Transfer-Encoding标题,我已将charset上述设置为us-asciiutf-8可能需要传输编码之类的东西。