Qore Mime Module Reference 1.6
Loading...
Searching...
No Matches
Mime::MultiPartMessage Class Reference

MultiPartMessage class implementation. More...

#include <Mime.qm.dox.h>

Inheritance diagram for Mime::MultiPartMessage:
[legend]

Public Member Methods

 constructor (string mptype, string boundary=MultiPartMessage::getBoundary())
 creates the object
 
string getBoundary ()
 returns the message boundary string used
 
abstract hash< MessageInfogetMsgAndHeaders ()
 returns a hash of the message
 
binary serialize ()
 serializes the message with the Content-Type header first and returns a binary object ready to send over a socket
 
binary serializeBody ()
 serializes the message body only and returns a binary object ready to send over a socket
 
int size ()
 returns the number of parts in the message
 

Static Public Member Methods

static string getBoundary ()
 returns a string embedded with the current timestamp designed to be used as MultiPart boundary string
 
static string getRandomString (int len)
 returns a string of random characters
 
static hash< MessageInfoparseBody (string boundary, data body, bool decode=True)
 returns a hash representing a parsed multipart message body from a boundary string and body arguments
 
static binary serializeHeaders (hash hdr)
 serializes a header hash to a binary object
 

Detailed Description

MultiPartMessage class implementation.

Member Function Documentation

◆ constructor()

Mime::MultiPartMessage::constructor ( string  mptype,
string  boundary = MultiPartMessage::getBoundary() 
)

creates the object

Parameters
mptypethe type of multi-part message: see MultiPartMessage Constants for possible values
boundarythe boundary to use between parts

◆ getBoundary()

string Mime::MultiPartMessage::getBoundary ( )

returns the message boundary string used

Returns
the message boundary string used
Since
Mime 1.4

◆ getMsgAndHeaders()

abstract hash< MessageInfo > Mime::MultiPartMessage::getMsgAndHeaders ( )

returns a hash of the message

Returns
a hash of the message with the following keys:
  • hdr: a hash of header information
  • body: a binary object of serialized message parts

◆ getRandomString()

static string Mime::MultiPartMessage::getRandomString ( int  len)
static

returns a string of random characters

Parameters
lenthe length of the string to return
Since
Mime 1.3.4 implemented by Util::get_random_string()

◆ parseBody()

static hash< MessageInfo > Mime::MultiPartMessage::parseBody ( string  boundary,
data  body,
bool  decode = True 
)
static

returns a hash representing a parsed multipart message body from a boundary string and body arguments

Also parses long headers as per RFC 2822 section 2.2.3

Example:
For example, given the following code:
string body = "This is a message with multiple parts in MIME format.\r
--frontier\r
Content-Type: text/plain\r
\r
This is the body of the message.\r
--frontier\r
Content-Type: text/html\r
Content-Transfer-Encoding: base64\r
\r
PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg\rMultiPartFormDataMessage
Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg=\r
--frontier--\r
";
printf("%N\n", MultiPartMessage::parseBody("frontier", body, True));

The above code will output the following:
hash: (2 members)
  body : "This is a message with multiple parts in MIME format."
  part : list: (2 elements)
    [0]=hash: (2 members)
      hdr : hash: (1 member)
        content-type : "text/plain"
      body : "This is the body of the message."
    [1]=hash: (2 members)
      hdr : hash: (2 members)
        content-type : "text/html"
        content-transfer-encoding : "base64"
      body : "<html>
  <head>
  </head>
  <body>
    <p>This is the body of the message.</p>
  </body>
</html>
"
Parameters
boundarythe boundary string
bodythe message body to parse; parsing only works if the line termination characters are "\r\n"
decodedecode the parts (in this case a part may itself have parts recursively)
Returns
a hash representing a parsed multipart message body with the following keys:
  • body: any non-part message body left after parsing the parts; will be an empty string if the entiore body is made up of the parts
  • part: a list of the parts; each list element is a hash representing a part of the message with the following keys:
    • hdr: a hash of part headers, where each header value is converted to lower case and used as a hash key
    • body: the body of the part as a string; this can only be a binary object if the decode argument is True, the part's content-encoding is base64 and the part's content type does not contain the word "text"
    • [part]: if the decode parameter is True then any content-encoding for parts is decoded automatically; also parts that have parts themselves are processed recursively if this argument is set
Exceptions
MULTIPART-PARSEBODY-ERRORno boundary found in body; no closing boundary; malformatted part or part header, etc
Note
multipart message parsing only works if the line termination characters are "\r\n"; make sure and convert the end of line characters before passing to this method if necessary

◆ serializeBody()

binary Mime::MultiPartMessage::serializeBody ( )

serializes the message body only and returns a binary object ready to send over a socket

Since
Mime 1.4

◆ serializeHeaders()

static binary Mime::MultiPartMessage::serializeHeaders ( hash  hdr)
static

serializes a header hash to a binary object

Parameters
hdrthe hash to serialize
Returns
the binary object representing the hash with each header separated by "\r\n"

◆ size()

int Mime::MultiPartMessage::size ( )

returns the number of parts in the message

Returns
the number of parts in the message
Since
Mime 1.4