Receive emails with POP3 protocol

4D v18 introduced an easy to use and powerful new way to create and send emails. A way that lets you send eye-catching emails based on HTML (with images, videos, and attachments), using just a few lines of code.  In 4D v18 R2, we’ve added the ability to locally download your emails and remove them from your email server. In this blog post, we’ll go through the new possibilities this offers. 

The new POP3 New transporter command allows you to use the POP3 protocol to manage email. The Post Office Protocol (POP) is a standard protocol to retrieve email from a mail server. It’s useful for actions such as connecting to a POP3 server, retrieving messages to process automatically and/or store in your local database, and deleting them from the server.

HDI POP3 mailbox downloading

Create a pop3 Transporter

As with SMTP, when using the POP3 protocol you first need to create a POP3 transporter (via the POP3 New transporter command):

$server:=New object
// Create a transporter from your server information
$POP3 transporter:=POP3 New transporter($server)

Mail information

You can get information about all of the messages in your mailbox with the getMailInfoList() method of your POP3 transporter object. The returned list contains, in particular, a message number identifying each email during the POP3 transaction:

$mailInfos:=$POP3 transporter.getMailInfoList()

If you need information for just a single message, you can use also the getMailInfo() method … just pass the message number in parameter:

$mailInfo:=$POP3 transporter.getMailInfo($mailNumber)

receive email

You can download all messages or specific messages using the getMail() method. To download a specific message, simply pass the message number returned by getMailInfoList() as a parameter to this method. The following example demonstrates how to download all of the messages in your mailbox:

$mailInfos:=$POP3 transporter.getMailInfoList()
$mails:=New collection
For each ($mail;$mailInfos)
  // Download email 
End for each

delete email

You can flag a message to be deleted during the session closure with the delete() method. In the same manner as the getMail() method, pass the message number returned by the getMailInfoList() as a parameter. For example, if you want to download and delete all of the messages in your mailbox:

$mailInfos:=$POP3 transporter.getMailInfoList()
For each ($mailInfo;$mailInfos)
  // Download email 
  ProcessMail($POP3 transporter.getMail($mailInfo.number))
  // Flag messages "to be deleted at the end of the session"
  $POP3 transporter.delete($mailInfo.number)
End for each
// Force the session closure to delete the messages flagged for deletion
$POP3 transporter:=Null

Mailbox information

You can get the number of emails in your mailbox and the size of the mailbox with the getBoxInfo() method:

$boxInfo:=$POP3 transporter.getBoxInfo()
ALERT("Size: "+String($boxInfo.size)+" - Count: "+String($boxInfo.mailCount))

log transaction

As with SMTP logs, you can log all your POP3 transactions. There are two ways to create your log:

  • Run a POP3 log on the server – click the “Start Request and Debug Logs” button on your 4D Server
  • Log a specific transaction (during debugging, for example) – use the logFile property of the POP3 transporter object:


// Enter the path of the log file you want to create
$transporter:=POP3 New transporter($server)


You can test all of these commands with the HDI above.

Fabrice Mainguené
• Product Owner •Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.