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 |
---|---|
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 pass an object with keyword values for specific flags to add to msgIDs
. You can pass any of the following keywords:
Parameter | 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 |
- False values are ignored.
- 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 |