libEtPan! API
Prev | Chapter 4. MIME | Next

# Creation functions

## mailmime_disposition_new_filename and mailmime_disposition_new_with_data

    #include <libetpan/libetpan.h>

    enum {
      MAILMIME_DISPOSITION_TYPE_ERROR,
      MAILMIME_DISPOSITION_TYPE_INLINE,
      MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
      MAILMIME_DISPOSITION_TYPE_EXTENSION
    };

    struct mailmime_disposition *
    mailmime_disposition_new_filename(int type, char * filename);

    struct mailmime_disposition *
    mailmime_disposition_new_with_data(int type,
        char * filename, char * creation_date, char * modification_date,
        char * read_date, size_t size);

These functions will create a MIME content disposition information.

- type a standard MIME disposition : MAILMIME_DISPOSITION_TYPE_INLINE or MAILMIME_DISPOSITION_TYPE_ATTACHMENT.

  filename is the filename.

  creation_date is the creation date.

  modification_date is the modification date.

  read_date is the last read date.

  size is the size of the file.

- This will return a MIME content disposition (see the Section called mailmime_disposition - MIME disposition information (Content-Disposition)).

Example 4-35. creating a MIME content disposition

    #include <libetpan/libetpan.h>

    int main(int argc, char ** argv)
    {
      struct mailmime_disposition * disposition;

      disposition =
        mailmime_disposition_new_filename(MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
          strdup("foo-bar.txt"));

      /* do the things */

      mailmime_disposition_free(disposition);
    }

## mailmime_fields_new_empty and mailmime_fields_add

    #include <libetpan/libetpan.h>

    struct mailmime_fields * mailmime_fields_new_empty(void);

    int mailmime_fields_add(struct mailmime_fields * fields,
    			struct mailmime_field * field);

mailmime_fields_new_empty() will create a new empty MIME header fields list.

mailmime_fields_add() will add MIME header fields to the MIME header fields list.

- fields. The MIME header field will be added to this MIME header fields list (see the Section called mailmime_fields - header fields).

- field is the MIME header field to add (see the Section called mailmime_field - MIME header field).

Example 4-36. creating a MIME header fields list

    #include <libetpan/libetpan.h>

    int main(int argc, char ** argv)
    {
      struct mailmime_fields * fields;
      struct mailmime_field * field;

      fields = mailmime_fields_new_empty();
      field = build_mime_field();

      /* do the things */

      mailmime_fields_add(fields, field);

      mailmime_fields_free(fields);
    }

## mailmime_fields_new_with_data and mailmime_fields_new_with_version

    #include <libetpan/libetpan.h>

    struct mailmime_fields *
    mailmime_fields_new_with_data(struct mailmime_mechanism * encoding,
    			      char * id,
    			      char * description,
    			      struct mailmime_disposition * disposition,
    			      struct mailmime_language * language);

    struct mailmime_fields *
    mailmime_fields_new_with_version(struct mailmime_mechanism * encoding,
    				 char * id,
    				 char * description,
    				 struct mailmime_disposition * disposition,
    				 struct mailmime_language * language);

mailmime_fields_new_with_data() will create a MIME header fields list with all the given fields (NULL can be used for the value if the field must not be present). MIME-Version header field will not be added.

mailmime_fields_new_with_version() will create a MIME header fields list with all the given fields (NULL can be used for the value if the field must not be present). MIME-Version header field will be added.

Example 4-37. creating new fields

    #include <libetpan/libetpan.h>

    int main(int argc, char ** argv)
    {
      struct mailmime_disposition * disposition;
      struct mailmime_fields * mime_fields;
      struct mailmime_mechanism * encoding;

      encoding = mailmime_mechanism_new(MAILMIME_MECHANISM_BASE64, NULL);

      disposition =
        mailmime_disposition_new_filename(MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
          strdup("foo-bar.txt"));

      mime_fields = mailmime_fields_new_with_version(encoding, NULL,
        NULL, disposition, NULL);

      /* do the things */

      mailmime_fields_free(mime_fields);
    }

