Need Assistance with Fetching Unread Emails Using IMAP in LiveCode

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
denilzalopes
Posts: 1
Joined: Sun Jul 07, 2024 9:28 am

Need Assistance with Fetching Unread Emails Using IMAP in LiveCode

Post by denilzalopes » Sun Jul 07, 2024 10:04 am

Hello everyone,

I am a graduate student in my final year, working on a project as part of my internship in an interdisciplinary laboratory focused on human and artificial cognition. My current task involves developing a software agent designed to assist and diagnose blind users, using LiveCode as the development platform.

I am attempting to use the tsNet library in LiveCode to fetch unread emails from an IMAP server. Despite following the documentation and available resources, I have encountered an issue that I have been unable to resolve.

Here are the details of my problem:

Tentative de connexion...

Envoi de la commande LOGIN...

Réponse de LOGIN reçue.

Réponse de LOGIN : * OK IMAP4rev1 proxy server ready

CAPABILITY ACL BINARY CHILDREN ID IDLE IMAP4rev1 LITERAL+ MULTIAPPEND NAMESPACE QUOTA SASL-IR SORT THREAD=ORDEREDSUBJECT UIDPLUS UNSELECT CATENATE CONDSTORE ENABLE ESEARCH ESORT I18NLEVEL=1 LIST-EXTENDED LIST-STATUS QRESYNC RIGHTS=EKTX SEARCHRES WITHIN XLIST AUTH=PLAIN
H001 OK completed
H002 OK [CAPABILITY IMAP4rev1 ACL BINARY CATENATE CHILDREN CONDSTORE ENABLE ESEARCH ESORT I18NLEVEL=1 ID IDLE LIST-EXTENDED LIST-STATUS LITERAL+ LOGIN-REFERRALS MULTIAPPEND NAMESPACE QRESYNC QUOTA RIGHTS=ektx SASL-IR SEARCHRES SORT THREAD=ORDEREDSUBJECT UIDPLUS UNSELECT WITHIN XLIST] LOGIN completed
LOGIN réussi. Sélection de la boîte INBOX...

Réponse de SELECT INBOX : * 647 EXISTS

0 RECENT
OK [UNSEEN 31] mailbox contains unseen messages
OK [UIDVALIDITY 1] UIDs are valid for this mailbox
OK [UIDNEXT 5844] next expected UID is 5844
FLAGS (\Answered \Deleted \Draft \Flagged \Seen $Forwarded $MDNSent Forwarded $Junk $NotJunk Junk JunkRecorded NonJunk NotJunk)
OK [PERMANENTFLAGS (\Answered \Deleted \Draft \Flagged \Seen $Forwarded $MDNSent Forwarded *)] junk-related flags are not permanent
OK [HIGHESTMODSEQ 27180] modseq tracked on this mailbox
H003 OK [READ-WRITE] SELECT completed
Connexion réussie. Récupération d'un message...

Envoi de la commande FETCH...

Réponse de FETCH reçue.

Réponse complète du serveur pour FETCH : (empty response)

I also receive an error message:
tsneterr: (3) URL using bad/illegal format or missing URL

Here is the script I am using:

local gIMAPURL, tSettings

on mouseUp
connectAndRetrieveOneMessage
end mouseUp

on connectAndRetrieveOneMessage
local tIMAPServer, tIMAPPort, tUsername, tPassword, tURL

put "imapP8" into tIMAPServer
put 993 into tIMAPPort
put "adresseEmailP8" into tUsername
put "mdpP8" into tPassword

put empty into tSettings
put tUsername into tSettings["username"]
put tPassword into tSettings["password"]
put true into tSettings["use_ssl"]
put "login" into tSettings["auth"]
put 30000 into tSettings["timeout"] -- Increase timeout to 30 seconds

put "imaps://" & tIMAPServer & ":" & tIMAPPort into tURL
put tURL into gIMAPURL

if connectToIMAPServer(tURL, tUsername, tPassword) then
getOneMessage
else
answer "Error connecting to the IMAP server."
end if
end connectAndRetrieveOneMessage

function connectToIMAPServer pURL, pUsername, pPassword
local tRequest, tResponse, tResult, tHeaders, tBytes

put "LOGIN " & quote & pUsername & quote & space & quote & pPassword & quote into tRequest

try
put tsNetCustomSync(pURL, tRequest, tHeaders, tResponse, tBytes, tSettings) into tResult

if "OK" is in tResponse then
put "SELECT INBOX" into tRequest
put tsNetCustomSync(pURL, tRequest, tHeaders, tResponse, tBytes, tSettings) into tResult
return "OK" is in tResponse
end if
catch e
answer "Error during connection: " & e
end try

return false
end connectToIMAPServer

on getOneMessage
local tRequest, tResponse, tResult, tHeaders, tBytes, tFullResponse

put "FETCH 1 (BODY[])" into tRequest

repeat 3 times -- Try to read up to 3 times
put tsNetCustomSync(gIMAPURL, tRequest, tHeaders, tResponse, tBytes, tSettings) into tResult
put tResult after tFullResponse
if tBytes = 0 then exit repeat
wait 1 second with messages -- Wait a bit between each read
end repeat

if "BODY[]" is in tFullResponse then
local tMessageBody
put char (offset("BODY[]", tFullResponse) + 7) to -1 of tFullResponse into tMessageBody
put char 1 to -3 of tMessageBody into tMessageBody -- Remove the last two characters

if there is a field "MessageField" then
put tMessageBody into field "MessageField"
end if
else
if there is a field "MessageField" then
put "Unable to retrieve message body." into field "MessageField"
end if
end if
end getOneMessage


I would greatly appreciate any help or advice on how to correctly fetch unread emails and extract their bodies using LiveCode and the tsNet library. If there are any additional configurations or steps that I might have missed, please let me know.

Thank you in advance for your assistance!

Best regards,
Denilza Lopes

charlesBUSd8qF
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 40
Joined: Wed Apr 11, 2012 10:28 pm

Re: Need Assistance with Fetching Unread Emails Using IMAP in LiveCode

Post by charlesBUSd8qF » Tue Jul 09, 2024 11:06 am

Hi Denilza,

For retrieving an e-mail message via IMAP, you can use tsNetGet (or tsNetGetSync) with an IMAP url that contains the UID of the relevent message. You do not use tsNetCustomSync to achieve this. To find the UID of the message to use in the URL, you can performed a IMAP SEARCH on the folder.

Take a look at:

https://lessons.livecode.com/m/4071/l/8 ... ap-account

It uses tsNetCustomSync to perform a "SEARCH NOT DELETED" command against the IMAP server to retrieve a list of UIDs for the messages in the INBOX that are not deleted.

Once the user selects a specific message, it uses a tsNetGetSync request with a URL that includes the relevant message UID for retrieving the message.

Here is an example URL for retrieving a message (with a message UID of 1):

imaps://imapP8:993/INBOX/;UID=1

Hope that helps,

Regards,

Charles

Post Reply