libcmime  
A lightweight C mime library
Data Structures | Macros | Enumerations | Functions
cmime_message.h File Reference

Defines functions and structs for message handling. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <time.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/param.h>
#include <errno.h>
#include "cmime_address.h"
#include "cmime_list.h"
#include "cmime_header.h"
#include "cmime_part.h"
#include "cmime_util.h"
#include "cmime_string.h"
Include dependency graph for cmime_message.h:

Go to the source code of this file.

Data Structures

struct  CMimeMessage_T
 Represents an email message. More...
 

Macros

#define cmime_message_get_sender(message)   (message->sender)
 
#define cmime_message_get_headers(message)   (message->headers)
 
#define cmime_message_get_recipients(message)   (message->recipients)
 
#define cmime_message_get_boundary(message)   (message->boundary)
 
#define cmime_message_part_count(message)   (message->parts->size)
 Return number of mime parts. More...
 
#define cmime_message_part_first(message)   ((CMimePart_T *)cmime_list_head(message->parts)->data)
 
#define cmime_message_part_last(message)   ((CMimePart_T *)cmime_list_tail(message->parts)->data)
 

Enumerations

enum  CMimeBoundaryType_T { CMIME_BOUNDARY_OPEN, CMIME_BOUNDARY_CLOSE }
 Possible types of boundaries. More...
 
enum  CMimeMultipartType_T {
  CMIME_MULTIPART_MIXED, CMIME_MULTIPART_DIGEST, CMIME_MULTIPART_MESSAGE, CMIME_MULTIPART_ALTERNATIVE,
  CMIME_MULTIPART_RELATED
}
 Possible multipart mime subtypes. More...
 

Functions

CMimeMessage_Tcmime_message_new (void)
 Creates a new CMimeMessage_T object. More...
 
void cmime_message_free (CMimeMessage_T *message)
 Free a CMimeMessage_T object. More...
 
void cmime_message_set_sender (CMimeMessage_T *message, const char *sender)
 Add sender to CMimeMessage_T object. If sender already exists, it will be overwritten. More...
 
void cmime_message_set_sender_encode (CMimeMessage_T *message, const char *sender)
 Add sender to CMimeMessage_T object and convert to 7 bit mime encoded string based on RFC 2047. If sender already exists, it will be overwritten. More...
 
char * cmime_message_get_sender_string (CMimeMessage_T *message)
 Get sender of a CMimeMessage_T object as string. More...
 
void cmime_message_set_message_id (CMimeMessage_T *message, const char *mid)
 Set message id to CMimeMessage_T object, if alreay exists, value will be overwritten. More...
 
char * cmime_message_get_message_id (CMimeMessage_T *message)
 Get MessageID of message object. More...
 
char * cmime_message_generate_message_id (void)
 Generate a message id. More...
 
void cmime_message_add_generated_message_id (CMimeMessage_T *message)
 Add a newly generated message id to a CMimeMessage_T object. More...
 
int cmime_message_set_header (CMimeMessage_T *message, const char *header)
 Set a header to message object. If header already exists, it will be overwritten. More...
 
CMimeHeader_Tcmime_message_get_header (CMimeMessage_T *message, const char *header)
 Get header for given key. More...
 
int cmime_message_add_recipient (CMimeMessage_T *message, const char *recipient, CMimeAddressType_T t)
 Add recipient to message. More...
 
int cmime_message_add_recipient_encode (CMimeMessage_T *message, const char *recipient, CMimeAddressType_T t)
 Add recipient to message and convert to 7 bit mime encoded string based on RFC 2047. More...
 
int cmime_message_add_recipient_to (CMimeMessage_T *message, const char *recipient)
 Add To recipient to message. More...
 
int cmime_message_add_recipient_cc (CMimeMessage_T *message, const char *recipient)
 Add Cc recipient to message. More...
 
int cmime_message_add_recipient_bcc (CMimeMessage_T *message, const char *recipient)
 Add Bcc recipient to message. More...
 
void cmime_message_set_content_type (CMimeMessage_T *message, const char *s)
 Set Content-Type header. More...
 
char * cmime_message_get_content_type (CMimeMessage_T *message)
 Get Content-Type header value. More...
 
void cmime_message_set_content_transfer_encoding (CMimeMessage_T *message, const char *s)
 set Content-Transfer-Encoding value More...
 
