In 4D v18, we introduced a new way to send emails. Then in 4D v18 R2, we added a new way to retrieve emails using POP3. Now in 4D v18 R4, we’re taking the first steps for a new way to handle the IMAP protocol. As we did for SMTP and POP3, there’s a new IMAP New transporter command to handle the IMAP protocol.
The Internet Message Access Protocol (IMAP) is an Internet standard protocol used by email clients to retrieve email messages from a mail server. This protocol makes it possible to manage multiple mailboxes, download emails, perform searches on the server, manage message state, and a lot more!
And with this first IMAP feature, you can add methods to the transporter to: have the list of the available mailboxes, select a mailbox, and receive email from the selected mailbox. Let’s see how.
The HDI below demonstrates how to receive email via IMAP:
create IMAP transporter
Like SMTP and POP3, when using the IMAP protocol, you first need to create a transporter (via the IMAP New transporter command):
$server:=New object
$server.host:="yourIMAPhost.com"
$server.port:=995
$server.user:="4D.IMAP@mail.com"
$server.password:="XXXXXX"
// Create a transporter from your server information
$IMAP_Transporter:=IMAP New transporter($server)
Select mailbox
Because IMAP supports multiple mailboxes, the first thing to do is specify the mailbox you want.
You can obtain a list of all the avaliable mailboxes on your mail server with the getBoxList method:
$boxList:=$IMAP_Transporter.getBoxList()
Alert("Your first mailbox name is "+$boxList[0].name)
Once you know the name of the specific mailbox you need, simply use the selectBox method:
$boxInfo:=$transporter.selectBox($boxList[$boxId].name)
This command returns useful information such as:
- the number of emails in the mailbox
- the number of recent messages
Download email
You have two ways to identify the mail you want to download:
- The first is to use the position of the email in the mailbox, just like you would for POP3. For example, if you want to download the last received mail:
$mail:=$transporter.getMail($boxInfo.mailCount)
- The second is to use the id attribute of the email object returned by the IMAP server. This way is best to use when you want to retrieve a specific email since an email’s position in the mailbox can be modified when you delete emails:
$mail:=$transporter.getMail($emailId)
As we said earlier, this is the first feature for IMAP. There’s more coming. Stay tuned!