Search code examples
cmimemailcoremailcore2

how to embed an image in an HTML mime message with libetpan


how to embed an image in an HTML mime message with libetpan

i use libetpan to send smtp message and i want embed an image in my message

for example :

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

source code

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

using embed object in my message

   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


Solution

  • To embed an image in an HTML mail message, you need to conform to RFC 2387. On this page, there's an example of how to build an HTML email with an embedded image.

    There's a related stack overflow question here: embedding image in html email

    Here's how to build such a message with mailcore2, which is a ObjC/C++ wrapper of 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: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]);
    

    Here's what the rendering should look like:

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