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.
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
$server.host:="yourPOP3host.com"
$server.port:=995
$server.user:="4D.POP3@mail.com"
$server.password:="XXXXXX"
// 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
$mails.push($transporter.getMail($mail.number))
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:
$server.host:="yourpop3server.com"
$server.user:="login"
$server.password:="psw"
// Enter the path of the log file you want to create
$server.logFile:="C:\\tmp\\POP3Log.txt"
$transporter:=POP3 New transporter($server)
You can test all of these commands with the HDI above.