char * cmime_message_get_content_transfer_encoding (CMimeMessage_T *message)
 Get Content-Transfer-Encoding value. More...
 
void cmime_message_set_content_id (CMimeMessage_T *message, const char *s)
 Set Content-ID header value. More...
 
char * cmime_message_get_content_id (CMimeMessage_T *message)
 Get Content-ID header value. More...
 
void cmime_message_set_content_description (CMimeMessage_T *message, const char *s)
 Set Content-Description value. More...
 
char * cmime_message_get_content_description (CMimeMessage_T *message)
 Get Content-Description header value. More...
 
void cmime_message_set_mime_version (CMimeMessage_T *message, const char *s)
 Set Mime-Version header value. According to RFC 2045, Mime-Version header is required at the top level of a message. Not required for each body part of a multipart entity. It's required for the embedded headers of a body of type "message/rfc822" or "message/partial" if the embedded message is itself claimed to be MIME. More...
 
char * cmime_message_get_mime_version (CMimeMessage_T *message)
 Get Mime-Version header value. More...
 
void cmime_message_set_date (CMimeMessage_T *message, const char *s)
 Set date string header. More...
 
char * cmime_message_get_date (CMimeMessage_T *message)
 Get date string from CMimeMessage_T object. More...
 
int cmime_message_set_date_now (CMimeMessage_T *message)
 Get current time and set date header. More...
 
void cmime_message_set_boundary (CMimeMessage_T *message, const char *boundary)
 Set message boundary. More...
 
char * cmime_message_generate_boundary (void)
 Generate a message boundary. More...
 
void cmime_message_add_generated_boundary (CMimeMessage_T *message)
 Add a newly generated boundary to a CMimeMessage_T object. More...
 
int cmime_message_from_file (CMimeMessage_T **message, const char *filename, int header_only)
 Parse given file and create a CMimeMessage_T object. More...
 
int cmime_message_to_file (CMimeMessage_T *message, const char *filename)
 Write CMimeMessage_T object to file. More...
 
char * cmime_message_to_string (CMimeMessage_T *message)
 Return a message object as string. More...
 
int cmime_message_from_string (CMimeMessage_T **message, const char *content, int header_only)
 Parse given string and create a new CMimeMessage_T object. More...
 
void cmime_message_set_subject (CMimeMessage_T *message, const char *s)
 Set message subject. More...
 
void cmime_message_set_subject_encode (CMimeMessage_T *message, const char *s)
 Set message subject and convert to 7 bit mime encoded string based on RFC 2047. More...
 
char * cmime_message_get_subject (CMimeMessage_T *message)
 Get the subject string from CMimeMessage_T object. More...
 
void cmime_message_prepend_subject (CMimeMessage_T *message, const char *s)
 prepend string to subject More...
 
void cmime_message_append_subject (CMimeMessage_T *message, const char *s)
 append string to subject More...
 
int cmime_message_set_body (CMimeMessage_T *message, const char *content)
 Set plain body to non multipart message. More...
 
int cmime_message_append_part (CMimeMessage_T *message, CMimePart_T *part)
 Append mime part to message object and generate boundary if necessary. More...
 
void cmime_message_add_attachment (CMimeMessage_T *message, char *attachment)
 add attachment to message More...
 
CMimeMessage_Tcmime_message_create_skeleton (const char *sender, const char *recipient, const char *subject)
 create message skeleton with basic header information More...
 
int cmime_message_part_remove (CMimeMessage_T *message, CMimePart_T *part)
 Remove a mime part from message. More...
 
int cmime_message_add_child_part (CMimeMessage_T *message, CMimePart_T *part, CMimePart_T *child, CMimeMultipartType_T subtype)
 Add a child part to given mimepart, set content type and generate a boundary if necessary. More...
 

Detailed Description

Defines functions and structs for message handling.

Macro Definition Documentation

#define cmime_message_get_boundary (   message)    (message->boundary)
Returns
message boundary
#define cmime_message_get_headers (   message)    (message->headers)
Returns
CMimeList_T with headers
#define cmime_message_get_recipients (   message)    (message->recipients)
Returns
CMimeList_T with recipients
#define cmime_message_get_sender (   message)    (message->sender)
Returns
sender of CMimeMessage_T object
cmime_message_part_count (   message)    (message->parts->size)

Return number of mime parts.

