| ||||||
This module provides access to the e-mail accounts configured in the device. The main functions it offers are:
e-mail messages are identified by numbers. These numbers are used to retrieve and load message contents, and to delete messages.
Before starting to work with a mailbox, it must be selected via mail.index. The subsequent calls to functions in this module will then operate on this mailbox.
The following example scans the first configured mailbox for all unread messages with subject "NEW IMAGES", stores all attached files in C:\data\images, and marks the message as read.
|
const PATH="c:\\data\\images\\" // a function to copy a stream into a file function copyfile(i, path) o=io.create(path); buf=io.read(i, 256); while buf#null do io.write(o, buf); buf=io.read(i, 256) end; io.close(o) end // select the first mailbox mail.index(0); // synchronize it to get new messages mail.sync(); // scan for unread messages with proper subject for id in mail.inbox() do m=mail.get(id); if m["unread"] and m["subject"]="NEW IMAGES" then // download the message body if required if m["body"]=null then m=mail.load(id) end; att=m["attached"]; if att#null then for i=0 to len(att)-1 do if isstr(att[i][0]) then // file? copyfile(mail.open(id, i), .PATH+att[i][0]) end end end; // mark the message as read m["unread"]=false; mail.set(id, m) end end; // stop synchronizing mail.conn(false) |
Returns the available mailboxes as an array. For each box, the following entries are provided:
| Key | Meaning | Type |
| name | The mailbox name | String |
| type | The mailbox type (pop or imap) | String |
| server | The server host and port | String |
| user | The login user name | String |
|
for box in mail.boxes() do print box end → [private,pop,mail.private.ch:110,lknecht]
[airbit,imap,mail.airbit.ch:143,Lucky Luke] [m-shell,imap,mail.airbit.ch:143,m Shell] |
Without arguments, returns the current connection state (true if a connection to the remote server mailbox exists, false otherwise).
With one argument, returns the current connection state, and sets the new connection state to state, i.e. connects if state=true and disconnects if state=false.
With IMAP4 mailboxes, connecting also starts mailbox synchronization, as with mail.sync.
Throws ErrNotReady if no mailbox has been selected.
|
// disconnect if a mailbox is selected if mail.index()>=0 then mail.conn(false) end |
Deletes the e-mail message with number msgnum from the inbox. msgnum must have been obtained via mail.inbox.
In general, the actual behaviour depends on the connection status:
Throws ErrNotFound if the message does not exist. Throws ErrNotReady if no mailbox has been selected.
|
// delete all e-mails marked with ``SPAM:'' for id in mail.inbox() do m=mail.get(id); if index(m["subject"], "SPAM:")=0 then mail.delete(id) end end |
Returns the sender address of the current mailbox.
Throws ErrNotReady if no mailbox has been selected.
|
print mail.from() → info@m-shell.net
|
Gets the e-mail message with number msgnum from the inbox. The array returned has the following fields:
| Key | Meaning | Type |
| from | The sender of the message | String |
| to | Recipients (only if message has been downloaded) | Array |
| cc | Carbon copy recipients (only if message has been downloaded) | Array |
| bcc | Blind carbon copy recipients (only if message has been downloaded) | Array |
| subject | Message subject | String |
| time | The time stamp of the message, as seconds since the start of year 0. See also module time. | Number |
| unread | true if the message is still unread, false if it has been seen. | Boolean |
| deleted | true if the message is marked as deleted, false if it is not marked. | Boolean |
| body | Message body text (only if message has been downloaded) | String |
| attached | Attachments (only if message has been downloaded and has attachments) | Array |
Each entry in attached has two fields:
| Index | Meaning | Type |
| 0 | File name (if attached file) or message number (if attached message) | String|Number |
| 1 | MIME type | String |
Throws ErrNotFound if the message does not exist. Throws ErrNotReady if no mailbox has been selected.
|
// print sender and subject of all messages // in the inbox sent within the last 24 hours ago=time.get()-24*3600; for id in mail.inbox() do m=mail.get(id); if m["time"]>=ago then print m["from"],m["subject"] end end |
See also: mail.load.
Returns the numbers of all messages in the inbox of the currently selected mailbox.
Throws ErrNotReady if no mailbox has been selected.
|
print mail.inbox() → [1054329,1054328,1054327,1054326,1054325,
1054324,...<111>] |
Without arguments, returns the index of the currently selected mailbox in the array returned by mail.boxes. Returns -1 if no mailbox is selected.
With one argument, returns the index of the currently selected mailbox and selects the mailbox with index boxIndex. All subequent operations will occur on this mailbox.
Throws ExcIndexOutOfRange if the selected mailbox does not exist.
|
i=mail.index(); if i>=0 then print mail.boxes()[i]["name"],"selected" else print "No mailbox selected" end → m-shell selected
|
Connects to the server if not already connected, then downloads the complete message with number msgnum, and returns it. Does not download the message if it is greater than maxSize bytes. If timeout>=0 and timeout milliseconds have passed without loading, null is returned.
The array returned has the same fields as if obtained via mail.get.
Throws ErrNotFound if the message does not exist. Throws ErrNotReady if no mailbox has been selected.
|
id=1054329 m=mail.get(id); // download if it is not complete yet if m["body"]=null then m=mail.load(id) end print m["body"] → Hi mshell team
I am a great fan of m-shell, but... |
Opens the attachment with index index, and returns a stream object to read its data from. index is the index into msg.get(msgnum)["attached"].
Throws ErrNotFound if the message does not exist. Throws ErrNotReady if no mailbox has been selected. Throws ExcIndexOutOfRange if the attachment does not exist.
The returned stream can be accessed with most functions from module io:
Sends an email message to the recipient(s) in recpts, with subject subject and body text body, attaching the files in files. If replyto#null, the Reply-To: field will be set accordingly.
The recipients in recpts can be specified in several ways:
| Key | Meaning | Type |
| to | To: address or addresses. | String|Array |
| cc | Cc: address or addresses. | String|Array |
| bcc | Bcc: address or addresses. | String|Array |
|
// send a silly message to info@m-shell.net mail.send("info@m-shell.net", "I use m-shell", "Hi!\n\nThis message was sent via " + "the m mail module.") // send a foto "foto.jpg" to two friends, cc to yourself mail.send(["to":[friend1@gmail.com, friend2@gmail.com], "cc":mail.from()], "My latest pic", "Hi friends!\n\nIsn't it great?", ["foto.jpg"]) |
Updates the e-mail message with number msgnum with the fields from message. Only the unread field can be modified, all other fields remain unchanged.
|
// mark all message in the inbox as unread for id in mail.inbox() do mail.set(id, ["unread":true]) end |
| ||||||
Connects to the remote e-mail server if not already connected, starts synchronizing the remote inbox with the local inbox in the device, and returns whether new messages were received since the start of the script or since the last call to mail.sync. If all=true, all message headers of a POP3 mailbox are downloaded, not only the new ones (for IMAP4 mailboxes, all is ignored). If timeout>=0 and timeout milliseconds have passed without connecting and downloading, null is returned.
The actual functions performed depend on the mailbox protocol (POP3 or IMAP4) and the configuration. Normal operations are:
Throws ErrNotReady if no mailbox has been selected.
|
// Wait for new messages, checking every minute // POP3 mailboxes require workaround for KIS001156 ispop=mail.boxes()[mail.index()]["type"]="pop"; while not mail.sync() do if ispop then mail.conn(false) end; // disconnect sleep(60000) end |