4D Blog

Home Product Search emails with IMAP protocol

Search emails with IMAP protocol

October 27, 2020

Product

Sometimes you need to find all of the unread emails on your mail server and download them to display them to your client. The searchMails method does it for you! It uses the IMAP search protocol which is done entirely on the server-side. This means that it’s fast and allows you to retrieve only the desired emails.

 

HDI IMAPMailsSearching

email search

To perform an email search, you must be connected to your mail server and have a mailbox selected. For the examples, we’ll use the Inbox mailbox:

$transporter:=IMAP New transporter($serverInfo)
$boxInfo:=$transporter.selectBox("inbox")

To search within the Inbox mailbox, send a query to the IMAP server. To search for all of the unread emails, create a request with the “unread ” flag and send it to the server via the searchMails method of your IMAP transporter:

$mailIds:=$transporter.searchMails("unread ")

You can conduct more complex searches by combining several flags. For example, if you want to search for all of the unread emails that were received from “imap.test@4d.com” since August 1, 2020:

$mailIds:=$transporter.searchMails("unread SINCE \"1-Aug-2020\" FROM \"imap.test@4d.com\"")

For a complete list of the available flags, refer to the searchMails method documentation.

In return for this search, you’ll get a collection containing all the email IDs that match your request.

download emails

To download all of the found emails, you can use the new getMails method which takes a collection of mails IDs in parameter :

$mails:=$transporter.getMails($mailsIds)

There you go! You have obtained all of your unread emails received from “imap.test@4d.com” since August 1, 2020, in just a few lines of code.

 

Discuss

Tags IMAP, Mail, v18 R5, v19

Latest related posts

  • May 20, 2025

    Automatic Refresh of OEM Build Licenses

  • May 16, 2025

    New class to handle incoming TCP connections

  • April 29, 2025

    Discover your AI-powered writing assistant in 4D Write Pro

Fabrice Mainguené
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.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy