EmailBundle Lua Reference Documentation
chilkat.EmailBundle
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
EmailBundle
Store multiple
Use the message count and indexed access methods to retrieve individual
emails for inspection, processing, saving, or sending.
Locate a specific email in the collection when application logic needs
to find a matching message.
Sort the bundle by date, sender, recipient, or subject to organize
messages before processing or display.
Remove individual messages by object or index, or clear the entire
bundle when the collection is no longer needed.
Serialize the bundle to XML and reload it later, or load task results
that return an email bundle.
For an extended overview, see
EmailBundle Class Overview.
Hold, search, sort, remove, save, and reload groups of email messages.
EmailBundle is a container for multiple
Email objects. It is commonly returned by mail-related
operations and can also be built manually by adding individual email
messages. The class provides a simple way to work with groups of messages:
retrieve emails by index, search for a matching email, sort by common email
fields, remove messages, clear the bundle, and serialize or reload the
collection as XML.
Email collection
Email objects together after
receiving, loading, filtering, or manually building a message set.
Retrieve messages
Search within the bundle
Sort messages
Remove or clear emails
Save and reload
Email objects, loop from
0 to MessageCount - 1, retrieve each message with
EmailAt, and then inspect, save, sort, remove, or process each
email as needed. Use EmailBundle for the group and
Email for the individual message contents.
Object Creation
local chilkat = require("chilkat")
local email_bundle = chilkat.EmailBundle.new()Creates the object (chilkat.EmailBundle() does the same). Lua's garbage collector frees it when it is no longer referenced; email_bundle:dispose() frees it at once, and in Lua 5.4 and later local email_bundle <close> = chilkat.EmailBundle.new() frees it when the block ends. Calling a method of a disposed object raises an error.
Errors
A method that fails returns nil followed by the error text, as io.open does: local body, err = email_bundle:method(...), where err is the object's last_error_text. Wrap the call in assert() to raise an error with that text instead. Methods that answer a question return true or false, numbers are returned as they are (check last_method_success where the description says a value means failure), and property reads and writes never fail for Chilkat reasons.
Misuse raises a Lua error: a wrong argument type or a missing argument (bad argument #1 to ...), an unknown property name, a write to a read-only property, an out-of-range integer, or an object of the wrong class.
Names are Chilkat's in snake_case: QuickGetStr is quick_get_str, LastErrorText is last_error_text. Properties are fields (email_bundle.verbose_logging = true) and methods use the colon (email_bundle:method(...)). Strings are UTF-8; binary data is a Lua string of bytes. Indexes are 0-based, as in every Chilkat API. Unlocking: call chilkat.unlock_bundle("...") once at startup.
Properties
DebugLogFilePath
local strVal = email_bundle.debug_log_file_path
email_bundle.debug_log_file_path = strVal
If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.
Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.
Possible causes of hangs include:
- A timeout property set to 0, indicating an infinite timeout.
- A hang occurring within an event callback in the application code.
- An internal bug in the Chilkat code causing the hang.
LastErrorHtml
local strVal = email_bundle.last_error_html
Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastErrorText
local strVal = email_bundle.last_error_text
Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
LastErrorXml
local strVal = email_bundle.last_error_xml
Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastMethodSuccess
local boolVal = email_bundle.last_method_success
email_bundle.last_method_success = boolVal
Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.
MessageCount
local intVal = email_bundle.message_count
Returns the number of Email objects currently contained in the bundle.
Valid zero-based indexes range from 0 through MessageCount - 1.
VerboseLogging
local boolVal = email_bundle.verbose_logging
email_bundle.verbose_logging = boolVal
If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
local strVal = email_bundle.version
Methods
AddEmail
-- ok is true, or nil and err on failure
local ok, err = email_bundle:add_email(email)
Adds an independent copy of the Email object in email to the end of this in-memory bundle.
The bundle does not retain a reference to email and does not take ownership of the supplied object. Changes made to email after this method returns do not affect the copy stored in the bundle, and changes to the stored copy do not affect email.
The same Email object may be added more than once. Each call adds another independent copy and increases the MessageCount property by one.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
Clear
Removes and discards all emails from this in-memory bundle.
After this method returns, MessageCount is 0. No messages are deleted from any mail server.
EmailAt
-- email is a chilkat.Email
-- ok is true, or nil and err on failure
local ok, err = email_bundle:email_at(index, email)
Copies the email at the zero-based index in index into the Email object in email.
Valid indexes range from 0 through MessageCount - 1. The first email is at index 0.
email receives an independent copy. Changes made to email do not modify the email stored in the bundle.
Note: This is an in-memory operation and does not communicate with a mail server.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
FindEmail
-- email is a chilkat.Email
-- ok is true, or nil and err on failure
local ok, err = email_bundle:find_email(json, email)
Finds the first email in the bundle whose MIME header field matches the search criteria specified by the JsonObject in json.
At this time, a search may specify only one MIME header field. Use the following JSON format:
{
"header": {
"name": "Subject",
"value": "Dinner on Wednesday?",
"caseInsensitive": true
}
}
header.name specifies the MIME header field name, and header.value specifies the complete field value to match. The field value must match exactly; this is not a substring, wildcard, or regular-expression search. Set header.caseInsensitive to true for a case-insensitive comparison or false for a case-sensitive comparison.
When a match is found, email receives the first matching email.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
GetXml
local str, err = email_bundle:get_xml()
Serializes the entire email bundle to Chilkat XML and returns the XML document as a string.
The XML contains the bundle and its individual Email objects. It can be loaded into another EmailBundle by calling LoadXmlString.
This is Chilkat's XML representation of an email bundle; it is not an RFC 822/MIME message.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
LoadXml
-- ok is true, or nil and err on failure
local ok, err = email_bundle:load_xml(path)
Loads an email bundle from the local filesystem path specified by path. The path may be absolute or relative; a filename alone refers to a file in the current working directory.
The file should contain Chilkat email-bundle XML previously produced by SaveXml or GetXml.
The emails loaded from path are appended to the emails already in the bundle. Existing emails are not removed or replaced.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
LoadXmlString
-- ok is true, or nil and err on failure
local ok, err = email_bundle:load_xml_string(xml_str)
Loads an email bundle from the Chilkat XML string in xml_str.
The XML is typically obtained from GetXml or read from a file created by SaveXml.
The emails loaded from xml_str are appended to the emails already in the bundle. Existing emails are not removed or replaced.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
RemoveEmail
-- ok is true, or nil and err on failure
local ok, err = email_bundle:remove_email(email)
Removes the first email in this in-memory bundle that matches email.
Chilkat first searches for an email having the same UIDL as email. If no email with a matching UIDL is found, it searches for the first email having the same Message-ID header field value. Only the first matching email is removed.
email may be an independent copy obtained from EmailAt.
Note: This method changes only the local bundle. It does not delete a message from a POP3 or IMAP server.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
RemoveEmailByIndex
-- ok is true, or nil and err on failure
local ok, err = email_bundle:remove_email_by_index(index)
Removes the email at the zero-based index in index.
Valid indexes range from 0 through MessageCount - 1.
Note: This method changes only the local bundle. It does not delete a message from a mail server.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SaveXml
-- ok is true, or nil and err on failure
local ok, err = email_bundle:save_xml(path)
Serializes the bundle and each contained Email to Chilkat XML, then writes the XML document to the local filesystem path specified by path. The path may be absolute or relative; a filename alone writes to the current working directory.
Reload the saved bundle by calling LoadXml. Use GetXml when the XML is needed in memory rather than in a file.
The saved file is a Chilkat email-bundle serialization, not an RFC 822/MIME message file.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SortByDate
email_bundle:sort_by_date(ascending)
Sorts the emails in this bundle by each Email object's EmailDate property.
Set ascending to true to sort in ascending order, from oldest to newest. Set ascending to false to sort in descending order, from newest to oldest.
The operation changes the order of the emails in the in-memory collection.
topSortByRecipient
email_bundle:sort_by_recipient(ascending)
Sorts the emails in this bundle by the full value of each email's To header field.
The comparisons are case-sensitive. Set ascending to true for ascending order or false for descending order. The operation changes the order of the emails in the in-memory collection.
SortBySender
email_bundle:sort_by_sender(ascending)
Sorts the emails in this bundle by the full content of each email's From header field.
Set ascending to true for ascending order or false for descending order. The operation changes the order of the emails in the in-memory collection.
SortBySubject
email_bundle:sort_by_subject(ascending)
Sorts the emails in this bundle by subject.
Set ascending to true for ascending order or false for descending order. The operation changes the order of the emails in the in-memory collection.