Parameters
messagea CMimeMessage_T obejct
Returns
number of mime parts
Examples:
parse_mime_message.c.
#define cmime_message_part_first (   message)    ((CMimePart_T *)cmime_list_head(message->parts)->data)
Returns
returns the first mime part of message
#define cmime_message_part_last (   message)    ((CMimePart_T *)cmime_list_tail(message->parts)->data)
Returns
returns the last mime part of message

Enumeration Type Documentation

Possible types of boundaries.

Enumerator
CMIME_BOUNDARY_OPEN 

indicates an opening boundary

CMIME_BOUNDARY_CLOSE 

indicates an closing boundary

Possible multipart mime subtypes.

Enumerator
CMIME_MULTIPART_MIXED 

multipart/mixed

CMIME_MULTIPART_DIGEST 

multipart/digest

CMIME_MULTIPART_MESSAGE 

message/rfc822

CMIME_MULTIPART_ALTERNATIVE 

multipart/alternative

CMIME_MULTIPART_RELATED 

multipart/related

Function Documentation

void cmime_message_add_attachment ( CMimeMessage_T message,
char *  attachment 
)

add attachment to message

Parameters
messagea CMimeMessage_T object
attachmenta file pointer
Examples:
create_mime_message_skeleton_add_attachment.c.
int cmime_message_add_child_part ( CMimeMessage_T message,
CMimePart_T part,
CMimePart_T child,
CMimeMultipartType_T  subtype 
)

Add a child part to given mimepart, set content type and generate a boundary if necessary.

Parameters
messagea CMimeMessage_T object
partthe parent mime part
childthe child mime part, which should be added
subtypethe multipart subtype
Returns
0 on success or -1 in case of error
void cmime_message_add_generated_boundary ( CMimeMessage_T message)

Add a newly generated boundary to a CMimeMessage_T object.

Parameters
messagea CMimeMessage_T object
Examples:
create_mime_message_part_from_file.c.
void cmime_message_add_generated_message_id ( CMimeMessage_T message)

Add a newly generated message id to a CMimeMessage_T object.

Parameters
messagea CMimeMessage_T object
int cmime_message_add_recipient ( CMimeMessage_T message,
const char *  recipient,
CMimeAddressType_T  t 
)

Add recipient to message.

Parameters
messagea CMimeMessage_T object
recipienta recipient string
tCMimeAddressType_T recipient type
Returns
0 on success or -1 in case of error
Examples:
create_mime_message.c, and create_mime_message_part_from_file.c.
int cmime_message_add_recipient_bcc ( CMimeMessage_T message,
const char *  recipient 
)

Add Bcc recipient to message.

Parameters
messagea CMimeMessage_T object
recipienta recipient string
Returns
0 on success or -1 in case of error
int cmime_message_add_recipient_cc ( CMimeMessage_T message,
const char *  recipient 
)

Add Cc recipient to message.

Parameters
messagea CMimeMessage_T object
recipienta recipient string
Returns
0 on success or -1 in case of error
int cmime_message_add_recipient_encode ( CMimeMessage_T message,
const char *  recipient,
CMimeAddressType_T  t 
)

Add recipient to message and convert to 7 bit mime encoded string based on RFC 2047.

Parameters
messagea CMimeMessage_T object
recipienta recipient string
tCMimeAddressType_T recipient type
Returns
0 on success or -1 in case of error
int cmime_message_add_recipient_to ( CMimeMessage_T message,
const char *  recipient 
)

Add To recipient to message.

Parameters
messagea CMimeMessage_T object
recipienta recipient string
Returns
0 on success or -1 in case of error
int cmime_message_append_part ( CMimeMessage_T message,
CMimePart_T part 
)

Append mime part to message object and generate boundary if necessary.

Parameters
messagea CMimeMessage_T object
parta CMimePart_T part
Returns
0 on success, -1 in case of error
Examples:
create_mime_message_part_from_file.c.
void cmime_message_append_subject ( CMimeMessage_T message,
const char *  s 
)

append string to subject

Parameters
messagea CMimeMessage_T object
sa append string for subject
Examples:
append_and_prepend_subject.c.
CMimeMessage_T* cmime_message_create_skeleton ( const char *  sender,
const char *  recipient,
const char *  subject 
)

create message skeleton with basic header information

