2014-12-15 2 views
-5

, как вставлять изображения в сообщении HTML мим с libetpanкак вставлять изображения в сообщении HTML мим с libetpan

я использую 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

Чтобы встроить изображение в почтовом сообщении HTML, вы должны соответствовать RFC 2387. На этой странице есть example о том, как создать HTML-письмо со встроенным изображением.

Там есть связанные Переполнение вопрос стека здесь: embedding image in html email

Вот как построить такое сообщение с mailcore2, который является оболочкой ObjC/C++ из libetpan:

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:[email protected]>> 
    This is the html part of the email. 
    <img src="cid:[email protected]"></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: [email protected] 
To: [email protected]@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: <[email protected]> 

amtsb2hiaXVvbHJueXZzNXQ2XHVmdGd5d2VoYmFmaGpremxidTh2b2hydHVqd255aHVpbnRyZnhu 
dWkgb2l1b3NydGhpdXRvZ2hqdWlyb2h5dWd0aXJlaHN1aWhndXNpaHhidnVqZmtkeG5qaG5iZ3Vy 
... 
... 
a25qbW9nNXRwbF0nemVycHpvemlnc3k5aDZqcm9wdHo7amlodDhpOTA4N3U5Nnkwb2tqMm9sd3An 
LGZ2cDBbZWRzcm85eWo1Zmtsc2xrZ3g= 

--RelatedBoundaryString 
Content-Type: image/jpgeg;name="masthead.png" 
Content-Transfer-Encoding: base64 
Content-Disposition: inline;filename="masthead.png" 
Content-ID: <[email protected]> 

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

 Смежные вопросы

  • Нет связанных вопросов^_^