## mailmime_get_content_message

    #include <libetpan/libetpan.h>

    struct mailmime_content * mailmime_get_content_message(void);

    struct mailmime_content * mailmime_get_content_text(void);

    struct mailmime_content * mailmime_content_new_with_str(const char * str);

mailmime_get_content_message() will create a MIME content type message/rfc822.

mailmime_get_content_text() will create a MIME content type plain/text.

mailmime_get_content_new_with_str() will create a MIME content type given by the string plain/text.

str. This string will NOT be referenced by any structure. This string will only be parsed to create the structure.

Example 4-38. Creating a MIME content type

    #include <libetpan/libetpan.h>

    int main(int argc, char ** argv)
    {
      struct mailmime_content * content;

      content = mailmime_get_content_message();

      /* do the things */

      mailmime_content_free(content);
    }

    int main(int argc, char ** argv)
    {
      struct mailmime_content * content;

      content = mailmime_get_content_text();

      /* do the things */

      mailmime_content_free(content);
    }

    int main(int argc, char ** argv)
    {
      struct mailmime_content * content;

      content = mailmime_get_content_new_with_str("multipart/mixed");

      /* do the things */

      mailmime_content_free(content);
    }

## mailmime_data_new_data and mailmime_data_new_file

    #include <libetpan/libetpan.h>

    enum {
      MAILMIME_MECHANISM_ERROR,
      MAILMIME_MECHANISM_7BIT,
      MAILMIME_MECHANISM_8BIT,
      MAILMIME_MECHANISM_BINARY,
      MAILMIME_MECHANISM_QUOTED_PRINTABLE,
      MAILMIME_MECHANISM_BASE64,
      MAILMIME_MECHANISM_TOKEN
    };

    struct mailmime_data *
    mailmime_data_new_data(int encoding, int encoded,
    		       const char * data, size_t length);

    struct mailmime_data *
    mailmime_data_new_file(int encoding, int encoded,
    		       char * filename);

mailmime_data_new_data() will create a new MIME content, using a string in memory.

mailmime_data_new_file() will create a new MIME content, using a file.

- encoding is the MIME encoding mechanism used to encode this part. The value can be MAILMIME_MECHANISM_7BIT, MAILMIME_MECHANISM_8BIT, MAILMIME_MECHANISM_BINARY, MAILMIME_MECHANISM_QUOTED_PRINTABLE or MAILMIME_MECHANISM_BASE64 (see the Section called mailmime_mechanism - MIME transfer encoding mechanism (Content-Transfer-Encoding)).

- encoded is set to 1 if the part is already encoded with the mechanism given in encoding.

- data is a pointer to the content of the part.

- length is the length of the data.

- filename is the name of the file.

Example 4-39. creating MIME content

    #include <libetpan/libetpan.h>

    #define DATA_STR "my data"

    int main(int argc, char ** argv)
    {
      struct mailmime_data * data;

      data = mailmime_data_new_data(MAILMIME_MECHANISM_BASE64, 0,
    		                DATA_STR, sizeof(DATA_STR) - 1);

      /* do the things */

      mailmime_data_free(data);
    }

    int main(int argc, char ** argv)
    {
      struct mailmime_data * data;

      data = mailmime_data_new_file(MAILMIME_MECHANISM_BASE64, 0,
    		                strdup("foo-bar.txt"));

      /* do the things */

      mailmime_data_free(data);
    }

## mailmime_new_message_data, mailmime_new_empty and mailmime_new_with_content

    #include <libetpan/libetpan.h>

    struct mailmime *
    mailmime_new_message_data(struct mailmime * msg_mime);

    struct mailmime *
    mailmime_new_empty(struct mailmime_content * content,
    		   struct mailmime_fields * mime_fields);

    int
    mailmime_new_with_content(const char * content_type,
    			  struct mailmime_fields * mime_fields,
    			  struct mailmime ** result);

    struct mailmime * mailmime_multiple_new(const char * type);

mailmime_new_message_data() will create a new MIME message with the given subpart.

