gpt4 book ai didi

c - 如何使用 libetpan 在 HTML mime 消息中嵌入图像

转载 作者:行者123 更新时间:2023-11-30 21:37:44 25 4
gpt4 key购买 nike

如何使用 libetpan 在 HTML mime 消息中嵌入图像

我使用libetpan发送smtp消息我想在我的消息中嵌入图像

例如:

<html><body>
<img src="picture1.jpg" />
</body></html>

源代码

static struct mailmime * get_file_part(const char * filename, const char * mime_type,
const char * text, size_t length)
{
char * disposition_name;
int encoding_type;
struct mailmime_disposition * disposition;
struct mailmime_mechanism * encoding;
struct mailmime_content * content;
struct mailmime * mime;
struct mailmime_fields * mime_fields;

disposition_name = NULL;
if (filename != NULL) {
disposition_name = strdup(filename);
}
disposition = mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_INLINE,disposition_name, NULL, NULL, NULL, (size_t)-1);
content = mailmime_content_new_with_str(mime_type);

encoding_type = MAILMIME_MECHANISM_BASE64;
encoding = mailmime_mechanism_new(encoding_type, NULL);
mime_fields = mailmime_fields_new_with_data(encoding,NULL, NULL, disposition, NULL);
mime = part_new_empty(content, mime_fields, NULL, 0);
mailmime_set_body_text(mime, (char *)text, length);

return mime;
}
static struct mailmime * get_sample_file_part(void)
{
struct mailmime * part;

part = get_file_part("picture1.jpg", "multipart/related", FILEDATA, sizeof(FILEDATA) - 1);

return part;
}

在我的消息中使用嵌入对象

   embed_part = get_sample_file_part();
r = mailmime_smart_add_part(messagem, embed_part);
if (r != MAILIMF_NO_ERROR)
goto err;

////////////////////////////////////////////////////
MMAPString *str = mmap_string_new("<html><body>test<img alt=\"\" src=\"");
str = mmap_string_append(str, lastboundary );
str = mmap_string_append(str, "\" style=\"height: 256px; width: 296px\"/></body></html>");

https://github.com/dinhviethoa/libetpan

最佳答案

要将图像嵌入 HTML 邮件消息中,您需要符合 RFC 2387 。在这个页面上,有一个example介绍如何构建带有嵌入图像的 HTML 电子邮件。

这里有一个相关的堆栈溢出问题: embedding image in html email

以下是如何使用mailcore2构建这样的消息,它是 libetpan 的 ObjC/C++ 包装器:

MCOMessageBuilder * builder = [[MCOMessageBuilder alloc] init];