Parameters
sendera from sender
recipienta to recipient
subjecta subject string
Returns
CMimeMessage_T pointer
Examples:
append_and_prepend_subject.c, create_mime_message_skeleton.c, and create_mime_message_skeleton_add_attachment.c.
void cmime_message_free ( CMimeMessage_T message)
int cmime_message_from_file ( CMimeMessage_T **  message,
const char *  filename,
int  header_only 
)

Parse given file and create a CMimeMessage_T object.

Parameters
messageout param to return the new message object
filenamepath to message file
header_onlyparse only message headers, 1 = tue, 0 = false
Returns
0 on success, -1 on stat error, -2 if not a regular file, -3 if reading fails, 1 if parsing failed because of invalid input, 2 if parsing failed due to memory exhaustion
Examples:
parse_mime_message.c, and save_attachment_to_file.c.
int cmime_message_from_string ( CMimeMessage_T **  message,
const char *  content,
int  header_only 
)

Parse given string and create a new CMimeMessage_T object.

Parameters
messageout param to return the new message object
contentmessage string to parse
header_onlyparse only message headers, 1 = tue, 0 = false
Returns
0 on success or -1 in case of error, 1 if parsing failed because of invalid input, 2 if parsing failed due to memory exhaustion
char* cmime_message_generate_boundary ( void  )

Generate a message boundary.

Returns
a newly allocated boundary
Examples:
create_mime_message.c.
char* cmime_message_generate_message_id ( void  )

Generate a message id.

Returns
a newly allocated message id string
Examples:
create_mime_message_part_from_file.c.
char* cmime_message_get_content_description ( CMimeMessage_T message)

Get Content-Description header value.

Parameters
messagea CMimeMessage_T object
char* cmime_message_get_content_id ( CMimeMessage_T message)

Get Content-ID header value.

Parameters
messagea CMimeMessage_T object
Returns
Content-ID header value
Examples:
parse_mime_message.c.
char* cmime_message_get_content_transfer_encoding ( CMimeMessage_T message)

Get Content-Transfer-Encoding value.

Parameters
messagea CMimeMessage_T object
Returns
Content-Transfer-Encoding header value
char* cmime_message_get_content_type ( CMimeMessage_T message)

Get Content-Type header value.

Parameters
messagea CMimeMessage_T object
Returns
Content-Type header value
Examples:
parse_mime_message.c.
char* cmime_message_get_date ( CMimeMessage_T message)

Get date string from CMimeMessage_T object.

Parameters
messagea CMimeMessage_T object
Returns
date string
CMimeHeader_T* cmime_message_get_header ( CMimeMessage_T message,
const char *  header 
)

Get header for given key.

Parameters
messagea CMimeMessage_T object
headername of header to search for
Returns
a CMImeHeader_T object, or NULL in case of error
char* cmime_message_get_message_id ( CMimeMessage_T message)

Get MessageID of message object.

Parameters
messagea CMimeMessage_T object
Returns
message id of CMimeMessage_T object
char* cmime_message_get_mime_version ( CMimeMessage_T message)

Get Mime-Version header value.

Parameters
messagea CMimeMessage_T object
Returns
Mime-Version header value
Examples:
parse_mime_message.c.
char* cmime_message_get_sender_string ( CMimeMessage_T message)

Get sender of a CMimeMessage_T object as string.

Parameters
messagea CMimeMessage_T object
Returns
sender of message as CMimeAddress_T object
Examples:
parse_mime_message.c.
char* cmime_message_get_subject ( CMimeMessage_T message)

Get the subject string from CMimeMessage_T object.

Parameters
messagea CMimeMessage_T object
Returns
subject string to append
CMimeMessage_T* cmime_message_new ( void  )
int cmime_message_part_remove ( CMimeMessage_T message,
CMimePart_T part 
)

Remove a mime part from message.

Parameters
messagea CMimeMessage_T object
partthe mime part which should be removed
Returns
0 on success, -1 in case of error
void cmime_message_prepend_subject ( CMimeMessage_T message,
const char *  s 
)

prepend string to subject

Parameters
messagea CMimeMessage_T object
sstring to prepend
Examples:
append_and_prepend_subject.c.
int cmime_message_set_body ( CMimeMessage_T message,
const char *  content 
)

Set plain body to non multipart message.

Parameters
messagea CMimeMessage_T object
contentbody content
Returns
0 on sucess, -1 if message is multipart
Examples:
append_and_prepend_subject.c, create_mime_message.c, create_mime_message_part_from_file.c, create_mime_message_skeleton.c, and create_mime_message_skeleton_add_attachment.c.
void cmime_message_set_boundary ( CMimeMessage_T message,
const char *  boundary 
)