mailmime_new_empty() will create a new MIME part with the given content type and MIME fields but with no content.

mailmime_new_with_content() will create a new MIME part with a content type given by a string and a given MIME fields list.

mailmime_multiple_new() will create a new MIME multipart with a content type given by a string.

- msg_mime is the sub part to add to the MIME message when creating it (see the Section called mailmime - MIME part).

- content is the content type of the part to create (see the Section called mailmime_content - MIME content type (Content-Type)).

- content_type is the content type of the part to create given by a string.

- mime_fields is the list of MIME header fields (see the Section called mailmime_fields - header fields).

Example 4-40. creating a MIME part

    #include <libetpan/libetpan.h>

    #define DATA_STR "my data"

    int main(int argc, char ** argv)
    {
      struct mailmime * mime;
      struct mailmime * single_part;

      mime_fields =
        mailmime_fields_new_encoding(MAILMIME_MECHANISM_QUOTED_PRINTABLE);
      mailmime_new_with_content("plain/text", mime_fields, &single_part);

      mailmime_set_body_text(single_part, DATA_STR, sizeof(DATA_STR) - 1);

      mime = mailmime_new_message_data(single_part);

      /* do the things */

      mailmime_free(mime);
    }

    int main(int argc, char ** argv)
    {
      struct mailmime * mime;
      struct mailmime * single_part;
      struct mailmime_content * content;

      mime_fields =
        mailmime_fields_new_encoding(MAILMIME_MECHANISM_QUOTED_PRINTABLE);
      content = mailmime_get_content_text();
      single_part = mailmime_new_empty(content, mime_fields);

      mailmime_set_body_text(single_part, DATA_STR, sizeof(DATA_STR) - 1);

      mime = mailmime_new_message_data(single_part);

      /* do the things */

      mailmime_free(mime);
    }

    int main(int argc, char ** argv)
    {
      struct mailmime * mime;

      mime = mailmime_multiple_new("multipart/mixed");

      /* do the things */

      mailmime_free(mime);
    }

## mailmime_set_preamble_file, mailmime_set_epilogue_file, mailmime_set_preamble_text and mailmime_set_epilogue_text

    #include <libetpan/libetpan.h>

    int mailmime_set_preamble_file(struct mailmime * build_info,
    			       char * filename);

    int mailmime_set_epilogue_file(struct mailmime * build_info,
    			       char * filename);

    int mailmime_set_preamble_text(struct mailmime * build_info,
    			       char * data_str, size_t length);

    int mailmime_set_epilogue_text(struct mailmime * build_info,
    			       char * data_str, size_t length);

mailmime_set_preamble_file() will define the preamble of a multipart.

mailmime_set_preamble_text() will define the preamble of a multipart.

mailmime_set_epilogue_file() will define the epilogue of a multipart.

mailmime_set_preamble_text() will define the preamble of a multipart.

- build_info is the MIME part to modify (see the Section called mailmime - MIME part).

- data_str is the string to define as epilogue or prologue.

- length is the length of the string to define as epilogue or prologue.

- filename is the name of the file which content will be defined as epilogue or prologue.

Example 4-41. setting preamble and epilogue

    #include <libetpan/libetpan.h>

    #define DATA_STR "test foo bar"

    int main(int argc, char ** argv)
    {
      struct mailmime * mime;

      mime = mailmime_multiple_new("multipart/mixed");

      mailmime_set_preamble_file(mime, strdup("foo-bar.txt"));

      mailmime_set_epilogue_data(mime, DATA_STR, sizeof(DATA_STR) - 1);

      /* do the things */

      mailmime_free(mime);
    }

## mailmime_set_body_file and mailmime_set_body_text

    #include <libetpan/libetpan.h>

    int mailmime_set_body_file(struct mailmime * build_info,
    			   char * filename);

    int mailmime_set_body_text(struct mailmime * build_info,
    			   char * data_str, size_t length);

mailmime_set_body_file() will define the body of a single part.

mailmime_set_body_text() will define the body of a single part.

- build_info is the MIME part to modify (see the Section called mailmime - MIME part).

