Quantcast
Channel: LiveCode Forums
Viewing all articles
Browse latest Browse all 440

Talking LiveCode • Need Assistance with Fetching Unread Emails Using IMAP in LiveCode

$
0
0
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

Statistics: Posted by denilzalopes — Sun Jul 07, 2024 10:04 am



Viewing all articles
Browse latest Browse all 440

Trending Articles