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

时间:2014-12-15 19:20:11

标签: c mime mailcore mailcore2

如何使用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

1 个答案:

答案 0 :(得分:1)

要在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--