- data_str is the string to define as the body of the part.

- length is the length of the string to define as the body of the part.

- filename is the name of the file which content will be defined as the body of the part.

Example 4-42. creating a MIME part

    #include <libetpan/libetpan.h>

    #define DATA_STR "my data"

    int main(int argc, char ** argv)
    {
      struct mailmime * mime;

      mime_fields =
        mailmime_fields_new_encoding(MAILMIME_MECHANISM_QUOTED_PRINTABLE);
      mailmime_new_with_content("plain/text", mime_fields, &mime);

      mailmime_set_body_text(mime, DATA_STR, sizeof(DATA_STR) - 1);

      /* do the things */

      mailmime_free(mime);
    }

## mailmime_add_part, mailmime_remove_part, mailmime_smart_add_part and mailmime_smart_remove_part

    #include <libetpan/libetpan.h>

    int mailmime_add_part(struct mailmime * build_info,
    		      struct mailmime * part);

    void mailmime_remove_part(struct mailmime * mime);

    int mailmime_smart_add_part(struct mailmime * mime,
        struct mailmime * mime_sub);

    int mailmime_smart_remove_part(struct mailmime * mime);

mailmime_add_part() will add a sub MIME part.

mailmime_remove_part() will detach the given sub part from its parent.

mailmime_smart_add_part() will add a sub MIME part. If the parent part is a message and no child exist, the part is set as the child. If the parent part is a message and a child already exists, if the child is multipart, the part to add is added as child of this multipart, else a multipart is added and the part is added as child of the multipart.

mailmime_smart_remove_part() will detach the given sub part from its parent. The sub part will be freed.

- build_info is the parent MIME part (see the Section called mailmime - MIME part).

- part is the part to add (see the Section called mailmime - MIME part).

- mime is the parent MIME part (see the Section called mailmime - MIME part).

- mime_sub is the part to add or to detach (see the Section called mailmime - MIME part).

Example 4-43. modifying MIME structure

    #include <libetpan/libetpan.h>

    int main(int argc, char ** argv)
    {
      struct mailmime * sub_mime;
      struct mailmime_fields * mime_fields;
      struct mailmime_content * content;

      content = mailmime_get_content_text();

      mime_fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_BASE64);

      sub_mime = mailmime_new_empty(content, mime_fields);

      mime = mailmime_new_message_data(NULL);

      mailmime_add_part(mime, sub_mime);

      /* do the things */

      mailmime_free(mime);

    int main(int argc, char ** argv)
    {
      struct mailmime * sub_mime;
      struct mailmime * other_sub_mime;
      struct mailmime_fields * mime_fields;
      struct mailmime_content * content;

      content = mailmime_get_content_text();
      mime_fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_BASE64);
      sub_mime = mailmime_new_empty(content, mime_fields);

      content = mailmime_get_content_text();
      mime_fields =
        mailmime_fields_new_encoding(MAILMIME_MECHANISM_QUOTED_PRINTABLE);
      other_sub_mime = mailmime_new_empty(content, mime_fields);

      mime = mailmime_new_message_data(NULL);

      mailmime_smart_add_part(mime, sub_mime);
      mailmime_smart_add_part(mime, other_sub_mime);

      /* do the things */

      mailmime_free(mime);

    int main(int argc, char ** argv)
    {
      struct mailmime * sub_mime;
      struct mailmime_fields * mime_fields;
      struct mailmime_content * content;

      content = mailmime_get_content_text();

      mime_fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_BASE64);

      sub_mime = mailmime_new_empty(content, mime_fields);

      mime = mailmime_new_message_data(NULL);

      mailmime_add_part(mime, sub_mime);

      mailmime_remove_part(sub_mime);

      /* do the things */

      mailmime_free(sub_mime);
      mailmime_free(mime);

    int main(int argc, char ** argv)
    {
      struct mailmime * sub_mime;
      struct mailmime_fields * mime_fields;
      struct mailmime_content * content;

      content = mailmime_get_content_text();

      mime_fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_BASE64);

      sub_mime = mailmime_new_empty(content, mime_fields);

      mime = mailmime_new_message_data(NULL);

      mailmime_add_part(mime, sub_mime);

      mailmime_smart_remove_part(sub_mime);

      /* do the things */

      mailmime_free(mime);
    }

