mime
The mime namespace offers filters that apply and remove common content transfer encodings, such as Base64 and Quoted-Printable.
It also provides functions to break text into lines and change the end-of-line convention. MIME is described mainly in RFC 2045, 2046, 2047, 2048, and 2049.
All functionality provided by the MIME module follows the ideas presented in LTN012, Filters sources and sinks.
To obtain the mime namespace, run:
-- loads the MIME module and everything it requires
local mime = require("mime")
mime
mime.b64(C, D) | Low-level filter to perform Base64 encoding. |
mime.decode(style) | Returns a filter that decodes data from a given transfer content encoding. |
mime.dot(m, B) | Low-level filter to perform SMTP stuffing and enable transmission of messages containing the sequence "CRLF.CRLF". |
mime.encode(style, mode) | Although both transfer content encodings specify a limit for the line length, the encoding filters do not break text into lines (for added flexibility). |
mime.eol(C, D, marker) | Low-level filter to perform end-of-line marker translation. |
mime.normalize(marker) | Converts most common end-of-line markers to a specific given marker. |
mime.qp(C, D, marker) | Low-level filter to perform Quoted-Printable encoding. |
mime.qpwrp(n, B, length) | Low-level filter to break Quoted-Printable text into lines. |
mime.stuff() | Creates and returns a filter that performs stuffing of SMTP messages. |
mime.unb64(C, D) | Low-level filter to perform Base64 decoding. |
mime.unqp(n, B, length) | Low-level filter to break text into lines with CRLF marker. |
mime.wrap(style, length) | Returns a filter that breaks data into lines. |
mime
mime.b64(C, D)
Note: The simplest use of this function is to encode a string into it's Base64 transfer content encoding. Notice the extra parenthesis around the call to mime.b64, to discard the second return value.
print((mime.b64("diego:password")))
--> ZGllZ286cGFzc3dvcmQ=
C
:
D
:
(optional)
With
A, B = mime.b64(C [, D])
A is the encoded version of the largest prefix of C..D that can be encoded unambiguously. B has the remaining bytes of C..D, before encoding. If D is #nil, A is padded with the encoding of the remaining bytes of C.
mime.decode(style)
style
:
Can be "base64" or "quoted-printable".
mime.dot(m, B)
print((string.gsub(mime.dot(2, ".\r\nStuffing the message.\r\n.\r\n."), "\r\n", "\\n")))
--> ..\nStuffing the message.\n..\n..
Note: The smtp.send function uses this filter automatically. You don't need to apply it again.
m
:
B
:
(optional)
With
A, n = mime.dot(m [, B])
A is the stuffed version of B. 'n' gives the number of characters from the sequence CRLF seen in the end of B. 'm' should tell the same, but for the previous chunk.
mime.encode(style, mode)
Below is a filter that converts binary data to the Base64 transfer content encoding and breaks it into lines of the correct size.
base64 = ltn12.filter.chain(
mime.encode("base64"),
mime.wrap("base64")
)
Note: Text data has to be converted to canonic form before being encoded.
base64 = ltn12.filter.chain(
mime.normalize(),
mime.encode("base64"),
mime.wrap("base64")
)
style
:
Can be "base64" or "quoted-printable".
mode
:
(optional) In the Quoted-Printable case, the user can
specify whether the data is textual or binary, by passing the mode strings
"text" or "binary". Mode defaults to "text".
mime.eol(C, D, marker)
C
:
The ASCII value of the last character of the previous chunk,
if it was a candidate for line break, or 0 otherwise.
D
:
(optional)
marker
:
(optional) Gives the new end-of-line marker and defaults
to CRLF.
With
A, B = mime.eol(C [, D, marker])
A is the translated version of D. C is the ASCII value of the last character of the previous chunk, if it was a candidate for line break, or 0 otherwise. B is the same as C, but for the current chunk. Marker gives the new end-of-line marker and defaults to CRLF.
-- translates the end-of-line marker to UNIX
unix = mime.eol(0, dos, "\n")
mime.normalize(marker)
marker
:
The new marker. It defaults to CRLF, the canonic end-of-line
marker defined by the MIME standard.
The function returns a filter that performs the conversion.
mime.qp(C, D, marker)
Note: The simplest use of this function is to encode a string into it's Quoted-Printable transfer content encoding. Notice the extra parenthesis around the call to #mime.qp, to discard the second return value.
print((mime.qp("maçã")))
--> ma=E7=E3=
C
:
D
:
(optional)
marker
:
With
A, B = mime.qp(C [, D, marker])
A is the encoded version of the largest prefix of C..D that can be encoded unambiguously. B has the remaining bytes of C..D, before encoding. If D is #nil, A is padded with the encoding of the remaining bytes of C. Throughout encoding, occurrences of CRLF are replaced by the marker, which itself defaults to CRLF.
mime.qpwrp(n, B, length)
n
:
B
:
(optional)
length
:
(optional)
With
A, m = mime.qpwrp(n [, B, length])
A is the decoded version of the largest prefix of C..D that can be decoded unambiguously. B has the remaining bytes of C..D, before decoding. If D is #nil, A is the empty string and B returns whatever couldn't be decoded.
mime.stuff()
mime.unb64(C, D)
Note: The simplest use of this function is to decode a string from it's Base64 transfer content encoding. Notice the extra parenthesis around the call to #mime.unqp, to discard the second return value.
print((mime.unb64("ZGllZ286cGFzc3dvcmQ=")))
--> diego:password
C
:
D
:
(optional)
With
A, B = mime.unb64(C [, D])
A is the decoded version of the largest prefix of C..D that can be decoded unambiguously. B has the remaining bytes of C..D, before decoding. If D is #nil, A is the empty string and B returns whatever couldn't be decoded.
mime.unqp(n, B, length)
Note: This function only breaks lines that are bigger than length bytes. The resulting line length does not include the CRLF marker.
n
:
B
:
(optional)
length
:
(optional)
With
A, m = mime.wrp(n [, B, length])
A is a copy of B, broken into lines of at most length bytes (defaults to 76). 'n' should tell how many bytes are left for the first line of B and 'm' returns the number of bytes left in the last line of A.
mime.wrap(style, length)
qp = ltn12.filter.chain(
mime.normalize(),
mime.encode("quoted-printable"),
mime.wrap("quoted-printable")
)
Note: To break into lines with a different end-of-line convention, apply a normalization filter after the line break filter.
style
:
The "text" line-wrap filter simply breaks text into
lines by inserting CRLF end-of-line markers at appropriate positions. The
"base64" line-wrap filter works just like the default "text" line-wrap
filter with default length. The function can also wrap "quoted-printable"
lines, taking care not to break lines in the middle of an escaped character.
In that case, the line length is fixed at 76.
length
:
(optional) Available when style is "text", defaults
76.