IMAPTransporter
The IMAPTransporter
class allows you to retrieve messages from a IMAP email server.
IMAP Transporter object
IMAP Transporter objects are instantiated with the IMAP New transporter command. They provide the following properties and functions:
.acceptUnsecureConnection : Boolean True if 4D is allowed to establish an unencrypted connection |
.addFlags( msgIDs : Collection ; keywords : Object ) : Object .addFlags( msgIDs : Text ; keywords : Object ) : Object .addFlags( msgIDs : Longint ; keywords : Object ) : Object adds flags to the msgIDs for the specified keywords |
.append( mailObj : Object ; destinationBox : Text ; options : Object ) : Object appends a mailObj to the destinationBox |
.authenticationMode : Text the authentication mode used to open the session on the mail server |
.checkConnection() : Object checks the connection using information stored in the transporter object |
.checkConnectionDelay : Integer the maximum time (in seconds) allowed prior to checking the connection to the server |
.connectionTimeOut : Integer the maximum wait time (in seconds) allowed to establish a connection to the server |
.copy( msgsIDs : Collection ; destinationBox : Text ) : Object .copy( allMsgs : Integer ; destinationBox : Text ) : Object copies the messages defined by msgsIDs or allMsgs to the destinationBox on the IMAP server |
.createBox( name : Text ) : Object creates a mailbox with the given name |
.delete( msgsIDs : Collection ) : Object .delete( allMsgs : Integer ) : Object sets the "deleted" flag for the messages defined in msgsIDs or allMsgs |
.deleteBox( name : Text ) : Object permanently removes the mailbox with the given name from the IMAP server |
.expunge() : Object removes all messages with the "deleted" flag from the IMAP mail server. |
.getBoxInfo( { name : Text }) : Object returns a boxInfo object corresponding to the current maibox, or the mailbox name |
.getBoxList( { parameters : Object } ) : Collection returns a collection of mailboxes describing all of the available mailboxes |
.getDelimiter() : Text returns the character used to delimit levels of hierarchy in the mailbox name |
.getMail( msgNumber: Integer { ; options : Object } ) : Object .getMail( msgID: Text { ; options : Object } ) : Object returns the Email object corresponding to the msgNumber or msgID in the mailbox designated by the IMAP_transporter |
.getMails( ids : Collection { ; options : Object } ) : Object .getMails( startMsg : Integer ; endMsg : Integer { ; options : Object } ) : Object returns an object containing a collection of Email objects |
.getMIMEAsBlob( msgNumber : Integer { ; updateSeen : Boolean } ) : Blob .getMIMEAsBlob( msgID : Text { ; updateSeen : Boolean } ) : Blob returns a BLOB containing the MIME contents for the message corresponding to the msgNumber or msgID in the mailbox designated by the IMAP_transporter |
.host : Text the name or the IP address of the host server |
.logFile : Text the path of the extended log file defined (if any) for the mail connection |
.move( msgsIDs : Collection ; destinationBox : Text ) : Object .move( allMsgs : Integer ; destinationBox : Text ) : Object moves the messages defined by msgsIDs or allMsgs to the destinationBox on the IMAP server |
.numToID( startMsg : Integer ; endMsg : Integer ) : Collection converts the sequence numbers to IMAP unique IDs for the messages in the sequential range designated by startMsg and endMsg |
.removeFlags( msgIDs : Collection ; keywords : Object ) : Object .removeFlags( msgIDs : Text ; keywords : Object ) : Object .removeFlags( msgIDs : Longint ; keywords : Object ) : Object removes flags from the msgIDs for the specified keywords |
.renameBox( currentName : Text ; newName : Text ) : Object changes the name of a mailbox on the IMAP server |
.port : Integer the port number used for mail transactions |
.searchMails( searchCriteria : Text ) : Collection searches for messages that match the given searchCriteria in the current mailbox |
.selectBox( name : Text { ; state : Integer } ) : Object selects the name mailbox as the current mailbox |
.subscribe( name : Text ) : Object allows adding or removing of the specified mailbox to/from the IMAP server’s set of “subscribed” mailboxes |
.unsubscribe( name : Text ) : Object removes a mailbox from a set of subscribed mailboxes |
.user : Text the user name used for authentication on the mail server |
IMAP New transporter
History
Release | Changes |
---|---|
18 R4 | Added |
IMAP New transporter( server : Object ) : 4D.IMAPTransporter
Parameter | Type | Description | |
---|---|---|---|
server | Object | -> | Mail server information |
Result | 4D.IMAPTransporter | <- | IMAP transporter object |
Description
The IMAP New transporter
command configures a new IMAP connection according to the server parameter and returns a new transporter object. The returned transporter object will then usually be used to receive emails.
In the server parameter, pass an object containing the following properties:
server | Default value (if omitted) |
---|---|
.acceptUnsecureConnection : Boolean True if 4D is allowed to establish an unencrypted connection | False |
.accessTokenOAuth2: Text .accessTokenOAuth2: Object Text string or token object representing OAuth2 authorization credentials. Used only with OAUTH2 authenticationMode . If accessTokenOAuth2 is used but authenticationMode is omitted, the OAuth 2 protocol is used (if allowed by the server). Not returned in IMAP transporter object. | none |
.authenticationMode : Text the authentication mode used to open the session on the mail server | the most secure authentication mode supported by the server is used |
.checkConnectionDelay : Integer the maximum time (in seconds) allowed prior to checking the connection to the server | 300 |
.connectionTimeOut : Integer the maximum wait time (in seconds) allowed to establish a connection to the server | 30 |
.host : Text the name or the IP address of the host server | mandatory |
.logFile : Text the path of the extended log file defined (if any) for the mail connection | none |
.password : Text User password for authentication on the server. Not returned in IMAP transporter object. | none |
.port : Integer the port number used for mail transactions | 993 |
.user : Text the user name used for authentication on the mail server | none |
Warning: Make sure the defined timeout is lower than the server timeout, otherwise the client timeout will be useless.
Result
The function returns an IMAP transporter object. All returned properties are read-only.
The IMAP connection is automatically closed when the transporter object is destroyed.
Example
$server:=New object
$server.host:="imap.gmail.com" //Mandatory
$server.port:=993
$server.user:="4d@gmail.com"
$server.password:="XXXXXXXX"
$server.logFile:="LogTest.txt" //log to save in the Logs folder
var $transporter : 4D.IMAPTransporter
$transporter:=IMAP New transporter($server)
$status:=$transporter.checkConnection()
If(Not($status.success))
ALERT("An error occurred: "+$status.statusText)
End if
4D.IMAPTransporter.new()
4D.IMAPTransporter.new( server : Object ) : 4D.IMAPTransporter
Parameter | Type | Description | |
---|---|---|---|
server | Object | -> | Mail server information |
Result | 4D.IMAPTransporter | <- | IMAP transporter object |
Description
The 4D.IMAPTransporter.new()
function creates and returns a new object of the 4D.IMAPTransporter
type. It is identical to the IMAP New transporter
command (shortcut).
.acceptUnsecureConnection
History
Release | Changes |
---|---|
17 R4 | Added |
.acceptUnsecureConnection : Boolean
Description
The .acceptUnsecureConnection
property contains True if 4D is allowed to establish an unencrypted connection when encrypted connection is not possible.
It contains False if unencrypted connections are unallowed, in which case an error in returned when encrypted connection is not possible.
Available secured ports are:
-
SMTP
- 465: SMTPS
- 587 or 25: SMTP with STARTTLS upgrade if supported by the server.
-
IMAP
- 143: IMAP non-encrypted port
- 993: IMAP with STARTTLS upgrade if supported by the server
-
POP3
- 110: POP3 non-encrypted port
- 995: POP3 with STARTTLS upgrade if supported by the server.
.addFlags()
History
Release | Changes |
---|---|
20 | Supports custom keywords |
18 R6 | Added |
.addFlags( msgIDs : Collection ; keywords : Object ) : Object
.addFlags( msgIDs : Text ; keywords : Object ) : Object
.addFlags( msgIDs : Longint ; keywords : Object ) : Object
Parameter | Type | Description | |
---|---|---|---|
msgIDs | Collection | -> | Collection of strings: Message unique IDs (text) Text: Unique ID of a message Longint (IMAP all): All messages in the selected mailbox |
keywords | Object | -> | Keyword flags to add |
Result | Object | <- | Status of the addFlags operation |
Description
The .addFlags()
function adds flags to the msgIDs
for the specified keywords
.
In the msgIDs
parameter, you can pass either:
- a collection containing the unique IDs of specific messages or
- the unique ID (text) of a single message or
- the following constant (longint) for all messages in the selected mailbox:
Constant | Value | Comment |
---|---|---|
IMAP all | 1 | Select all messages in the selected mailbox |
The keywords
parameter lets you define the flags to add to msgIDs
. You can use the following standard flags as well as custom flags (custom flags support depends on the server implementation):
Property | Type | Description |
---|---|---|
$draft | Boolean | True to add the "draft" flag to the message |
$seen | Boolean | True to add the "seen" flag to the message |
$flagged | Boolean | True to add the "flagged" flag to the message |
$answered | Boolean | True to add the "answered" flag to the message |
$deleted | Boolean | True to add the "deleted" flag to the message |
<custom flag> | Boolean | True to add the custom flag to the message |
The custom flags names must respect this rule: the keyword must be a case-insensitive string excluding control chars and space and can not include any of these characters: ( ) { ] % * " \
- For a keyword to be taken into account it has to be true.
- The interpretation of keyword flags may vary per mail client.
Returned object
The function returns an object describing the IMAP status:
Property | Type | Description | |
---|---|---|---|
success | Boolean | True if the operation is successful, False otherwise | |
statusText | Text | Status message returned by the IMAP server, or last error returned in the 4D error stack | |
errors | Collection | 4D error stack (not returned if a IMAP server response is received) | |
[].errcode | Number | 4D error code | |
[].message | Text | Description of the 4D error | |
[].componentSignature | Text | Signature of the internal component which returned the error |
Example
var $options;$transporter;$boxInfo;$status : Object
$options:=New object
$options.host:="imap.gmail.com"
$options.port:=993
$options.user:="4d@gmail.com"
$options.password:="xxxxx"
// Create transporter
$transporter:=IMAP New transporter($options)
// Select mailbox
$boxInfo:=$transporter.selectBox("INBOX")
// Mark all messages from INBOX as read/seen
$flags:=New object
$flags["$seen"]:=True
$status:=$transporter.addFlags(IMAP all;$flags)
.append()
History
Release | Changes |
---|---|
18 R6 | Added |
.append( mailObj : Object ; destinationBox : Text ; options : Object ) : Object
Parameter | Type | Description | |
---|---|---|---|
mailObj | Object | -> | Email object |
destinationBox | Text | -> | Mailbox to receive Email object |
options | Object | -> | Object containing charset info |
Result | Object | <- | Status of the append operation |
Description
The .append()
function appends a mailObj
to the destinationBox
.
In the mailObj
parameter, pass an Email object. For a comprehensive description of mail properties, see Email object. The .append()
function supports keyword tags in the Email object's keywords
attribute.
The optional destinationBox
parameter lets you pass the name of a mailbox where the mailObj
will be appended. If omitted, the current mailbox is used.
In the optional options
parameter, you can pass an object to define the charset and encoding for specific parts of the email. Available properties:
Property | Type | Description |
---|---|---|
headerCharset | Text | Charset and encoding used for the following parts of the email: subject, attachment filenames, and email name attribute(s). Possible values: See possible charsets table below |
bodyCharset | Text | Charset and encoding used for the html and text body contents of the email. Possible values: See possible charsets table below |
Possible charsets:
Constant | Value | Comment |
---|---|---|
mail mode ISO2022JP | US-ASCII_ISO-2022-JP_UTF8_QP |
|
mail mode ISO88591 | ISO-8859-1 |
|
mail mode UTF8 | US-ASCII_UTF8_QP | headerCharset & bodyCharset: US-ASCII if possible, otherwise UTF-8 & Quoted-printable (default value) |
mail mode UTF8 in base64 | US-ASCII_UTF8_B64 | headerCharset & bodyCharset: US-ASCII if possible, otherwise UTF-8 & base64 |
Returned object
The function returns an object describing the IMAP status:
Property | Type | Description | |
---|---|---|---|
success | Boolean | True if the operation is successful, False otherwise | |
statusText | Text | Status message returned by the IMAP server, or last error returned in the 4D error stack | |
errors | Collection | 4D error stack (not returned if a IMAP server response is received) | |
[].errcode | Number | 4D error code | |
[].message | Text | Description of the 4D error | |
[].componentSignature | Text | Signature of the internal component which returned the error |
Example
To save an email in the Drafts mailbox:
var $settings; $status; $msg; $imap: Object
$settings:=New object("host"; "domain.com"; "user"; "xxxx"; "password"; "xxxx"; "port"; 993)
$imap:=IMAP New transporter($settings)
$msg:=New object
$msg.from:="xxxx@domain.com"
$msg.subject:="Lorem Ipsum"
$msg.textBody:="Lorem ipsum dolor sit amet, consectetur adipiscing elit."
$msg.keywords:=New object
$msg.keywords["$seen"]:=True//flag the message as read
$msg.keywords["$draft"]:=True//flag the message as a draft
$status:=$imap.append($msg; "Drafts")
.authenticationMode
History
Release | Changes |
---|---|
17 R4 | Added |
.authenticationMode : Text
Description
The .authenticationMode
property contains the authentication mode used to open the session on the mail server.
By default, the most secured mode supported by the server is used.
Possible values are:
Value | Constants | Comment |
---|---|---|
CRAM-MD5 | IMAP authentication CRAM MD5 | Authentication using CRAM-MD5 protocol |
LOGIN | IMAP authentication login | Authentication using LOGIN protocol |
OAUTH2 | IMAP authentication OAUTH2 | Authentication using OAuth2 protocol |
PLAIN | IMAP authentication plain | Authentication using PLAIN protocol |
.checkConnection()
History
Release | Changes |
---|---|
17 R4 | Added |
.checkConnection() : Object
Parameter | Type | Description | |
---|---|---|---|
Result | Object | <- | Status of the transporter object connection |
Description
The .checkConnection()
function checks the connection using information stored in the transporter object, recreates the connection if necessary, and returns the status. This function allows you to verify that the values provided by the user are valid and consistent.
Returned object
The function sends a request to the mail server and returns an object describing the mail status. This object can contain the following properties:
Property | Type | Description | |
---|---|---|---|
success | boolean | True if the check is successful, False otherwise | |
status | number | (SMTP only) Status code returned by the mail server (0 in case of an issue unrelated to the mail processing) | |
statusText | text | Status message returned by the mail server, or last error returned in the 4D error stack | |
errors | collection | 4D error stack (not returned if a mail server response is received) | |
[ ].errCode | number | 4D error code | |
[ ].message | text | Description of the 4D error | |
[ ].componentSignature | text | Signature of the internal component which returned the error |
.checkConnectionDelay
History
Release | Changes |
---|---|
18 R4 | Added |
.checkConnectionDelay : Integer
Description
The .checkConnectionDelay
property contains the maximum time (in seconds) allowed prior to checking the connection to the server. If this time is exceeded between two method calls, the connection to the server will be checked. By default, if the property has not been set in the server object, the value is 300.
Warning: Make sure the defined timeout is lower than the server timeout, otherwise the client timeout will be useless.
.connectionTimeOut
History
Release | Changes |
---|---|
17 R5 | Added |
.connectionTimeOut : Integer
Description
The .connectionTimeOut
property contains the maximum wait time (in seconds) allowed to establish a connection to the server. By default, if the property has not been set in the server object (used to create the transporter object with SMTP New transporter
, POP3 New transporter
, or IMAP New transporter
), the value is 30.
.copy()
History
Release | Changes |
---|---|
18 R5 | Added |
.copy( msgsIDs : Collection ; destinationBox : Text ) : Object
.copy( allMsgs : Integer ; destinationBox : Text ) : Object
Parameter | Type | Description | |
---|---|---|---|
msgsIDs | Collection | -> | Collection of message unique IDs (strings) |
allMsgs | Integer | -> | IMAP all : All messages in the selected mailbox |
destinationBox | Text | -> | Mailbox to receive copied messages |
Result | Object | <- | Status of the copy operation |
Description
The .copy()
function copies the messages defined by msgsIDs or allMsgs to the destinationBox on the IMAP server.
You can pass:
- in the msgsIDs parameter, a collection containing the unique IDs of the specific messages to copy, or
- in the allMsgs parameter, the
IMAP all
constant (integer) to copy all messages in the selected mailbox.
The destinationBox parameter allows you to pass a text value with the name of the mailbox where the copies of messages will be placed.
Returned object
The function returns an object describing the IMAP status:
Property | Type | Description | |
---|---|---|---|
success | Boolean | True if the operation is successful, False otherwise | |
statusText | Text | Status message returned by the IMAP server, or last error returned in the 4D error stack | |
errors | Collection | 4D error stack (not returned if a IMAP server response is received) | |
[].errcode | Number | 4D error code | |
[].message | Text | Description of the 4D error | |
[].componentSignature | Text | Signature of the internal component which returned the error |
Example 1
To copy a selection of messages:
var $server;$boxInfo;$status : Object
var $mailIds : Collection
var $transporter : 4D.IMAPTransporter
$server:=New object
$server.host:="imap.gmail.com" //Mandatory
$server.port:=993
$server.user:="4d@gmail.com"
$server.password:="XXXXXXXX"
$transporter:=IMAP New transporter($server)
//select mailbox
$boxInfo:=$transporter.selectBox("inbox")
//get collection of message unique IDs
$mailIds:=$transporter.searchMails("subject \"4D new feature:\"")
// copy found messages to the "documents" mailbox
$status:=$transporter.copy($mailIds;"documents")
Example 2
To copy all messages in the current mailbox:
var $server;$boxInfo;$status : Object
var $transporter : 4D.IMAPTransporter
$server:=New object
$server.host:="imap.gmail.com" //Mandatory
$server.port:=993
$server.user:="4d@gmail.com"
$server.password:="XXXXXXXX"
$transporter:=IMAP New transporter($server)
//select mailbox
$boxInfo:=$transporter.selectBox("inbox")
// copy all messages to the "documents" mailbox
$status:=$transporter.copy(IMAP all;"documents")
.createBox()
History
Release | Changes |
---|---|
19 | Added |
.createBox( name : Text ) : Object
Parameter | Type | Description | |
---|---|---|---|
name | Text | -> | Name of the new mailbox |
Result | Object | <- | Status of the mailbox creation operation |
Description
The .createBox()
function creates a mailbox with the given name
. If the IMAP server’s hierarchy separator character appears elsewhere in the mailbox name, the IMAP server will create any parent names needed to create the given mailbox.
In other words, an attempt to create "Projects/IMAP/Doc" on a server in which "/" is the hierarchy separator character will create:
- Only the "Doc" mailbox if "Projects" & "IMAP" already exist.
- "IMAP" & "Doc" mailboxes if only “Projects” already exists.
- "Projects" & “IMAP” & "Doc" mailboxes, if they do not already exist.
In the name
parameter, pass the name of the new mailbox.
Returned object
The function returns an object describing the IMAP status:
Property | Type | Description | |
---|---|---|---|
success | Boolean | True if the operation is successful, False otherwise | |
statusText | Text | Status message returned by the IMAP server, or last error returned in the 4D error stack | |
errors | Collection | 4D error stack (not returned if a IMAP server response is received) | |
[].errcode | Number | 4D error code | |
[].message | Text | Description of the 4D error | |
[].componentSignature | Text | Signature of the internal component which returned the error |
Example
To create a new “Invoices” mailbox:
var $pw : text
var $options; $transporter; $status : object
$options:=New object
$pw:=Request("Please enter your password:")
If(OK=1)
$options.host:="imap.gmail.com"
$options.user:="test@gmail.com"
$options.password:=$pw
$transporter:=IMAP New transporter($options)
$status:=$transporter.createBox("Invoices")
If ($status.success)
ALERT("Mailbox creation successful!")
Else
ALERT("Error: "+$status.statusText)
End if
End if
.delete()
History
Release | Changes |
---|---|
18 R5 | Added |
.delete( msgsIDs : Collection ) : Object
.delete( allMsgs : Integer ) : Object
Parameter | Type | Description | |
---|---|---|---|
msgsIDs | Collection | -> | Collection of message unique IDs (strings) |
allMsgs | Integer | -> | IMAP all : All messages in the selected mailbox |
Result | Object | <- | Status of the delete operation |
Description
The .delete()
function sets the "deleted" flag for the messages defined in msgsIDs
or allMsgs
.
You can pass:
- in the
msgsIDs
parameter, a collection containing the unique IDs of the specific messages to delete, or - in the
allMsgs
parameter, theIMAP all
constant (integer) to delete all messages in the selected mailbox.
Executing this function does not actually remove messages. Messages with the "delete" flag can still be found by the .searchMails() function. Flagged messages are deleted from the IMAP server with the .expunge()
function or by selecting another mailbox or when the transporter object (created with IMAP New transporter) is destroyed.
Returned object
The function returns an object describing the IMAP status:
Property | Type | Description | |
---|---|---|---|
success | Boolean | True if the operation is successful, False otherwise | |
statusText | Text | Status message returned by the IMAP server, or last error returned in the 4D error stack | |
errors | Collection | 4D error stack (not returned if a IMAP server response is received) | |
[].errcode | Number | 4D error code | |
[].message | Text | Description of the 4D error | |
[].componentSignature | Text | Signature of the internal component which returned the error |
Example 1
To delete a selection of messages:
var $server;$boxInfo;$status : Object
var $mailIds : Collection
var $transporter : 4D.IMAPTransporter
$server:=New object
$server.host:="imap.gmail.com" //Mandatory
$server.port:=993
$server.user:="4d@gmail.com"
$server.password:="XXXXXXXX"
$transporter:=IMAP New transporter($server)
//select mailbox
$boxInfo:=$transporter.selectBox("Inbox")
//get collection of message unique IDs
$mailIds:=$transporter.searchMails("subject \"Reports\"")
// Delete selected messages
$status:=$transporter.delete($mailIds)
Example 2
To delete all messages in the current mailbox:
var $server;$boxInfo;$status : Object
var $transporter : 4D.IMAPTransporter
$server:=New object
$server.host:="imap.gmail.com" //Mandatory
$server.port:=993
$server.user:="4d@gmail.com"
$server.password:="XXXXXXXX"
$transporter:=IMAP New transporter($server)
//select mailbox
$boxInfo:=$transporter.selectBox("Junk Email")
// delete all messages in the current mailbox
$status:=$transporter.delete(IMAP all)
.deleteBox()
History
Release | Changes |
---|---|
19 | Added |
.deleteBox( name : Text ) : Object
Parameter | Type | Description | |
---|---|---|---|
name | Text | -> | Name of the mailbox to delete |
Result | Object | <- | Status of the mailbox deletion operation |
Description
The .deleteBox()
function permanently removes the mailbox with the given name
from the IMAP server. Attempting to delete an INBOX or a mailbox that does not exist will generate an error.
In the name
parameter, pass the name of the mailbox to delete.
- The function cannot delete a mailbox that has child mailboxes if the parent mailbox has the "\Noselect" attribute.
- All messages in the deleted mailbox will also be deleted.
- The ability to delete a mailbox depends on the mail server.
Returned object
The function returns an object describing the IMAP status:
Property | Type | Description | |
---|---|---|---|
success | Boolean | True if the operation is successful, False otherwise | |
statusText | Text | Status message returned by the IMAP server, or last error returned in the 4D error stack | |
errors | Collection | 4D error stack (not returned if a IMAP server response is received) | |
[].errcode | Number | 4D error code | |
[].message | Text | Description of the 4D error | |
[].componentSignature | Text | Signature of the internal component which returned the error |
Example
To delete the "Nova Orion Industries" child mailbox from the "Bills" mailbox hierarchy:
var $pw; $name : text
var $options; $transporter; $status : object
$options:=New object
$pw:=Request("Please enter your password:")
If(OK=1) $options.host:="imap.gmail.com"
$options.user:="test@gmail.com"
$options.password:=$pw
$transporter:=IMAP New transporter($options)
// delete mailbox
$name:="Bills"+$transporter.getDelimiter()+"Nova Orion Industries"
$status:=$transporter.deleteBox($name)
If ($status.success)
ALERT("Mailbox deletion successful!")
Else
ALERT("Error: "+$status.statusText)
End if
End if
.expunge()
History
Release | Changes |
---|---|
18 R6 | Added |
.expunge() : Object
Parameter | Type | Description | |
---|---|---|---|
Result | Object | <- | Status of the expunge operation |
Description
The .expunge()
function removes all messages with the "deleted" flag from the IMAP mail server. The "deleted" flag can be set with the .delete()
or .addFlags()
methods.
Returned object
The function returns an object describing the IMAP status:
Property | Type | Description | |
---|---|---|---|
success | Boolean | True if the operation is successful, False otherwise | |
statusText | Text | Status message returned by the IMAP server, or last error returned in the 4D error stack | |
errors | Collection | 4D error stack (not returned if a IMAP server response is received) | |
[].errcode | Number | 4D error code | |
[].message | Text | Description of the 4D error | |
[].componentSignature | Text | Signature of the internal component which returned the error |
Example
var $options;$transporter;$boxInfo;$status : Object
var $ids : Collection
$options:=New object
$options.host:="imap.gmail.com"
$options.port:=993
$options.user:="4d@gmail.com"
$options.password:="xxxxx"
// Create transporter
$transporter:=IMAP New transporter($options)
// Select mailbox
$boxInfo:=$transporter.selectBox("INBOX")
// Find and delete all seen messages in INBOX
$ids:=$transporter.searchMails("SEEN")
$status:=$transporter.delete($ids)
// Purge all messages flagged as deleted
$status:=$transporter.expunge()
.getBoxInfo()
History
Release | Changes |
---|---|
20 | id is returned |
18 R5 | name is optional |
18 R4 | Added |
.getBoxInfo( { name : Text }) : Object
Parameter | Type | Description | |
---|---|---|---|
name | Text | -> | Name of the mailbox |
Result | Object | <- | boxInfo object |
Description
The .getBoxInfo()
function returns a boxInfo
object corresponding to the current maibox, or the mailbox name. This function returns the same information as .selectBox()
without changing the current mailbox.
In the optional name parameter, pass the name of the mailbox to access. The name represents an unambiguous left-to-right hierarchy with levels separated by a specific delimiter character. The delimiter can be found with the .getDelimiter()
function.
If the mailbox name is not selectable or does not exist, the function generates an error and returns null.
Returned object
The boxInfo
object returned contains the following properties:
Property | Type | Description |
---|---|---|
name | text | Name of the mailbox |
mailCount | number | Number of messages in the mailbox |
mailRecent | number | Number of messages with the "recent" flag (indicating new messages) |
id | text | Unique id of the mailbox |
Example
var $transporter : 4D.IMAPTransporter
$transporter:=IMAP New transporter($server)
$info:=$transporter.getBoxInfo("INBOX")
ALERT("INBOX contains "+String($info.mailRecent)+" recent emails.")
.getBoxList()
History
Release | Changes |
---|---|
18 R4 | Added |
19 | Add isSubscribed parameter |
.getBoxList( { parameters : Object } ) : Collection
Parameter | Type | Description | |
---|---|---|---|
parameters | Object | -> | Parameter object |
Result | Collection | <- | Collection of mailbox objects |
Description
The .getBoxList()
function returns a collection of mailboxes describing all of the available mailboxes. This function allows you to locally manage the list of messages located on the IMAP mail server.
In the optional parameters
parameter, pass an object containing values to filter the returned mailboxes. You can pass:
Property | Type | Description |
---|---|---|
isSubscribed | Boolean |
Result
Each object of the returned collection contains the following properties:
Property | Type | Description |
---|---|---|
[].name | text | Name of the mailbox |
[].selectable | boolean | Indicates whether or not the access rights allow the mailbox to be selected:
|
[].inferior | boolean | Indicates whether or not the access rights allow creating a lower hierachy in the mailbox:
|
[].interesting | boolean | Indicates if the mailbox has been marked "interesting" by the server:
|
If the account does not contain any mailboxes, an empty collection is returned.
- If there is no open connection,
.getBoxList()
will open a connection.- If the connection has not been used since the designated connection delay (see
IMAP New transporter
), the.checkConnection( )
function is automatically called.
Example
var $transporter : 4D.IMAPTransporter
$transporter:=IMAP New transporter($server)
$boxList:=$transporter.getBoxList()
For each($box;$boxList)
If($box.interesting)
$split:=Split string($box.name;$transporter.getDelimiter())
ALERT("New emails are available in the box: "+$split[$split.length-1])
End if
End for each
.getDelimiter()
History
Release | Changes |
---|---|
18 R4 | Added |
.getDelimiter() : Text
Parameter | Type | Description | |
---|---|---|---|
Result | Text | <- | Hierarchy delimiter character |
Description
The .getDelimiter()
function returns the character used to delimit levels of hierarchy in the mailbox name.
The delimiter is a character which can be used to:
- create lower level (inferior) mailboxes
- search higher or lower within the mailbox hierarchy
Result
Mailbox name delimiter character.
- If there is no open connection,
.getDelimiter()
will open a connection.- If the connection has not been used since the designated connection delay, the
.checkConnection()
function is automatically called.
Example
var $transporter : 4D.IMAPTransporter
$transporter:=IMAP New transporter($server)
$boxList:=$transporter.getBoxList()
For each($box;$boxList)
If($box.interesting)
$split:=Split string($box.name;$transporter.getDelimiter())
ALERT("New emails are available in the box: "+$split[$split.length-1])
End if
End for each
.getMail()
History
Release | Changes |
---|---|
18 R4 | Added |
.getMail( msgNumber: Integer { ; options : Object } ) : Object
.getMail( msgID: Text { ; options : Object } ) : Object
Parameter | Type | Description | |
---|---|---|---|
msgNumber | Integer | -> | Sequence number of the message |
msgID | Text | -> | Unique ID of the message |
options | Object | -> | Message handling instructions |
Result | Object | <- | Email object |
Description
The .getMail()
function returns the Email
object corresponding to the msgNumber or msgID in the mailbox designated by the IMAP_transporter
. This function allows you to locally handle the email contents.
In the first parameter, you can pass either:
- msgNumber, an integer value indicating the sequence number of the message to retrieve or
- msgID, a text value indicating the unique ID of the message to retrieve.
The optional options parameter allows you pass an object defining additional instructions for handling the message. The following properties are available:
Property | Type | Description |
---|---|---|
updateSeen | boolean | If True, the message is marked as "seen" in the mailbox. If False, the message is not marked as "seen". Default value: True |
withBody | boolean | Pass True to return the body of the message. If False, only the message header is returned. Default value: True |
- The function generates an error and returns Null if msgID designates a non-existing message,
- If no mailbox is selected with the
.selectBox()
function, an error is generated,- If there is no open connection,
.getMail()
will open a connection the last mailbox specified with.selectBox()
`.
Result
.getMail()
returns an Email
object with the following specific IMAP properties: id, receivedAt, and size.
Example
You want to get the message with ID = 1:
var $server : Object
var $info; $mail; $boxInfo : Variant
var $transporter : 4D.IMAPTransporter
$server:=New object
$server.host:="imap.gmail.com" //Mandatory
$server.port:=993
$server.user:="4d@gmail.com"
$server.password:="XXXXXXXX"
//create transporter
$transporter:=IMAP New transporter($server)
//select mailbox
$boxInfo:=$transporter.selectBox("Inbox")
//get Email object with ID 1
$mail:=$transporter.getMail(1)
.getMails()
History
Release | Changes |
---|---|
18 R5 | Added |
.getMails( ids : Collection { ; options : Object } ) : Object
.getMails( startMsg : Integer ; endMsg : Integer { ; options : Object } ) : Object
Parameter | Type | Description | |
---|---|---|---|
ids | Collection | -> | Collection of message ID |
startMsg | Integer | -> | Sequence number of the first message |
endMsg | Integer | -> | Sequence number of the last message |
options | Object | -> | Message handling instructions |
Result | Object | <- | Object containing:
|
Description
The .getMails()
function returns an object containing a collection of Email
objects.
First Syntax:
.getMails( ids { ; options } ) -> result
The first syntax allows you to retrieve messages based on their IDs.
In the ids parameter, pass a collection of IDs for the messages to return. You can get the IDs with .getMail()
.
The optional options parameter allows you to define the parts of the messages to be returned. See the Options table below for a description of the available properties.
Second syntax:
.getMails( startMsg ; endMsg { ; options } ) -> result
The second syntax allows you to retrieve messages based on a sequential range. The values passed represent the position of the messages in the mailbox.
In the startMsg parameter, pass an integer value corresponding to the number of the first message in a sequential range. If you pass a negative number (startMsg <= 0), the first message of the mailbox will be used as the beginning of the sequence.
In the endMsg parameter, pass an integer value corresponding to the number of the last message to be included in a sequential range. If you pass a negative number (endMsg <= 0), the last message of the mailbox will be used as the end of the sequence.
The optional options parameter allows you to define the parts of the messages to be returned.
Options
Property | Type | Description |
---|---|---|
updateSeen | Boolean | If True, the specified messages are marked as "seen" in the mailbox. If False, the messages are not marked as "seen". Default value: True |
withBody | Boolean | Pass True to return the body of the specified messages. If False, only the message headers are returned. Default value: True |
- If no mailbox is selected with the
.selectBox()
command, an error is generated.- If there is no open connection,
.getMails()
will open a connection the last mailbox specified with.selectBox()
.
Result
.getMails()
returns an object containing the following collections:
Property | Type | Description |
---|---|---|
list | Collection | Collection of Email objects. If no Email objects are found, an empty collection is returned. |
notFound | Collection | Collection of:
|
Example
You want to retrieve the 20 most recent emails without changing their "seen" status:
var $server,$boxInfo,$result : Object
var $transporter : 4D.IMAPTransporter
$server:=New object
$server.host:="imap.gmail.com" //Mandatory
$server.port:=993
$server.user:="4d@gmail.com"
$server.password:="XXXXXXXX"
//create transporter
$transporter:=IMAP New transporter($server)
//select mailbox
$boxInfo:=$transporter.selectBox("INBOX")
If($boxInfo.mailCount>0)
// retrieve the headers of the last 20 messages without marking them as read
$result:=$transporter.getMails($boxInfo.mailCount-20;$boxInfo.mailCount;\
New object("withBody";False;"updateSeen";False))
For each($mail;$result.list)
// ...
End for each
End if
.getMIMEAsBlob()
History
Release | Changes |
---|---|
18 R4 | Added |
.getMIMEAsBlob( msgNumber : Integer { ; updateSeen : Boolean } ) : Blob
.getMIMEAsBlob( msgID : Text { ; updateSeen : Boolean } ) : Blob
Parameter | Type | Description | |
---|---|---|---|
msgNumber | Integer | -> | Sequence number of the message |
msgID | Text | -> | Unique ID of the message |
updateSeen | Boolean | -> | If True, the message is marked "seen" in the mailbox. If False the message is left untouched. |
Result | BLOB | <- | Blob of the MIME string returned from the mail server |
Description
The .getMIMEAsBlob()
function returns a BLOB containing the MIME contents for the message corresponding to the msgNumber or msgID in the mailbox designated by the IMAP_transporter
.
In the first parameter, you can pass either:
- msgNumber, an integer value indicating the sequence number of the message to retrieve or
- msgID, a text value indicating the unique ID of the message to retrieve.
The optional updateSeen parameter allows you to specify if the message is marked as "seen" in the mailbox. You can pass:
- True - to mark the message as "seen" (indicating the message has been read)
- False - to leave the message's "seen" status untouched
- The function returns an empty BLOB if msgNumber or msgID* designates a non-existing message,
- If no mailbox is selected with the
.selectBox()
command, an error is generated,- If there is no open connection,
.getMIMEAsBlob()
will open a connection the last mailbox specified with.selectBox()
.
Result
.getMIMEAsBlob()
returns a BLOB
which can be archived in a database or converted to an Email
object with the MAIL Convert from MIME
command.
Example
var $server : Object
var $boxInfo : Variant
var $blob : Blob
var $transporter : 4D.IMAPTransporter
$server:=New object
$server.host:="imap.gmail.com"
$server.port:=993
$server.user:="4d@gmail.com"
$server.password:="XXXXXXXX"
//create transporter
$transporter:=IMAP New transporter($server)
//select mailbox
$boxInfo:=$transporter.selectBox("Inbox")
//get BLOB
$blob:=$transporter.getMIMEAsBlob(1)
.host
History
Release | Changes |
---|---|
17 R5 | Added |
.host : Text
Description
The .host
property contains the name or the IP address of the host server. Used for mail transactions (SMTP, POP3, IMAP).
.logFile
History
Release | Changes |
---|---|
17 R5 | Added |
.logFile : Text
Description
The .logFile
property contains the path of the extended log file defined (if any) for the mail connection. It can be relative (to the current Logs folder) or absolute.
Unlike regular log files (enabled via the SET DATABASE PARAMETER
command), extended log files store MIME contents of all sent mails and do not have any size limit. For more information about extended log files, refer to:
- SMTP connections - 4DSMTPLog.txt
- POP3 connections - 4DPOP3Log.txt
- IMAP connections - 4DIMAPLog.txt
.move()
History
Release | Changes |
---|---|
18 R5 | Added |
.move( msgsIDs : Collection ; destinationBox : Text ) : Object
.move( allMsgs : Integer ; destinationBox : Text ) : Object
Parameter | Type | Description | |
---|---|---|---|
msgsIDs | Collection | -> | Collection of message unique IDs (strings) |
allMsgs | Integer | -> | IMAP all : All messages in the selected mailbox |
destinationBox | Text | -> | Mailbox to receive moved messages |
Result | Object | <- | Status of the move operation |
Description
The .move()
function moves the messages defined by msgsIDs or allMsgs to the destinationBox on the IMAP server.
You can pass:
- in the msgsIDs parameter, a collection containing the unique IDs of the specific messages to move, or
- in the allMsgs parameter, the
IMAP all
constant (integer) to move all messages in the selected mailbox.
The destinationBox parameter allows you to pass a text value with the name of the mailbox where the messages will be moved.
This function is only supported by IMAP servers compliant with RFC 8474.
Returned object
The function returns an object describing the IMAP status:
Property | Type | Description | |
---|---|---|---|
success | Boolean | True if the operation is successful, False otherwise | |
statusText | Text | Status message returned by the IMAP server, or last error returned in the 4D error stack | |
errors | Collection | 4D error stack (not returned if a IMAP server response is received) | |
[].errcode | Number | 4D error code | |
[].message | Text | Description of the 4D error | |
[].componentSignature | Text | Signature of the internal component which returned the error |
Example 1
To move a selection of messages:
var $server;$boxInfo;$status : Object
var $mailIds : Collection
var $transporter : 4D.IMAPTransporter
$server:=New object
$server.host:="imap.gmail.com" //Mandatory
$server.port:=993
$server.user:="4d@gmail.com"
$server.password:="XXXXXXXX"
$transporter:=IMAP New transporter($server)
//select mailbox
$boxInfo:=$transporter.selectBox("inbox")
//get collection of message unique IDs
$mailIds:=$transporter.searchMails("subject \"4D new feature:\"")
// Move found messages from the current mailbox to the "documents" mailbox
$status:=$transporter.move($mailIds;"documents")
Example 2
To move all messages in the current mailbox:
var $server;$boxInfo;$status : Object
var $transporter : 4D.IMAPTransporter
$server:=New object
$server.host:="imap.gmail.com" //Mandatory
$server.port:=993
$server.user:="4d@gmail.com"
$server.password:="XXXXXXXX"
$transporter:=IMAP New transporter($server)
//select mailbox
$boxInfo:=$transporter.selectBox("inbox")
// move all messages in the current mailbox to the "documents" mailbox
$status:=$transporter.move(IMAP all;"documents")
.numToID()
History
Release | Changes |
---|---|
18 R5 | Added |
.numToID( startMsg : Integer ; endMsg : Integer ) : Collection
Parameter | Type | Description | |
---|---|---|---|
startMsg | Integer | -> | Sequence number of the first message |
endMsg | Integer | -> | Sequence number of the last message |
Result | Collection | <- | Collection of unique IDs |
Description
The .numToID()
function converts the sequence numbers to IMAP unique IDs for the messages in the sequential range designated by startMsg and endMsg in the currently selected mailbox.
In the startMsg parameter, pass an integer value corresponding to the number of the first message in a sequential range. If you pass a negative number (startMsg <= 0), the first message of the mailbox will be used as the beginning of the sequence.
In the endMsg parameter, pass an integer value corresponding to the number of the last message to be included in a sequential range. If you pass a negative number (endMsg <= 0), the last message of the mailbox will be used as the end of the sequence.
Result
The function returns a collection of strings (unique IDs).
Example
var $transporter : 4D.IMAPTransporter
var $server;$boxInfo;$status : Object
var $mailIds : Collection
$server:=New object
$server.host:="imap.gmail.com" //Mandatory
$server.port:=993
$server.user:="4d@gmail.com"
$server.password:="XXXXXXXX"
$transporter:=IMAP New transporter($server)
//select mailbox
$boxInfo:=$transporter.selectBox("inbox")
//get IDs for 5 last messages received
$mailIds:=$transporter.numToID(($boxInfo.mailCount-5);$boxInfo.mailCount)
//delete the messages from the current mailbox
$status:=$transporter.delete($mailIds)
.removeFlags()
History
Release | Changes |
---|---|
20 | Supports custom keywords |
18 R6 | Added |
.removeFlags( msgIDs : Collection ; keywords : Object ) : Object
.removeFlags( msgIDs : Text ; keywords : Object ) : Object
.removeFlags( msgIDs : Longint ; keywords : Object ) : Object
Parameter | Type | Description | |
---|---|---|---|
msgIDs | Collection | -> | Collection of strings: Message unique IDs (text) Text: Unique ID of a message Longint (IMAP all): All messages in the selected mailbox |
keywords | Object | -> | Keyword flags to remove |
Result | Object | <- | Status of the removeFlags operation |
Description
The .removeFlags()
function removes flags from the msgIDs
for the specified keywords
.
In the msgIDs
parameter, you can pass either:
- a collection containing the unique IDs of specific messages or
- the unique ID (text) of a single message or
- the following constant (longint) for all messages in the selected mailbox:
Constant | Value | Comment |
---|---|---|
IMAP all | 1 | Select all messages in the selected mailbox |
The keywords
parameter lets you define the flags to remove from msgIDs
. You can use the following standard flags as well as custom flags:
Parameter | Type | Description |
---|---|---|
$draft | Boolean | True to remove the "draft" flag from the message |
$seen | Boolean | True to remove the "seen" flag from the message |
$flagged | Boolean | True to remove the "flagged" flag from the message |
$answered | Boolean | True to remove the "answered" flag from the message |
$deleted | Boolean | True to remove the "deleted" flag from the message |
<custom flag> | Boolean | True to remove the custom flag from the message |
Please refer to .addFlags() for more information on custom flags.
- For a keyword to be taken into account it has to be true.
Returned object
The function returns an object describing the IMAP status:
Property | Type | Description | |
---|---|---|---|
success | Boolean | True if the operation is successful, False otherwise | |
statusText | Text | Status message returned by the IMAP server, or last error returned in the 4D error stack | |
errors | Collection | 4D error stack (not returned if a IMAP server response is received) | |
[].errcode | Number | 4D error code | |
[].message | Text | Description of the 4D error | |
[].componentSignature | Text | Signature of the internal component which returned the error |
Example
var $options;$transporter;$boxInfo;$status : Object
$options:=New object
$options.host:="imap.gmail.com"
$options.port:=993
$options.user:="4d@gmail.com"
$options.password:="xxxxx"
// Create transporter
$transporter:=IMAP New transporter($options)
// Select mailbox
$boxInfo:=$transporter.selectBox("INBOX")
// Mark all messages from INBOX as unseen
$flags:=New object
$flags["$seen"]:=True
$status:=$transporter.removeFlags(IMAP all;$flags)
.renameBox()
History
Release | Changes |
---|---|
19 | Added |
.renameBox( currentName : Text ; newName : Text ) : Object
Parameter | Type | Description | |
---|---|---|---|
currentName | Text | -> | Name of the current mailbox |
newName | Text | -> | New mailbox name |
Result | Object | <- | Status of the renaming operation |
Description
The .renameBox()
function changes the name of a mailbox on the IMAP server. Attempting to rename a mailbox from a mailbox name that does not exist or to a mailbox name that already exists will generate an error.
In the currentName
parameter, pass the name of the mailbox to be renamed.
Pass the new name for the mailbox in the newName
parameter.
Returned object
The function returns an object describing the IMAP status:
Property | Type | Description | |
---|---|---|---|
success | Boolean | True if the operation is successful, False otherwise | |
statusText | Text | Status message returned by the IMAP server, or last error returned in the 4D error stack | |
errors | Collection | 4D error stack (not returned if a IMAP server response is received) | |
[].errcode | Number | 4D error code | |
[].message | Text | Description of the 4D error | |
[].componentSignature | Text | Signature of the internal component which returned the error |
Example
To to rename your “Invoices” mailbox to “Bills”:
var $pw : text
var $options; $transporter; $status : object
$options:=New object
$pw:=Request("Please enter your password:")
If(OK=1) $options.host:="imap.gmail.com"
$options.user:="test@gmail.com"
$options.password:=$pw
$transporter:=IMAP New transporter($options)
// rename mailbox
$status:=$transporter.renameBox("Invoices"; "Bills")
If ($status.success)
ALERT("Mailbox renaming successful!")
Else
ALERT("Error: "+$status.statusText)
End if
End if
.port
History
Release | Changes |
---|---|
17 R4 | Added |
.port : Integer
Description
The .port
property contains the port number used for mail transactions. By default, if the port property has not been set in the server object (used to create the transporter object with SMTP New transporter
, POP3 New transporter
, IMAP New transporter
), the port used is:
- SMTP - 587
- POP3 - 995
- IMAP - 993
.searchMails()
History
Release | Changes |
---|---|
18 R5 | Added |
.searchMails( searchCriteria : Text ) : Collection
Parameter | Type | Description | |
---|---|---|---|
searchCriteria | Text | -> | Search criteria |
Result | Collection | <- | Collection of message numbers |
Description
This function is based upon the specification for the IMAP protocol.
The .searchMails()
function searches for messages that match the given searchCriteria in the current mailbox. searchCriteria consists of one or more search keys.
searchCriteria is a text parameter listing one or more search keys (see Authorized search-keys below) associated or not with values to look for. A search key may be a single or multiple items. For example:
SearchKey1 = FLAGGED
SearchKey2 = NOT FLAGGED
SearchKey3 = FLAGGED DRAFT
Matching is usually not case-sensitive
- If the searchCriteria is a null string, the search will be equivalent to a “select all”.
- If the searchCriteria includes multiple search keys, the result is the intersection (AND function) of all the messages that match those keys.
searchCriteria = FLAGGED FROM "SMITH"
... returns all messages with \Flagged flag set AND sent by Smith.
- You can use the OR or NOT operators as follows:
searchCriteria = OR SEEN FLAGGED
... returns all messages with \Seen flag set OR \Flagged flag set
searchCriteria = NOT SEEN
... returns all messages with \Seen flag not set.
searchCriteria = HEADER CONTENT-TYPE "MIXED" NOT HEADER CONTENT-TYPE "TEXT"...
... returns message whose content-type header contains “Mixed” and does not contain “Text”.
searchCriteria = HEADER CONTENT-TYPE "E" NOT SUBJECT "o" NOT HEADER CONTENT-TYPE "MIXED"
... returns message whose content-type header contains “ e ” and whose Subject header does not contain “ o ” and whose content-type header is not “ Mixed ”.
As concerns the last two examples, notice that the result of the search is different when you remove the parentheses of the first search key list.
- The searchCriteria may include the optional [CHARSET] specification. This consists of the "CHARSET" word followed by a registered [CHARSET] (US ASCII, ISO-8859). It indicates the charset of the searchCriteria string. Therefore, you must convert the searchCriteria string into the specified charset if you use the [CHARSET] specification (see the
CONVERT FROM TEXT
orConvert to text
commands). By default, 4D encodes in Quotable Printable the searchCriteria string if it contains extended characters.
searchCriteria = CHARSET "ISO-8859" BODY "Help"
... means the search criteria uses the charset iso-8859 and the server will have to convert the search criteria before searching, if necessary.
Search value types
Search-keys may request the value to search for:
-
Search-keys with a date value: the date is a string that must be formatted as follows: date-day+"-"+date-month+"-"+date-year where date-day indicates the number of the day of the month (max. 2 characters), date-month indicates the name of the month (Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Dec) and date-year indicates the year (4 characters). Example:
searchCriteria = SENTBEFORE 1-Feb-2020
(a date does not usually need to be quoted since it does not contain any special characters) -
Search-keys with a string value: the string may contain any character and must be quoted. If the string does not contain any special characters, like the space character for instance, it does not need to be quoted. Quoting such strings will ensure that your string value will be correctly interpreted. Example:
searchCriteria = FROM "SMITH"
For all search keys that use strings, a message matches the key if the string is a substring of the field. Matching is not case-sensitive. -
Search-keys with a field-name value: the field-name is the name of a header field. Example:
searchCriteria = HEADER CONTENT-TYPE "MIXED"
-
Search-keys with a flag value: the flag may accept one or several keywords (including standard flags), separated by spaces. Example:
searchCriteria = KEYWORD \Flagged \Draft
-
Search-keys with a message set value: Identifies a set of messages. For message sequence numbers, these are consecutive numbers from 1 to the total number of messages in the mailbox. A comma delimits individual numbers; a colon delimits between two numbers inclusive. Examples:
2,4:7,9,12:*
is2,4,5,6,7,9,12,13,14,15
for a mailbox with 15 messages.searchCriteria = 1:5 ANSWERED
search in message selection from message sequence number 1 to 5 for messages which have the \Answered flag set.searchCriteria= 2,4 ANSWERED
search in the message selection (message numbers 2 and 4) for messages which have the \Answered flag set.
Authorized search-keys
ALL: All messages in the mailbox.
ANSWERED: Messages with the \Answered flag set.
UNANSWERED: Messages that do not have the \Answered flag set.
DELETED: Messages with the \Deleted flag set.
UNDELETED: Messages that do not have the \Deleted flag set.
DRAFT: Messages with the \Draft flag set.
UNDRAFT: Messages that do not have the \Draft flag set.
FLAGGED: Messages with the \Flagged flag set.
UNFLAGGED: Messages that do not have the \Flagged flag set.
RECENT: Messages that have the \Recent flag set.
OLD: Messages that do not have the \Recent flag set.
SEEN: Messages that have the \Seen flag set.
UNSEEN: Messages that do not have the \Seen flag set.
NEW: Messages that have the \Recent flag set but not the \Seen flag. This is functionally equivalent to “(RECENT UNSEEN)”.
KEYWORD flag: Messages with the specified keyword set.
UNKEYWORD flag: Messages that do not have the specified keyword set.
BEFORE date: Messages whose internal date is earlier than the specified date.
ON date: Messages whose internal date is within the specified date.
SINCE date: Messages whose internal date is within or later than the specified date.
SENTBEFORE date: Messages whose Date header is earlier than the specified date.
SENTON date: Messages whose Date header is within the specified date.
SENTSINCE date: Messages whose Date header is within or later than the specified date.
TO string: Messages that contain the specified string in the TO header.
FROM string: Messages that contain the specified string in the FROM header.
CC string: Messages that contain the specified string in the CC header.
BCC string: Messages that contain the specified string in the BCC header.
SUBJECT string: Messages that contain the specified string in the Subject header.
BODY string: Messages that contain the specified string in the message body.
TEXT string: Messages that contain the specified string in the header or in the message body.
HEADER field-name string: Messages that have a header with the specified field-name and that contain the specified string in the field-body.
UID message-UID: Messages with unique identifiers corresponding to the specified unique identifier set.
LARGER n: Messages with a size larger than the specified number of bytes.
SMALLER n: Messages with a size smaller than the specified number of bytes.
NOT search-key: Messages that do not match the specified search key.
OR search-key1 search-key2: Messages that match either search key.
.selectBox()
History
Release | Changes |
---|---|
20 | id, flags, permanentFlags are returned |
18 R4 | Added |
.selectBox( name : Text { ; state : Integer } ) : Object
Parameter | Type | Description | |
---|---|---|---|
name | Text | -> | Name of the mailbox |
state | Integer | -> | Mailbox access status |
Result | Object | <- | boxInfo object |
Description
The .selectBox()
function selects the name mailbox as the current mailbox. This function allows you to retrieve information about the mailbox.
To get the information from a mailbox without changing the current mailbox, use
.getBoxInfo()
.
In the name parameter, pass the name of the mailbox to access. The name represents an unambiguous left-to-right hierarchy with levels separated by a specific delimiter character. The delimiter can be found with the .getDelimiter()
function.
The optional state parameter defines the type of access to the mailbox. The possible values are:
Constant | Value | Comment |
---|---|---|
IMAP read only state | 1 | The selected mailbox is accessed with read only privileges. Messages with a "recent" flag (indicating new messages) remain unchanged. |
IMAP read write state | 0 | The selected mailbox is accessed with read and write privileges. Messages are considered "seen" and lose the "recent" flag (indicating new messages). (Default value) |
- The function generates an error and returns Null if name designates a non-existing mailbox.
- If there is no open connection,
.selectBox()
will open a connection.- If the connection has not been used since the designated connection delay (see
IMAP New transporter
), the.checkConnection()
function is automatically called.
Returned object
The boxInfo
object returned contains the following properties:
Property | Type | Description |
---|---|---|
name | Text | Name of the mailbox |
mailCount | number | Number of messages in the mailbox |
mailRecent | number | Number of messages with the "recent" flag |
id | text | Unique id of the mailbox |
flags | text | List of flags currently used for the mailbox, separated by spaces |
permanentFlags | text | List of flags that the client can change permanently (except for the \Recent flag, which is managed by the IMAP server), separated by spaces |
If permanentFlags
string includes the special flag *, it means that the server supports custom flags.
Example
var $server; $boxinfo : Object
$server:=New object
$server.host:="imap.gmail.com" //Mandatory
$server.user:="4d@gmail.com"
$server.password:="XXXXXXXX"
var $transporter : 4D.IMAPTransporter
$transporter:=IMAP New transporter($server)
$boxInfo:=$transporter.selectBox("INBOX")
.subscribe()
History
Release | Changes |
---|---|
19 | Added |
.subscribe( name : Text ) : Object
Parameter | Type | Description | |
---|---|---|---|
name | Text | -> | Name of the mailbox |
Result | Object | <- | Status of the subscribe operation |
Description
The .subscribe()
function allows adding or removing of the specified mailbox to/from the IMAP server’s set of “subscribed” mailboxes. As such, you can choose to narrow down a large list of available mailboxes by subscribing to those you usually want to see.
In the name
parameter, pass the name of the mailbox to add (subscribe) to your "subscribed" mailboxes.
Returned object
The function returns an object describing the IMAP status:
Property | Type | Description | |
---|---|---|---|
success | Boolean | True if the operation is successful, False otherwise | |
statusText | Text | Status message returned by the IMAP server, or last error returned in the 4D error stack | |
errors | Collection | 4D error stack (not returned if a IMAP server response is received) | |
[].errcode | Number | 4D error code | |
[].message | Text | Description of the 4D error | |
[].componentSignature | Text | Signature of the internal component which returned the error |
Example
To subscribe to the "Atlas Corp” mailbox in the "Bills" hierarchy:
var $pw; $name : text
var $options; $transporter; $status : object
$options:=New object
$pw:=Request("Please enter your password:")
If(OK=1) $options.host:="imap.gmail.com"
$options.user:="test@gmail.com"
$options.password:=$pw
$transporter:=IMAP New transporter($options)
$name:="Bills"+$transporter.getDelimiter()+"Atlas Corp"
$status:=$transporter.subscribe($name)
If ($status.success)
ALERT("Mailbox subscription successful!")
Else
ALERT("Error: "+$status.statusText)
End if
End if
.unsubscribe()
History
Release | Changes |
---|---|
19 | Added |
.unsubscribe( name : Text ) : Object
Parameter | Type | Description | |
---|---|---|---|
name | Text | -> | Name of the mailbox |
Result | Object | <- | Status of the unsubscribe operation |
Description
The .unsubscribe()
function removes a mailbox from a set of subscribed mailboxes. This allows you reduce the number of mailboxes you usually see.
In the name
parameter, pass the name of the mailbox to remove (unsubscribe) from your active mailboxes.
Returned object
The function returns an object describing the IMAP status:
Property | Type | Description | |
---|---|---|---|
success | Boolean | True if the operation is successful, False otherwise | |
statusText | Text | Status message returned by the IMAP server, or last error returned in the 4D error stack | |
errors | Collection | 4D error stack (not returned if a IMAP server response is received) | |
[].errcode | Number | 4D error code | |
[].message | Text | Description of the 4D error | |
[].componentSignature | Text | Signature of the internal component which returned the error |
Example
To unsubscribe from the "Atlas Corp” mailbox in the "Bills" hierarchy:
var $pw; $name : text
var $options; $transporter; $status : object
$options:=New object
$pw:=Request("Please enter your password:")
If(OK=1) $options.host:="imap.gmail.com"
$options.user:="test@gmail.com"
$options.password:=$pw
$transporter:=IMAP New transporter($options)
$name:="Bills"+$transporter.getDelimiter()+"Atlas Corp"
$status:=$transporter.unsubscribe($name)
If ($status.success)
ALERT("Mailbox unsubscription successful!")
Else
ALERT("Error: "+$status.statusText)
End if
End if
.user
History
Release | Changes |
---|---|
17 R4 | Added |
.user : Text
Description
The .user
property contains the user name used for authentication on the mail server.