GoogleMail is the Gmail API client within 4D NetKit. It provides send, append, read, delete, label management, and change-notification operations.
Both JMAP (4D mail object) and MIME (raw RFC 2822) formats are supported, controlled by the mailType property set when creating the Google object.
A GoogleMail object is accessed via the mail property of a Google object: $google.mail.
A GoogleMail object exposes the following properties:
| Property | Type | Description |
|---|---|---|
| mailType | Text | (read-only) Mail type used to send and receive emails. Can be "MIME" or "JMAP". Set via the mailType option in cs.NetKit.Google.new(). |
| userId | Text | User identifier used to identify the user in Service mode. Can be the id or the userPrincipalName. |
.append( mail : Text { ; labelIds : Collection } ) : Object
.append( mail : Blob { ; labelIds : Collection } ) : Object
.append( mail : Object { ; labelIds : Collection } ) : Object
| Parameter | Type | Description | |
|---|---|---|---|
| Text | Blob | Object | -> | Email to store. BLOB or Text for MIME format; Object (JMAP) for JMAP format. | |
| labelIds | Collection | -> | Collection of label IDs to apply. Defaults to ["DRAFT"] when omitted or empty. |
| Result | Object | <- | Status object with an additional id property. |
.append() stores a mail message in the user’s mailbox without sending it. Useful for importing existing messages or saving drafts with custom labels.
If
labelIdsis passed and the mail has afromorsenderheader, the Gmail server automatically adds theSENTlabel.
The method returns a status object with an additional id property:
| Property | Type | Description |
|---|---|---|
| id | Text | ID of the email created on the server. |
| success | Boolean | See status object. |
| statusText | Text | See status object. |
| errors | Collection | See status object. |
// Append a mail as a draft (default)
$status:=$google.mail.append($mail)
// Append a mail directly to the inbox
$status:=$google.mail.append($mail; ["INBOX"])
.delete( mailID : Text { ; permanently : Boolean } ) : Object
| Parameter | Type | Description | |
|---|---|---|---|
| mailID | Text | -> | ID of the mail to delete. |
| permanently | Boolean | -> | If true, deletes permanently. If false (default), moves the message to Trash. |
| Result | Object | <- | Status object. |
.delete() deletes the specified message from the user’s mailbox, either permanently or by moving it to Trash.
The method returns a standard status object.
This method requires one of the following OAuth scopes:
https://mail.google.com/
https://www.googleapis.com/auth/gmail.modify
// Delete permanently
$status:=$google.mail.delete($mailId; True)
.getMail( mailID : Text { ; param : Object } ) : Object
.getMail( mailID : Text { ; param : Object } ) : Blob
| Parameter | Type | Description | |
|---|---|---|---|
| mailID | Text | -> | ID of the message to retrieve. |
| param | Object | -> | Options for the message to retrieve (optional). |
| Result | Object | Blob | <- | Downloaded mail; Null on error. |
.getMail() gets the specified message from the user’s mailbox.
In param, you can pass the following properties:
| Property | Type | Description |
|---|---|---|
| format | Text | The format to return the message in. Can be: "raw" (default, full email), "minimal" (ID and labels only), "metadata" (ID, labels, and headers only). |
| headers | Collection | Collection of header names to return. Only used when format is "metadata". |
| mailType | Text | Override the instance mailType for this call. Can be "MIME" or "JMAP". |
The method returns a mail in one of the following formats depending on mailType:
| Format | Type | Comment |
|---|---|---|
| MIME | Blob | |
| JMAP | Object | Contains an id attribute. |
.getMailIds( { param : Object } ) : cs.NetKit.GoogleMailIdList
| Parameter | Type | Description | |
|---|---|---|---|
| param | Object | -> | Options for filtering messages (optional). |
| Result | cs.NetKit.GoogleMailIdList | <- | Paginated list of Gmail message IDs. Use next() / previous() to navigate pages. |
.getMailIds() returns an object containing a collection of message IDs in the user’s mailbox.
In param, you can pass the following properties:
| Property | Type | Description |
|---|---|---|
| top | Integer | Maximum number of messages to return. Default is 100, maximum is 500. |
| search | Text | Only return messages matching the specified query. Supports Gmail search syntax. See Gmail search operators. |
| labelIds | Collection | Only return messages that have all the specified label IDs. |
| includeSpamTrash | Boolean | Include messages from SPAM and TRASH. Default is false. |
The method returns a GoogleMailIdList object with the following properties:
| Property | Type | Description |
|---|---|---|
| mailIds | Collection | Collection of objects, each with id (Text) and threadId (Text). Empty if no mail is returned. |
| isLastPage | Boolean | true if the last page is reached. |
| page | Integer | Current page number. Starts at 1. Default page size is 10 (configurable via top). |
| next() | 4D.Function | Loads the next page. Returns true if successful, false otherwise. |
| previous() | 4D.Function | Loads the previous page. Returns true if successful, false otherwise. |
| success | Boolean | See status object. |
| statusText | Text | See status object. |
| errors | Collection | See status object. |
This method requires one of the following OAuth scopes:
https://www.googleapis.com/auth/gmail.modify
https://www.googleapis.com/auth/gmail.readonly
https://www.googleapis.com/auth/gmail.metadata
.getMails( mailIDs : Collection { ; param : Object } ) : Collection
| Parameter | Type | Description | |
|---|---|---|---|
| mailIDs | Collection | -> | Collection of mail IDs (Text), or collection of objects each with an id property. |
| param | Object | -> | Options (same properties as .getMail()) (optional). |
| Result | Collection | <- | Collection of mails (JMAP objects or Blobs depending on mailType). Null on error. |
.getMails() gets a collection of emails based on the specified mailIDs collection.
The maximum number of IDs supported is 100. For more than 100 mails, call the function multiple times.
In param, you can pass the following properties:
| Property | Type | Description |
|---|---|---|
| format | Text | The format to return the messages in. Can be: "raw" (default), "minimal", or "metadata". |
| headers | Collection | Collection of header names to return. Only used when format is "metadata". |
| mailType | Text | Override the instance mailType for this call. Can be "MIME" or "JMAP". |
The method returns a collection of mails in one of the following formats depending on mailType:
| Format | Type | Comment |
|---|---|---|
| MIME | Blob | |
| JMAP | Object | Contains an id attribute. |
.send( email : Text ) : Object
.send( email : Object ) : Object
.send( email : Blob ) : Object
| Parameter | Type | Description | |
|---|---|---|---|
| Text | Blob | Object | -> | Email to be sent. | |
| Result | Object | <- | Status object. |
.send() sends an email using the MIME or JMAP format.
In email, pass the email to send:
The data type passed in email must be compatible with the mailType property.
To avoid authentication errors, ensure your application has appropriate authorizations. One of the following OAuth scopes is required:
gmail.modify,gmail.compose, orgmail.send.
The method returns a standard status object.
var $oAuth2 : cs.NetKit.OAuth2Provider
var $credential:={}
$credential.name:="Google"
$credential.permission:="signedIn"
$credential.clientId:="your-client-id"
$credential.clientSecret:="your-client-secret"
$credential.redirectURI:="http://127.0.0.1:50993/authorize/"
$credential.scope:="https://www.googleapis.com/auth/gmail.send"
$oAuth2:=cs.NetKit.OAuth2Provider.new($credential)
var $email:={}
$email.from:="noreply.mail@gmail.com"
$email.to:="address1@mail.com,address2@mail.com"
$email.cc:={name: "Stephen"; email: "address3@mail.com"}
$email.subject:="Hello world"
$email.textBody:="Test mail \r\n This is just a test e-mail \r\n Please ignore it"
$email.attachments:=[MAIL New attachment($filePath)]
var $Google:=cs.NetKit.Google.new($oAuth2; {mailType: "JMAP"})
var $status:=$Google.mail.send($email)
.untrash( mailID : Text ) : Object
| Parameter | Type | Description | |
|---|---|---|---|
| mailID | Text | -> | ID of the message to remove from Trash. |
| Result | Object | <- | Status object. |
.untrash() removes the specified message from Trash.
The method returns a standard status object.
This method requires one of the following OAuth scopes:
https://mail.google.com/
https://www.googleapis.com/auth/gmail.modify
.update( mailIDs : Collection ; param : Object ) : Object
| Parameter | Type | Description | |
|---|---|---|---|
| mailIDs | Collection | -> | Collection of mail IDs (Text), or collection of objects each with an id property. Limited to 1000 IDs per request. |
| param | Object | -> | Modification options. |
| Result | Object | <- | Status object. |
.update() adds or removes labels on the specified messages to help categorize emails. Labels can be system labels (e.g., INBOX, SPAM, TRASH, UNREAD, STARRED, IMPORTANT) or custom labels. Multiple labels can be applied simultaneously.
In param, you can pass the following properties:
| Property | Type | Description |
|---|---|---|
| addLabelIds | Collection | Collection of label IDs to add to the messages. |
| removeLabelIds | Collection | Collection of label IDs to remove from the messages. |
The method returns a standard status object.
// Mark a collection of emails as unread
$result:=$google.mail.update($mailIds; {addLabelIds: ["UNREAD"]})
.createLabel( labelInfo : Object ) : Object
| Parameter | Type | Description | |
|---|---|---|---|
| labelInfo | Object | -> | labelInfo object containing label properties to create. |
| Result | Object | <- | Status object with an additional label property. |
.createLabel() creates a new label.
The method returns a status object with an additional label property:
| Property | Type | Description |
|---|---|---|
| label | Object | Newly created label instance (see labelInfo object). |
| success | Boolean | See status object. |
| statusText | Text | See status object. |
| errors | Collection | See status object. |
$status:=$google.mail.createLabel({name: "Backup"})
$labelId:=$status.label.id
.deleteLabel( labelId : Text ) : Object
| Parameter | Type | Description | |
|---|---|---|---|
| labelId | Text | -> | ID of the label to delete. |
| Result | Object | <- | Status object. |
.deleteLabel() immediately and permanently deletes the specified label and removes it from any messages and threads it is applied to.
This method is only available for labels with
type="user".
The method returns a standard status object.
$status:=$google.mail.deleteLabel($labelId)
.getLabel( labelId : Text ) : Object
| Parameter | Type | Description | |
|---|---|---|---|
| labelId | Text | -> | ID of the label to retrieve. |
| Result | Object | <- | labelInfo object with additional counter properties. |
.getLabel() returns the information of a label.
The returned labelInfo object includes the following additional properties:
| Property | Type | Description |
|---|---|---|
| messagesTotal | Integer | Total number of messages with this label. |
| messagesUnread | Integer | Number of unread messages with this label. |
| threadsTotal | Integer | Total number of threads with this label. |
| threadsUnread | Integer | Number of unread threads with this label. |
$info:=$google.mail.getLabel($labelId)
$name:=$info.name
$emailNumber:=$info.messagesTotal
$unread:=$info.messagesUnread
.getLabelList() : Object
| Parameter | Type | Description | |
|---|---|---|---|
| Result | Object | <- | Status object with an additional labels property. |
.getLabelList() returns an object containing the collection of all labels in the user’s mailbox.
The method returns a status object with an additional labels property:
| Property | Type | Description |
|---|---|---|
| labels | Collection | Collection of mailLabel objects. |
| success | Boolean | See status object. |
| statusText | Text | See status object. |
| errors | Collection | See status object. |
A mailLabel object contains the following properties:
| Property | Type | Description |
|---|---|---|
| name | Text | Display name of the label. |
| id | Text | Immutable ID of the label. |
| messageListVisibility | Text | Visibility in the message list. Can be "show" or "hide". |
| labelListVisibility | Text | Visibility in the label list. Can be "labelShow", "labelShowIfUnread", or "labelHide". |
| type | Text | Owner type: "user" (custom, modifiable) or "system" (created by Gmail, cannot be modified or deleted). |
.updateLabel( labelId : Text ; labelInfo : Object ) : Object
| Parameter | Type | Description | |
|---|---|---|---|
| labelId | Text | -> | ID of the label to update. |
| labelInfo | Object | -> | labelInfo object containing updated label properties. |
| Result | Object | <- | Status object with an additional label property. |
.updateLabel() updates the specified label.
This method is only available for labels with
type="user".
The method returns a status object with an additional label property:
| Property | Type | Description |
|---|---|---|
| label | Object | Updated label instance (see labelInfo object). |
| success | Boolean | See status object. |
| statusText | Text | See status object. |
| errors | Collection | See status object. |
$status:=$google.mail.updateLabel($labelId; {name: "Backup January"})
.notifier( param : Object { ; labelId : Text } ) : cs.NetKit.GoogleNotification
| Parameter | Type | Description | |
|---|---|---|---|
| param | Object | -> | Callback and mode definitions (see below). |
| labelId | Text | -> | (optional) Subscribe only to changes of items in this label. If omitted, subscribe to changes in all labels. |
| Result | cs.NetKit.GoogleNotification | <- | Notification object with start(), stop(), expiration, and isStarted. Call start() to begin monitoring. |
.notifier() creates and returns a GoogleNotification object allowing you to configure, start, and stop subscriptions to mail change notifications.
Two modes are available:
{endPoint}/4dnk-google-notification?state={uuid}.When a resource changes, user-defined callbacks are dispatched in the 4D worker where the notifier’s start() function was originally called. The subscription is automatically closed when the notifier object is destroyed.
In param, you can pass the following properties:
| Property | Type | Description |
|---|---|---|
| endPoint | Text | Webhook URL for push mode. If omitted, uses pull mode. Must be a publicly accessible HTTPS endpoint. See endPoint management. |
| onCreate | 4D.Function | Callback for a mail creation (optional). |
| onDelete | 4D.Function | Callback for a mail deletion (optional). |
| onModify | 4D.Function | Callback for a mail modification (optional). |
| timer | Integer | Polling interval in seconds for pull mode (default: 30) (optional). |
| topicName | Text | Google Cloud Pub/Sub topic used by the Gmail API to push mailbox change notifications. The subscription must be created with the delivery type “Push”, and the endPoint URL must include the “4dnk-google-notification” endpoint: “https://mydomain/4dnk-google-notification”. Required when endPoint is specified. |
Callback functions receive two parameters:
| Parameter | Type | Description |
|---|---|---|
| cs.NetKit.Google | The current Google object. | |
| event | Object | Object with type (Text: "mailCreated", "mailDeleted", or "mailModified") and ids (Collection of affected mail IDs). |
The returned GoogleNotification object contains the following properties:
| Property | Type | Description |
|---|---|---|
| endPoint | Text | Publicly accessible HTTPS endpoint that receives notifications. |
| expiration | Text | Expiration date and time (timestamp). Read-only. |
| isStarted | Boolean | true when notifications are active, false when stopped. Read-only. |
| start() | 4D.Function | Starts the subscription. Returns a status object (success, statusText, errors). |
| stop() | 4D.Function | Stops the subscription. Returns a status object (success, statusText, errors). |
| timer | Integer | Interval in seconds between delta query checks (pull mode). |
Mail notifications via webhook (push mode):
var $notif:=$google.mail.notifier({ \
endPoint: "https://myserver.com"; \
onCreate: Formula(ALERT("New mail: "+String($2.ids))); \
onDelete: Formula(ALERT("Mail deleted: "+String($2.ids))) \
})
$status:=$notif.start()
Several .GoogleMail label management methods use a labelInfo object, containing the following properties:
| Property | Type | Description |
|---|---|---|
| id | Text | ID of the label. |
| name | Text | Display name of the label. Mandatory when creating a label. |
| messageListVisibility | Text | Visibility in the message list. Can be "show" or "hide". |
| labelListVisibility | Text | Visibility in the label list. Can be "labelShow", "labelShowIfUnread", or "labelHide". |
| color | Object | Color for the label (only available for type="user" labels). Contains textColor (Text, hex) and backgroundColor (Text, hex, e.g., "#000000"). |
| type | Text | Owner type: "system" (Gmail-created) or "user" (custom, modifiable). |
Several GoogleMail functions return a status object containing the following properties:
| Property | Type | Description |
|---|---|---|
| success | Boolean | true if the operation was successful. |
| statusText | Text | Status message returned by the Gmail server or last error from the 4D error stack. |
| errors | Collection | Collection of 4D error items (not returned if a server response is received): errcode, message, componentSignature. |