## mailmime_set_imf_fields

    #include <libetpan/libetpan.h>

    void mailmime_set_imf_fields(struct mailmime * build_info,
        struct mailimf_fields * fields);

mailmime_set_imf_fields() will set the fields of the given MIME message.

- build_info is the MIME message to modify (see the Section called mailmime - MIME part).

- fields is the header fields to set for the message (see the Section called mailimf_fields - list of header fields in Chapter 3).

Example 4-44. modifying MIME structure

    #include <libetpan/libetpan.h>

    #define DATA_STR "test foo bar"

    int main(int argc, char ** argv)
    {
      struct mailmime * mime;
      struct mailmime_fields * mime_fields;
      struct mailimf_fields * imf_fields;

      mime_fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_8BIT);

      mailmime_new_with_content("text/plain", mime_fields, &mime);

      mailmime_set_body_text(mime, DATA_STR, sizeof(DATA_STR) - 1);

      /* look at the example in mailimf_fields to see how to
         build a mailimf_fields */
      imf_fields = build_fields();

      mailmime_set_imf_fields(mime, imf_fields);

      /* do the things */

      mailmime_free(mime);
    }

## mailmime_fields_new_encoding and mailmime_fields_new_filename

    #include <libetpan/libetpan.h>

    enum {
      MAILMIME_MECHANISM_ERROR,
      MAILMIME_MECHANISM_7BIT,
      MAILMIME_MECHANISM_8BIT,
      MAILMIME_MECHANISM_BINARY,
      MAILMIME_MECHANISM_QUOTED_PRINTABLE,
      MAILMIME_MECHANISM_BASE64,
      MAILMIME_MECHANISM_TOKEN
    };

    enum {
      MAILMIME_DISPOSITION_TYPE_ERROR,
      MAILMIME_DISPOSITION_TYPE_INLINE,
      MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
      MAILMIME_DISPOSITION_TYPE_EXTENSION
    };

    struct mailmime_fields * mailmime_fields_new_encoding(int encoding_type);

    struct mailmime_fields * mailmime_fields_new_filename(int dsp_type,
        char * filename, int encoding_type);

mailmime_fields_new_encoding() will create a list of MIME header fields with only Content-Transfer-Encoding.

mailmime_fields_new_filename() will create a list of MIME header fields with Content-Transfer-Encoding and Content-Disposition.

The result will be a list of MIME header fields (see the Section called mailmime_fields - header fields).

- encoding_type is the MIME encoding mechanism. The value can be MAILMIME_MECHANISM_7BIT, MAILMIME_MECHANISM_8BIT, MAILMIME_MECHANISM_BINARY, MAILMIME_MECHANISM_QUOTED_PRINTABLE or MAILMIME_MECHANISM_BASE64 (see the Section called mailmime_mechanism - MIME transfer encoding mechanism (Content-Transfer-Encoding)).

- dsp_type is the disposition type. The value can be MAILMIME_DISPOSITION_TYPE_INLINE or MAILMIME_DISPOSITION_TYPE_ATTACHMENT (see the Section called mailmime_disposition_type - Type of MIME disposition).

- filename is the filename for MIME content disposition.

Example 4-45. creating MIME fields with only Content-Transfer-Encoding

    #include <libetpan/libetpan.h>

    int main(void)
    {
      struct mailmime_fields * fields;

      fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_BASE64);

      /* do the things */

      mailmime_fields_free(fields);
    }

    int main(void)
    {
      struct mailmime_fields * fields;

      fields =
      mailmime_fields_new_filename(MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
        strdup("foo-bar.txt"), MAILMIME_MECHANISM_BASE64);

      /* do the things */

      mailmime_fields_free(fields);
    }

Prev | Home | Next
Rendering of MIME parts | Up | Helper functions