Set message boundary.

Parameters
messagea CMimeMessage_T object
boundarythe boundary to set
void cmime_message_set_content_description ( CMimeMessage_T message,
const char *  s 
)

Set Content-Description value.

Parameters
messagea CMimeMessage_T object
sContent-Description header value
void cmime_message_set_content_id ( CMimeMessage_T message,
const char *  s 
)

Set Content-ID header value.

Parameters
messagea CMimeMessage_T object
sContent-ID header value
void cmime_message_set_content_transfer_encoding ( CMimeMessage_T message,
const char *  s 
)

set Content-Transfer-Encoding value

Parameters
messagea CMimeMessage_T object
sContent-Transfer-Encoding value
void cmime_message_set_content_type ( CMimeMessage_T message,
const char *  s 
)

Set Content-Type header.

Parameters
messagea CMimeMessage_T object
sContent-Type string
void cmime_message_set_date ( CMimeMessage_T message,
const char *  s 
)

Set date string header.

Parameters
messagea CMimeMessage_T object
sdate string
int cmime_message_set_date_now ( CMimeMessage_T message)

Get current time and set date header.

Parameters
messagea CMimeMessage_T object
Returns
0 on success or -1 in case of error
Examples:
create_mime_message.c, and create_mime_message_part_from_file.c.
int cmime_message_set_header ( CMimeMessage_T message,
const char *  header 
)

Set a header to message object. If header already exists, it will be overwritten.

Parameters
messagea CMimeMessage_T object
headerfull header string
Returns
0 on success or -1 in case of error
void cmime_message_set_message_id ( CMimeMessage_T message,
const char *  mid 
)

Set message id to CMimeMessage_T object, if alreay exists, value will be overwritten.

Parameters
messagea CMimeMessage_T object
midmessage id string
Examples:
create_mime_message.c, and create_mime_message_part_from_file.c.
void cmime_message_set_mime_version ( CMimeMessage_T message,
const char *  s 
)

Set Mime-Version header value. According to RFC 2045, Mime-Version header is required at the top level of a message. Not required for each body part of a multipart entity. It's required for the embedded headers of a body of type "message/rfc822" or "message/partial" if the embedded message is itself claimed to be MIME.

Parameters
messagea CMimeMessage_T object
sMime-Version header value
void cmime_message_set_sender ( CMimeMessage_T message,
const char *  sender 
)

Add sender to CMimeMessage_T object. If sender already exists, it will be overwritten.

Parameters
messagea CMimeMessage_T obect
sendersender string
Examples:
create_mime_message.c, and create_mime_message_part_from_file.c.
void cmime_message_set_sender_encode ( CMimeMessage_T message,
const char *  sender 
)

Add sender to CMimeMessage_T object and convert to 7 bit mime encoded string based on RFC 2047. If sender already exists, it will be overwritten.

Parameters
messagea CMimeMessage_T obect
sendersender string
void cmime_message_set_subject ( CMimeMessage_T message,
const char *  s 
)

Set message subject.

Parameters
messagea CMimeMessage_T object
sthe subject string
Examples:
create_mime_message.c, and create_mime_message_part_from_file.c.
void cmime_message_set_subject_encode ( CMimeMessage_T message,
const char *  s 
)

Set message subject and convert to 7 bit mime encoded string based on RFC 2047.

Parameters
messagea CMimeMessage_T object
sthe subject string
int cmime_message_to_file ( CMimeMessage_T message,
const char *  filename 
)

Write CMimeMessage_T object to file.

Parameters
messagea CMimeMessage_T object
filenamepath to file, which should be written
Returns
the number of items successfully written on success, -1 in case of error
Examples:
append_and_prepend_subject.c, create_mime_message.c, create_mime_message_part_from_file.c, create_mime_message_skeleton.c, and create_mime_message_skeleton_add_attachment.c.
char* cmime_message_to_string ( CMimeMessage_T message)

Return a message object as string.

Parameters
messagea CMimeMessage_T object
Returns
message as newly allocated string
Examples:
append_and_prepend_subject.c, create_mime_message.c, create_mime_message_part_from_file.c, create_mime_message_skeleton.c, create_mime_message_skeleton_add_attachment.c, and parse_mime_message.c.