// text parts
[builder setTextBody:@"This is the plain text part of the email."];
[builder setHtmlBody:@"<html><body><img src="cid:masthead.png@qcode.co.uk>>
This is the html part of the email.
<img src="cid:logo.png@qcode.co.uk"></body></html>"];

// embedded images
[builder addRelatedAttachment:[MCOAttachment attachmentWithContentsOfFile:@"logo.png"]];
[builder addRelatedAttachment:[MCOAttachment attachmentWithContentsOfFile:@"masthead.png"]];

// other attachments
[builder addAttachment:[MCOAttachment attachmentWithContentsOfFile:@"Invoice_1.pdf"]];
[builder addAttachment:[MCOAttachment attachmentWithContentsOfFile:@"SpecialOffer.pdf"]];

// rendering
NSLog(@"%@", [[NSString alloc] initWithData:[builder data] encoding:NSUTF8StringEncoding]);

渲染效果应如下所示:

From: from@qcode.co.uk
To: to@@qcode.co.uk
Subject: Example Email
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="MixedBoundaryString"

--MixedBoundaryString
Content-Type: multipart/related; boundary="RelatedBoundaryString"

--RelatedBoundaryString
Content-Type: multipart/alternative; boundary="AlternativeBoundaryString"

--AlternativeBoundaryString
Content-Type: text/plain;charset="utf-8"
Content-Transfer-Encoding: quoted-printable

This is the plain text part of the email.

--AlternativeBoundaryString
Content-Type: text/html;charset="utf-8"
Content-Transfer-Encoding: quoted-printable

<html>
<body>
<img src=3D=22cid:masthead.png=40qcode.co.uk=22>
This is the html part of the email.
<img src=3D=22cid:logo.png=40qcode.co.uk=22>
</body>
</html>

--AlternativeBoundaryString--

--RelatedBoundaryString
Content-Type: image/jpgeg;name="logo.png"
Content-Transfer-Encoding: base64
Content-Disposition: inline;filename="logo.png"
Content-ID: <logo.png@qcode.co.uk>

amtsb2hiaXVvbHJueXZzNXQ2XHVmdGd5d2VoYmFmaGpremxidTh2b2hydHVqd255aHVpbnRyZnhu
dWkgb2l1b3NydGhpdXRvZ2hqdWlyb2h5dWd0aXJlaHN1aWhndXNpaHhidnVqZmtkeG5qaG5iZ3Vy
...
...
a25qbW9nNXRwbF0nemVycHpvemlnc3k5aDZqcm9wdHo7amlodDhpOTA4N3U5Nnkwb2tqMm9sd3An
LGZ2cDBbZWRzcm85eWo1Zmtsc2xrZ3g=

--RelatedBoundaryString
Content-Type: image/jpgeg;name="masthead.png"
Content-Transfer-Encoding: base64
Content-Disposition: inline;filename="masthead.png"
Content-ID: <masthead.png@qcode.co.uk>

aXR4ZGh5Yjd1OHk3MzQ4eXFndzhpYW9wO2tibHB6c2tqOTgwNXE0aW9qYWJ6aXBqOTBpcjl2MC1t
dGlmOTA0cW05dGkwbWk0OXQwYVttaXZvcnBhXGtsbGo7emt2c2pkZnI7Z2lwb2F1amdpNTh1NDlh
...
...
eXN6dWdoeXhiNzhuZzdnaHQ3eW9zemlqb2FqZWt0cmZ1eXZnamhka3JmdDg3aXV2dWd5aGVidXdz
dhyuhehe76YTGSFGA=

--RelatedBoundaryString--

--MixedBoundaryString
Content-Type: application/pdf;name="Invoice_1.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;filename="Invoice_1.pdf"

aGZqZGtsZ3poZHVpeWZoemd2dXNoamRibngganZodWpyYWRuIHVqO0hmSjtyRVVPIEZSO05SVURF
SEx1aWhudWpoZ3h1XGh1c2loZWRma25kamlsXHpodXZpZmhkcnVsaGpnZmtsaGVqZ2xod2plZmdq
...
...
a2psajY1ZWxqanNveHV5ZXJ3NTQzYXRnZnJhZXdhcmV0eXRia2xhanNueXVpNjRvNWllc3l1c2lw
dWg4NTA0

--MixedBoundaryString
Content-Type: application/pdf;name="SpecialOffer.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;filename="SpecialOffer.pdf"

aXBvY21odWl0dnI1dWk4OXdzNHU5NTgwcDN3YTt1OTQwc3U4NTk1dTg0dTV5OGlncHE1dW4zOTgw
cS0zNHU4NTk0eWI4OTcwdjg5MHE4cHV0O3BvYTt6dWI7dWlvenZ1em9pdW51dDlvdTg5YnE4N3Z3
...
...
OTViOHk5cDV3dTh5bnB3dWZ2OHQ5dTh2cHVpO2p2Ymd1eTg5MGg3ajY4bjZ2ODl1ZGlvcjQ1amts
dfnhgjdfihn=

--MixedBoundaryString--

关于c - 如何使用 libetpan 在 HTML mime 消息中嵌入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27491489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com