Jump to content
Apzo

_pop3 UDF, according to the 1939 RFC

Recommended Posts

For $i=$boundary1 To $boundary2-1

If $aSplit[$i] = "" Then $Write = True

If $Write = True Then $message &= $aSplit[$i] & @CRLF

Next

I get an error message above for the bolded line -- it says that the array is badly formatted. The only thing I can think of is that $i might be equaling something < 0 perhaps.

Chris

Share this post


Link to post
Share on other sites

I get an error message above for the bolded line -- it says that the array is badly formatted. The only thing I can think of is that $i might be equaling something < 0 perhaps.

Chris

Hello

I can't figure out where the problem is. Everything works fine on my comp. What Autoit version do you use?

Added:

Put these 2 commands into the code next after "$Write=False" line

ConsoleWrite ($boundary1 & @CRLF & $boundary2 & @CRLF)
    _ArrayDisplay ($aSplit)

And post here the content of an "$aSplit" Array. Important: these array MUST NOT contain any confidentinal information. Send test letter to the mailbox beforehand

Edited by Tipulatoid

Share this post


Link to post
Share on other sites

3.3.0.0

When I do a console write of $i to see what the value is of each iteration, its -1,0,1,2...etc... stops at 17. The other thing is that if I comment that line out and view the array I see the name of who sent the email, the address its from, the subject and charset but nothing else. If you have an instant messaging program I'd be happy to give you the POP3 details I'm using ... perhaps its something specific with my account?

Share this post


Link to post
Share on other sites

3.3.0.0

When I do a console write of $i to see what the value is of each iteration, its -1,0,1,2...etc... stops at 17. The other thing is that if I comment that line out and view the array I see the name of who sent the email, the address its from, the subject and charset but nothing else. If you have an instant messaging program I'd be happy to give you the POP3 details I'm using ... perhaps its something specific with my account?

Yes, you can send me all POP3 details to the maxim.sviridov[at]gmail.com I'll look through

Share this post


Link to post
Share on other sites

I need to save an attachment out of an email to a directory. Any idea how I would do that?

Share this post


Link to post
Share on other sites

Hi Tipulatoid

I wrote (with great help from MrCreatoR) e-mail parser, which may be useful for someone.

It returns an Array where saved:

In the 1st line - Sender's name

In the 2nd line - Sender's address

In the 3rd line - Recipient name

In the 4th line - Recipient address

In the 5th line - Subject

In the 6th line - Charset

In the 7th line - Message body

In the 8th line - Attachment's name (if exists)

In the 9th line - Attachment's body (if exists)

In the next pairs of lines - names and bodies of the other attachments (if exist)

Tried your 2 functions, got this in autoit:

C:\!HBSVN\Ashbytech\Charlene\Charlene.au3 (77) : ==> Array variable subscript badly formatted.:

$iBoundary = $aSplit[$iFirstBoundaryString]

$iBoundary = $aSplit[^ ERROR

Here's my code:

If _pop3Connect($sPop3Server, $sPop3Login, $sPop3Password) = 0 Then
    ;$aMessageStats = _Pop3Stat()
    If Not @error Then
        ;_ArrayDisplay($aMessageStats)
        $aMessageStr = _Pop3Retr(1)
        MsgBox(0,"",$aMessage)
        $aMessage = _ParseEmail($aMessageStr)
        _ArrayDisplay($aMessage)
    EndIf
Else
    MsgBox(16,"Connexion au serveur POP3 échouée","La connexion au serveur POP3 a échoué. Vérifiez vos paramètres.")
EndIf
_Pop3Quit()
_pop3Disconnect()

Share this post


Link to post
Share on other sites

Hi Tipulatoid

Tried your 2 functions, got this in autoit:

Here's my code:

If _pop3Connect($sPop3Server, $sPop3Login, $sPop3Password) = 0 Then
    ;$aMessageStats = _Pop3Stat()
    If Not @error Then
        ;_ArrayDisplay($aMessageStats)
        $aMessageStr = _Pop3Retr(1)
        MsgBox(0,"",$aMessage)
        $aMessage = _ParseEmail($aMessageStr)
        _ArrayDisplay($aMessage)
    EndIf
Else
    MsgBox(16,"Connexion au serveur POP3 échouée","La connexion au serveur POP3 a échoué. Vérifiez vos paramètres.")
EndIf
_Pop3Quit()
_pop3Disconnect()

Right after I noticed that guy a couple a post before who encountered the same problem.

Sorry about the redundancy.

Any solutions so far ?

Thanks!

hench

Share this post


Link to post
Share on other sites

anyone have an updated version of this or other POP3 email parser? I would love to get my hands on a working version. I am getting very similar problems...it looks like maybe the mail server the emails are coming off of is sending things in a different set of formats.

Share this post


Link to post
Share on other sites

Someone seems to have updated it but only posted on the German autoit forums, not really sure how much they updated or anything, I just stumbled into it.

Spoiler: doesn't work with SSL or Gmail...

; :collapseFolds=0:maxLineLen=80:mode=autoitscript:tabSize=8:indentSize=8:folding=indent:
;===============================================================================
; Original UDF:
; http://www.autoitscript.com/forum/index.php?showtopic=22838
;~ Basic functions for AU3 Scripts, based on the 1939 RFC.
;~ See http://www.ietf.org/rfc/rfc1939.txt
;~ Include version : 0.99 (March 2006, 9th).
;~ Requires AU3 beta version 3.1.1.110 or newer.
;~ Author : Luc HENNINOT <lhenninot@nordnet.fr>
;===============================================================================
; Name ..........: _POP3.au3
; Author: .......: Thorsten Willert (thorsten [dot] willert [at] gmx [dot] de)
; Date ..........: Thu Feb 24 22:59:56 CET 2010 @778 /Internet Time/
; Version .......: 1.03
; AutoIt ........: v3.3.2.0
;===============================================================================
#include-once

#Region Global constants
; -- _POP3 error codes, sent by SetError. Use @error to display it. --
Global Enum $POP3_ERROR_OK = 0, _
$POP3_ERROR, _
$POP3_ERROR_TCPCONNECT_FAILED, _
$POP3_ERROR_SERVER_RESPONSE_TIMEOUT, _
$POP3_ERROR_ALREADY_CONNECTED, _
$POP3_ERROR_NOT_CONNECTED, _
$POP3_ERROR_NO_AUTH, _
$POP3_ERROR_TCPRECV_TIMEOUT, _
$POP3_ERROR_USER_REFUSED, _
$POP3_ERROR_PASSWD_REFUSED, _
$POP3_ERROR_ERR_RESPONSE, _
$POP3_ERROR_NO_OK_RESPONSE, _
$POP3_ERROR_STAT_BADRESPONSE, _
$POP3_ERROR_NO_TCP_RESPONSE, _
$POP3_ERROR_STAT_REFUSED, _
$POP3_ERROR_LIST_REFUSED, _
$POP3_ERROR_RSET_REFUSED, _
$POP3_ERROR_RETR_REFUSED, _
$POP3_ERROR_QUIT_REFUSED, _
$POP3_ERROR_DELE_REFUSED, _
$POP3_ERROR_TOP_REFUSED, _
$POP3_ERROR_UIDL_REFUSED, _
$POP3_ERROR_NOOP_REFUSED ; I love this one ;)

;-- _POP3 vars --
Global Const $POP3_OK = '^\+OK'; Regexp syntax
#EndRegion Global constants

#Region Global variables
Global $POP3_TRACE = True
Global $POP3_ISCONNECTED = 0
Global $POP3_ISAUTH = 0
Global $POP3_SOCKET
Global $POP3_SERVER_RESPONSE_TIMEOUT = 60000 ; 1 mn, modify it if needed
#EndRegion Global variables

#Region Example

#include <array.au3>
If _POP3Connect("somenonsslemail@domain.com", "apassword") Then
$a = _POP3Info()
_ArrayDisplay($a)
_POP3Quit()
_POP3Disconnect()
EndIf
Exit

#EndRegion Example

; #FUNCTION# ===================================================================
; Name ..........: _POP3Info
; Description ...: Returns an array with the specified informations about all mails
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3Info([$vInfo = ""])
; Parameter(s): .: $vInfo - Optional: (Default = "") : string or array
; Return Value ..: Success - array (Default: array[date,from,to,subject])
; Failure - 0
; @ERROR -
; Author(s) .....: Thorsten Willert
; Date ..........: Fri Jan 15 18:17:54 CET 2010
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3Info($vInfo = "")
If $POP3_ISAUTH Then
Local $vInf
If $vInfo = "" Then
Local $vInf[4] = ["Date", "From", "To", "Subject"]
ElseIf IsArray($vInfo) Then
$vInf = $vInfo
Else
Local $vInf[1] = [$vInfo]
EndIf

Local $iCnt = _POP3MsgCnt()
If @error Then Return SetError(@error, 0, 0)

Local $sTMP, $aTMP
Local $iInf = UBound($vInf)

If $iCnt > 0 Then
Local $aRet[$iCnt + 1][$iInf]
$aRet[0][0] = $iCnt
For $i = 1 To $iCnt
$sTMP = _POP3Top($i)
If @error Then Return SetError(@error, 0, 0)
For $j = 0 To $iInf - 1
$aTMP = StringRegExp($sTMP, '(?i)\n' & $vInf[$j] & ':\s*(.*?)\r', 3)
If Not @error Then $aRet[$i][$j] = $aTMP[0]
Next
Next
Return $aRet
EndIf

Return SetError($POP3_ERROR, 0, 0)
Else
Return SetError($POP3_ERROR_NO_AUTH, 0, 0)
EndIf
EndFunc ;==>_POP3Info

; #FUNCTION# ===================================================================
; Name ..........: _POP3Connect
; Description ...: Conects to the according pop3 server.
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3Connect($sLogin, $sPasswd[, $sServer = ""[, $iPort = 110]])
; Parameter(s): .: $sLogin -
; $sPasswd -
; $sServer - Optional: (Default = "") : pop3 server
; $iPort - Optional: (Default = 110) :
; Return Value ..: Success - 1
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Fri Jan 15 18:37:29 CET 2010
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3Connect($sLogin, $sPasswd, $sServer = "", $iPort = 110)
If $POP3_ISCONNECTED = 0 Then

If $iPort = 995 Then
ConsoleWriteError("_POP3Connect: Error: SSL not supported ..." & @CRLF)
Return SetError(1, 0, 0)
EndIf

If Not StringInStr($sServer, ".") Then
Local $aTMP = StringRegExp($sLogin, '.*?@(.*?)\.(.*)', 3)
If UBound($aTMP) Then
$sServer = $aTMP[0]
Local $sD = "." & $aTMP[1]
Else
ConsoleWriteError("_POP3Connect: Error: Can't find domain in login-name." & @CRLF)
Return SetError(1, 0, 0)
EndIf
Select
Case Ping("pop3." & $sServer & $sD, 2000)
$sServer = "pop3." & $sServer & $sD
Case Ping("pop." & $sServer & $sD, 2000)
$sServer = "pop." & $sServer & $sD
Case Ping("pop3." & $sServer & ".com", 2000)
$sServer = "pop3." & $sServer & ".com"
Case Ping("pop." & $sServer & ".com", 2000)
$sServer = "pop." & $sServer & ".com"
Case Else
ConsoleWriteError("_POP3Connect: Error: Can't find POP3-server." & @CRLF)
Return SetError(1, 0, 0)
EndSelect
EndIf

TCPStartup()

; Basic name to IP conversion
ConsoleWrite("_POP3Connect: connecting to: (" & $sServer & ") ")
If StringRegExp($sServer, "[a-zA-Z]") Then $sServer = TCPNameToIP($sServer)
ConsoleWrite($sServer & ":" & $iPort & @CRLF)
$POP3_SOCKET = TCPConnect($sServer, $iPort)
If @error Then
$POP3_ISCONNECTED = 0
ConsoleWriteError("_POP3Connect: Error: " & @error & @CRLF)
Return SetError($POP3_ERROR_TCPCONNECT_FAILED, 0, 0)
Else
$POP3_ISCONNECTED = 1
EndIf

; We need a first OK from pop3 server
__POP3WaitForOK()
If @error Then Return SetError($POP3_ERROR_NO_OK_RESPONSE, 0, 0)

; Send user
__POP3Cmd("USER " & $sLogin)
If @error Then Return SetError(@error, 0, 0)

; Send passwd
__POP3Cmd("PASS " & $sPasswd)
If @error Then Return SetError(@error, 0, 0)

$POP3_ISAUTH = 1
Return 1
Else
Return SetError($POP3_ERROR_ALREADY_CONNECTED, 0, 0)
EndIf
EndFunc ;==>_POP3Connect

; #FUNCTION# ===================================================================
; Name ..........: _POP3Dele
; Description ...: Delete msg n-msg_number.
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3Dele($iMsg)
; Parameter(s): .: $iMsg - msg-number
; Return Value ..: Success - server response
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 15:24:41 CET 2010
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3Dele($iMsg)
If $POP3_ISAUTH Then
__POP3Cmd("DELE " & $iMsg)
If @error Then Return SetError(@error, 0, 0)

Local $sRet = __POP3WaitTcpResponse()
If @error Then Return SetError($POP3_ERROR_NO_TCP_RESPONSE, 0, 0)
Return $sRet
Else
Return SetError($POP3_ERROR_NO_AUTH, 0, 0)
EndIf
EndFunc ;==>_POP3Dele

; #FUNCTION# ===================================================================
; Name ..........: _POP3Disconnect
; Description ...: Shuts down connection.
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3Disconnect()
; Parameter(s): .: -
; Return Value ..: Success - 1
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 11:15:16 CET 2010
; Remark(s) .....: Use _POP3Quit to exit !!
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3Disconnect()
If $POP3_ISCONNECTED <> 0 Then
TCPCloseSocket($POP3_SOCKET)
TCPShutdown()
$POP3_ISCONNECTED = 0
Return 1
Else
Return SetError($POP3_ERROR_NOT_CONNECTED, 0, 0)
EndIf
EndFunc ;==>_POP3Disconnect

; #FUNCTION# ===================================================================
; Name ..........: _POP3List
; Description ...: Returns an array with the msg number and its size (octets)
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3List([$iMsg = -1])
; Parameter(s): .: $iMsg - Optional: (Default = -1) :
; | -1 = all
; Return Value ..: Success - array[n][2]
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert, Oscar
; Date ..........: Thu Feb 24 23:00:26 CET 2010
; Link ..........:
; Related .......: _POP3Uidl
; Example .......: No
; ==============================================================================
Func _POP3List($iMsg = -1)
If $POP3_ISAUTH Then
Local $aRet[1][2], $aTMP2
Local $sAddMsg = ""

If $iMsg <> -1 Then
$sAddMsg = " " & $iMsg
EndIf

; Send List
Local $sRet = __POP3Cmd("LIST" & $sAddMsg)
If @error Then Return SetError(@error, 0, 0)

While $iMsg = -1 And Not StringRegExp($sRet, "\r\n\.\r\n")
$sRet = $sRet & __POP3WaitTcpResponse()
If @error Then Return SetError($POP3_ERROR_NO_TCP_RESPONSE, 0, 0)
WEnd

$sRet = StringRegExpReplace($sRet, '.+?message.+\(.+\)\r\n', @LF) ; Yahoo-Support, by Oscar

; Stripping useless infos for complete listing
If $iMsg = -1 Then
$sRet = StringMid($sRet, 2, StringLen($sRet) - 6)
Else
$sRet = StringMid($sRet, 1, StringLen($sRet) - 2)
EndIf

Local $aTMP = StringSplit(StringStripCR($sRet), @LF)

Local $iE = UBound($aTMP)
ReDim $aRet[$iE][2]
$aRet[0][0] = $iE - 1
For $i = 1 To $iE - 1
$aTMP2 = StringSplit($aTMP[$i], " ", 2)
$aRet[$i][0] = $aTMP2[0]
$aRet[$i][1] = $aTMP2[1]
Next

Return $aRet
EndIf
EndFunc ;==>_POP3List

; #FUNCTION# ===================================================================
; Name ..........: _POP3Noop
; Description ...: Actually, does nothing.
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3Noop()
; Parameter(s): .: -
; Return Value ..: Success - 1
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 11:22:36 CET 2010
; Remark(s) .....: The most interesting command from RFC 1939 ;)
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3Noop()
If $POP3_ISAUTH Then
; Send NOOP
__POP3Cmd("NOOP")
If @error Then Return SetError($POP3_ERROR_USER_REFUSED, 0, 0)
Return 1
EndIf
EndFunc ;==>_POP3Noop

; #FUNCTION# ===================================================================
; Name ..........: _POP3Quit
; Description ...: Validates your actions (dele for example) and stops the connection as it should.
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3Quit()
; Parameter(s): .: -
; Return Value ..: Success - 1
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 11:25:00 CET 2010
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3Quit()
If $POP3_ISAUTH Then
__POP3Cmd("QUIT")
If @error Then Return SetError(@error, 0, 0)
Return 1
Else
Return SetError($POP3_ERROR_NO_AUTH, 0, 0)
EndIf
EndFunc ;==>_POP3Quit

; #FUNCTION# ===================================================================
; Name ..........: _POP3Retr
; Description ...: Downloads the according message
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3Retr([$iMsg = -1])
; Parameter(s): .: $iMsg - Optional: (Default = -1) :
; | -1 = newest
; Return Value ..: Success - string
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 17:23:03 CET 2010
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3Retr($iMsg = -1)
If $POP3_ISAUTH Then
If $iMsg = -1 Then
Local $aStat = _POP3Stat()
If Not @error Then $iMsg = $aStat[0]
EndIf
; Send Retr
Local $sRet = __POP3Cmd("RETR " & $iMsg)
If @error Then Return SetError(@error, 0, 0)

; Downloading until final dot and cariage return.
While Not StringRegExp($sRet, "\r\n\.\r\n")
$sRet = $sRet & __POP3WaitTcpResponse()
If @error Then Return SetError($POP3_ERROR_NO_TCP_RESPONSE, 0, 0)
WEnd

Return $sRet
Else
Return SetError($POP3_ERROR_NO_AUTH, 0, 0)
EndIf
EndFunc ;==>_POP3Retr

; #FUNCTION# ===================================================================
; Name ..........: _POP3Rset
; Description ...: Withdraw changes, such as dele orders
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3Rset()
; Parameter(s): .: -
; Return Value ..: Success - 1
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 11:34:52 CET 2010
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3Rset()
If $POP3_ISAUTH Then
; Send RSET
__POP3Cmd("RSET")
If @error Then Return SetError(@error, 0, 0)
Return 1
Else
Return SetError($POP3_ERROR_NO_AUTH, 0, 0)
EndIf
EndFunc ;==>_POP3Rset

; #FUNCTION# ===================================================================
; Name ..........: _POP3Stat
; Description ...: Gets the number of messages in the pop3 account (array[1]) and the size(array[2]) in octets
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3Stat()
; Parameter(s): .: -
; Return Value ..: Success - array
; Failure - array[-1,-1]
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Fri Jan 15 09:54:17 CET 2010
; Link ..........:
; Related .......: _POP3MsgCnt
; Example .......: No
; ==============================================================================
Func _POP3Stat()
Local $aRet[2] = [-1, -1]
If $POP3_ISAUTH Then
; Send STAT
Local $sRet = __POP3Cmd("STAT")
If @error Then Return SetError(@error, 0, 0)

$sRet = StringStripWS($sRet, 3)
$aRet = StringSplit($sRet, " ", 2)
If IsArray($aRet) Then
Return $aRet
Else
Return SetError($POP3_ERROR_STAT_BADRESPONSE, 0, $aRet)
EndIf
Else
Return SetError($POP3_ERROR_NO_AUTH, 0, $aRet)
EndIf
EndFunc ;==>_POP3Stat

; #FUNCTION# ===================================================================
; Name ..........: _POP3MsgCnt
; Description ...: Returns the number of messages
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3MsgCnt()
; Parameter(s): .: -
; Return Value ..: Success - number of messages
; Failure - -1
; @ERROR -
; Author(s) .....: Thorsten Willert
; Date ..........: Fri Jan 15 09:56:20 CET 2010
; Link ..........:
; Related .......: _POP3Stat
; Example .......: NO
; ==============================================================================
Func _POP3MsgCnt()
Local $a = _POP3Stat()
Return SetError(@error, 0, $a[0])
EndFunc ;==>_POP3MsgCnt

; #FUNCTION# ===================================================================
; Name ..........: _POP3Top
; Description ...: Retreives the mail headers, and the X first lines of the message
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3Top([$iMsg = -1[, $iLines = 0]])
; Parameter(s): .: $iMsg - Optional: (Default = -1) :
; | -1 : newest
; $iLines - Optional: (Default = 0) :
; Return Value ..: Success - string
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 17:26:42 CET 2010
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3Top($iMsg = -1, $iLines = 0)
If $POP3_ISAUTH Then
If $iMsg = -1 Then
Local $aStat = _POP3Stat()
If Not @error Then $iMsg = $aStat[0]
EndIf
; Send Top
Local $sRet = __POP3Cmd("TOP " & $iMsg & " " & $iLines)
If @error Then Return SetError(@error, 0, 0)

; Downloading until final dot and cariage return.
While Not StringRegExp($sRet, "\r\n\.\r\n")
$sRet = $sRet & __POP3WaitTcpResponse()
If @error Then Return SetError($POP3_ERROR_NO_TCP_RESPONSE, 0, 0)
WEnd
Return $sRet
Else
Return SetError($POP3_ERROR_NO_AUTH, 0, 0)
EndIf
EndFunc ;==>_POP3Top

; #FUNCTION# ===================================================================
; Name ..........: _POP3Uidl
; Description ...: Same as _POP3List(), but with UIDL identifiers instead of message size.
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3Uidl([$iMsg = -1])
; Parameter(s): .: $iMsg - Optional: (Default = -1) :
; Return Value ..: Success - array[n][2]
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 16:51:30 CET 2010
; Link ..........:
; Related .......: _POP3List
; Example .......: No
; ==============================================================================
Func _POP3Uidl($iMsg = -1)
If $POP3_ISAUTH Then
Local $aRet[1][2], $aTMP2
Local $sAddMsg = ""

If $iMsg <> -1 Then $sAddMsg = " " & $iMsg

; Send List
Local $sRet = __POP3Cmd("UIDL " & $sAddMsg)
If @error Then Return SetError(@error, 0, 0)

While $iMsg = -1 And Not StringRegExp($sRet, "\r\n\.\r\n")
$sRet = $sRet & __POP3WaitTcpResponse()
If @error Then Return SetError($POP3_ERROR_NO_TCP_RESPONSE, 0, 0)
WEnd

; Stripping useless infos for complete listing
If $iMsg = -1 Then
$sRet = StringMid($sRet, 2, StringLen($sRet) - 6)
Else
$sRet = StringMid($sRet, 1, StringLen($sRet) - 2)
EndIf

Local $aTMP = StringSplit(StringStripCR($sRet), @LF)

Local $iE = UBound($aTMP)
ReDim $aRet[$iE][2]
$aRet[0][0] = $iE - 1
For $i = 1 To $iE - 1
$aTMP2 = StringSplit($aTMP[$i], " ", 2)
$aRet[$i][0] = $aTMP2[0]
$aRet[$i][1] = $aTMP2[1]
Next

Return $aRet
Else
Return SetError($POP3_ERROR_NO_AUTH, 0, 0)
EndIf
EndFunc ;==>_POP3Uidl

; #INTERNAL_USE_ONLY# ==========================================================
; Name ..........: __POP3Cmd
; Description ...:
; AutoIt Version : V3.3.2.0
; Syntax ........: __POP3Cmd($sMSg)
; Parameter(s): .: $sMSg -
; Return Value ..: Success - string
; Failure - 0
; @ERROR -
; Author(s) .....: Thorsten Willert
; Date ..........: Thu Jan 14 17:07:08 CET 2010
; ==============================================================================
Func __POP3Cmd($sMSg)
If $POP3_TRACE Then ConsoleWrite(">: " & $sMSg & @CRLF)
TCPSend($POP3_SOCKET, $sMSg & @CRLF)
If @error Then Return SetError($POP3_ERROR_USER_REFUSED, 0, 0)
Local $sRet = __POP3WaitForOK()
If @error Then Return SetError($POP3_ERROR_NO_OK_RESPONSE, 0, 0)
Return $sRet
EndFunc ;==>__POP3Cmd

; #INTERNAL_USE_ONLY# ==========================================================
; Name ..........: __POP3WaitForOK
; Description ...: Returns the server response if it starts with "+OK"
; AutoIt Version : V3.3.2.0
; Syntax ........: __POP3WaitForOK()
; Parameter(s): .: -
; Return Value ..: Success - string
; Failure - ""
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 11:50:34 CET 2010
; ==============================================================================
Func __POP3WaitForOK()
; Wait for server response.
Local $sRet
Local $T = TimerInit()
While TimerDiff($T) < $POP3_SERVER_RESPONSE_TIMEOUT
$sRet = __POP3WaitTcpResponse()
If Not @error And StringRegExp($sRet, '\+OK') Then Return StringRegExpReplace($sRet, '\+OK\s?', "")
If StringRegExp($sRet, '\-ERR\s?') Then Return SetError($POP3_ERROR_ERR_RESPONSE, 0, "")
Sleep(100)
WEnd

Return SetError($POP3_ERROR_SERVER_RESPONSE_TIMEOUT, 0, "")
EndFunc ;==>__POP3WaitForOK

; #INTERNAL_USE_ONLY# ==========================================================
; Name ..........: __POP3WaitTcpResponse
; Description ...: Returns the server response
; AutoIt Version : V3.3.2.0
; Syntax ........: __POP3WaitTcpResponse([$iTimeOut = 30000])
; Parameter(s): .: $iTimeOut - Optional: (Default = 30000) :
; Return Value ..: Success - string
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 11:51:07 CET 2010
; ==============================================================================
Func __POP3WaitTcpResponse($iTimeOut = 30000)
;Timeout to 30 s, should be enough in most cases. Overwright it if needed.
Local $sRet = ""
Local $T = TimerInit()
While TimerDiff($T) < $iTimeOut
$sRet = TCPRecv($POP3_SOCKET, 512)
If $POP3_TRACE And $sRet Then ConsoleWrite("<: " & $sRet)
If $sRet <> "" Then Return $sRet
Sleep(100)
WEnd

Return SetError($POP3_ERROR_TCPRECV_TIMEOUT, 0, 0)
EndFunc ;==>__POP3WaitTcpResponse

POP3.au3

Edit: stupid buggy autoit tags

Edit2: wow, why are things formatted in the code tags >:(

Edited by FlutterShy

Share this post


Link to post
Share on other sites

Thanks FlutterShy! It is easy to do that these functions to work with SSL. All we need is install stunnel. Configure it in that way:

[POP3 configuration] <- This is comment about what below lines apply to

client = yes ;This means that stunnel works in client mode

accept = 127.0.0.1:110 ;Port number on our machine on which stunnel will be listen to incoming non-ciphered connection. By default if POP3 is non secured 110 is the port on which it works. That could be any other free port.

connect = you.email.ssl.service.provider:995

As I describe here

To connect with POP3 ssl server we call _POP3Connect("username","pass","127.0.0.1","110"). Easy to do! :-) Remember to comment-out Example section in the beginning of the script. FlutterShy Thanks for the code, You saved me a lot of work.

Edited by hevoxer

Share this post


Link to post
Share on other sites

Here is my contribution to this UDF

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

; :collapseFolds=0:maxLineLen=80:mode=autoitscript:tabSize=8:indentSize=8:folding=indent:
;===============================================================================
; Original UDF:
; <a href='http://www.autoitscript.com/forum/index.php?showtopic=22838' class='bbc_url' title=''>http://www.autoitscript.com/forum/index.php?showtopic=22838</a>
;~ Basic functions for AU3 Scripts, based on the 1939 RFC.
;~ See <a href='http://www.ietf.org/rfc/rfc1939.txt' class='bbc_url' title='External link' rel='nofollow external'>http://www.ietf.org/rfc/rfc1939.txt</a>
;~ Include version : 0.99 (March 2006, 9th).
;~ Requires AU3 beta version 3.1.1.110 or newer.
;~ Author : Luc HENNINOT <lhenninot@nordnet.fr>
;===============================================================================
; Name ..........: _POP3.au3
; Author: .......: Thorsten Willert (thorsten [dot] willert [at] gmx [dot] de)
; Date ..........: Thu Feb 24 22:59:56 CET 2010 @778 /Internet Time/
; Version .......: 1.03
; AutoIt ........: v3.3.2.0
;===============================================================================
#include-once
#include <FileConstants.au3>
#include <array.au3>

#Region Global constants
; -- _POP3 error codes, sent by SetError. Use @error to display it. --
Global Enum $POP3_ERROR_OK = 0, _
        $POP3_ERROR, _
        $POP3_ERROR_TCPCONNECT_FAILED, _
        $POP3_ERROR_SERVER_RESPONSE_TIMEOUT, _
        $POP3_ERROR_ALREADY_CONNECTED, _
        $POP3_ERROR_NOT_CONNECTED, _
        $POP3_ERROR_NO_AUTH, _
        $POP3_ERROR_TCPRECV_TIMEOUT, _
        $POP3_ERROR_USER_REFUSED, _
        $POP3_ERROR_PASSWD_REFUSED, _
        $POP3_ERROR_ERR_RESPONSE, _
        $POP3_ERROR_NO_OK_RESPONSE, _
        $POP3_ERROR_STAT_BADRESPONSE, _
        $POP3_ERROR_NO_TCP_RESPONSE, _
        $POP3_ERROR_STAT_REFUSED, _
        $POP3_ERROR_LIST_REFUSED, _
        $POP3_ERROR_RSET_REFUSED, _
        $POP3_ERROR_RETR_REFUSED, _
        $POP3_ERROR_QUIT_REFUSED, _
        $POP3_ERROR_DELE_REFUSED, _
        $POP3_ERROR_TOP_REFUSED, _
        $POP3_ERROR_UIDL_REFUSED, _
        $POP3_ERROR_NOOP_REFUSED ; I love this one ;)

;-- _POP3 vars --
Global Const $POP3_OK = '^\+OK'; Regexp syntax
#EndRegion Global constants

#Region Global variables
Global $g__bPOP3_TRACE = True
Global $g__vPOP3_ISCONNECTED = 0
Global $g__vPOP3_ISAUTH = 0
Global $g__iPOP3_SOCKET
Global $g__iPOP3_SERVER_RESPONSE_TIMEOUT = 60000 ; 1 mn, modify it if needed
#EndRegion Global variables

#Region POP3 - Example

; _POP3_Example()

Func _POP3_Example()
    If _POP3_Connect("somenonsslemail@domain.com", "apassword") Then
        Local $aInfo = _POP3_Info()
        _ArrayDisplay($aInfo)
        _POP3_RFC_QUIT()
        _POP3_Disconnect()
    EndIf
EndFunc   ;==>_POP3_Example
#EndRegion POP3 - Example

#Region POP3 - rfc1939 Functions ; http://www.ietf.org/rfc/rfc1939.txt

; #FUNCTION# ===================================================================
; Name ..........: _POP3_RFC_DELE
; Description ...: Delete msg n-msg_number.
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3_RFC_DELE($iMsg)
; Parameter(s): .: $iMsg - msg-number
; Return Value ..: Success - server response
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 15:24:41 CET 2010
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3_RFC_DELE($iMsg)
    If $g__vPOP3_ISAUTH Then
        __POP3_Cmd("DELE " & $iMsg)
        If @error Then Return SetError(@error, 0, 0)

        Local $sRet = __POP3_WaitTcpResponse()
        If @error Then Return SetError($POP3_ERROR_NO_TCP_RESPONSE, 0, 0)
        Return $sRet
    Else
        Return SetError($POP3_ERROR_NO_AUTH, 0, 0)
    EndIf
EndFunc   ;==>_POP3_RFC_DELE

; #FUNCTION# ===================================================================
; Name ..........: _POP3_RFC_LIST
; Description ...: Returns an array with the msg number and its size (octets)
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3_RFC_LIST([$iMsg = -1])
; Parameter(s): .: $iMsg - Optional: (Default = -1) :
; | -1 = all
; Return Value ..: Success - array[n][2]
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert, Oscar
; Date ..........: Thu Feb 24 23:00:26 CET 2010
; Link ..........:
; Related .......: _POP3_RFC_UIDL
; Example .......: No
; ==============================================================================
Func _POP3_RFC_LIST($iMsg = -1)
    If $g__vPOP3_ISAUTH Then
        Local $aRet[1][2], $aTMP2
        Local $sAddMsg = ""

        If $iMsg <> -1 Then
            $sAddMsg = " " & $iMsg
        EndIf

        ; Send List
        Local $sRet = __POP3_Cmd("LIST" & $sAddMsg)
        If @error Then Return SetError(@error, 0, 0)

        While $iMsg = -1 And Not StringRegExp($sRet, "\r\n\.\r\n")
            $sRet = $sRet & __POP3_WaitTcpResponse()
            If @error Then Return SetError($POP3_ERROR_NO_TCP_RESPONSE, 0, 0)
        WEnd

        $sRet = StringRegExpReplace($sRet, '.+?message.+\(.+\)\r\n', @LF) ; Yahoo-Support, by Oscar

        ; Stripping useless infos for complete listing
        If $iMsg = -1 Then
            $sRet = StringMid($sRet, 2, StringLen($sRet) - 6)
        Else
            $sRet = StringMid($sRet, 1, StringLen($sRet) - 2)
        EndIf

        Local $aTMP = StringSplit(StringStripCR($sRet), @LF)

        Local $iE = UBound($aTMP)
        ReDim $aRet[$iE][2]
        $aRet[0][0] = $iE - 1
        For $i = 1 To $iE - 1
            $aTMP2 = StringSplit($aTMP[$i], " ", 2)
            $aRet[$i][0] = $aTMP2[0]
            $aRet[$i][1] = $aTMP2[1]
        Next

        Return $aRet
    EndIf
EndFunc   ;==>_POP3_RFC_LIST

; #FUNCTION# ===================================================================
; Name ..........: _POP3_RFC_NOOP
; Description ...: Actually, does nothing.
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3_RFC_NOOP()
; Parameter(s): .: -
; Return Value ..: Success - 1
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 11:22:36 CET 2010
; Remark(s) .....: The most interesting command from RFC 1939 ;)
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3_RFC_NOOP()
    If $g__vPOP3_ISAUTH Then
        ; Send NOOP
        __POP3_Cmd("NOOP")
        If @error Then Return SetError($POP3_ERROR_USER_REFUSED, 0, 0)
        Return 1
    EndIf
EndFunc   ;==>_POP3_RFC_NOOP

; #FUNCTION# ===================================================================
; Name ..........: _POP3_RFC_QUIT
; Description ...: Validates your actions (dele for example) and stops the connection as it should.
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3_RFC_QUIT()
; Parameter(s): .: -
; Return Value ..: Success - 1
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 11:25:00 CET 2010
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3_RFC_QUIT()
    If $g__vPOP3_ISAUTH Then
        __POP3_Cmd("QUIT")
        If @error Then Return SetError(@error, 0, 0)
        Return 1
    Else
        Return SetError($POP3_ERROR_NO_AUTH, 0, 0)
    EndIf
EndFunc   ;==>_POP3_RFC_QUIT

; #FUNCTION# ===================================================================
; Name ..........: _POP3_RFC_RETR
; Description ...: Downloads the according message
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3_RFC_RETR([$iMsg = -1])
; Parameter(s): .: $iMsg - Optional: (Default = -1) :
; | -1 = newest
; Return Value ..: Success - string
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 17:23:03 CET 2010
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3_RFC_RETR($iMsg = -1)
    If $g__vPOP3_ISAUTH Then
        If $iMsg = -1 Then
            Local $aStat = _POP3_RFC_STAT()
            If Not @error Then $iMsg = $aStat[0]
        EndIf
        ; Send Retr
        Local $sRet = __POP3_Cmd("RETR " & $iMsg)
        If @error Then Return SetError(@error, 0, 0)

        ; Downloading until final dot and cariage return.
        While Not StringRegExp($sRet, "\r\n\.\r\n")
            $sRet = $sRet & __POP3_WaitTcpResponse()
            If @error Then Return SetError($POP3_ERROR_NO_TCP_RESPONSE, 0, 0)
        WEnd

        Return $sRet
    Else
        Return SetError($POP3_ERROR_NO_AUTH, 0, 0)
    EndIf
EndFunc   ;==>_POP3_RFC_RETR

; #FUNCTION# ===================================================================
; Name ..........: _POP3_RFC_RSET
; Description ...: Withdraw changes, such as dele orders
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3_RFC_RSET()
; Parameter(s): .: -
; Return Value ..: Success - 1
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 11:34:52 CET 2010
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3_RFC_RSET()
    If $g__vPOP3_ISAUTH Then
        ; Send RSET
        __POP3_Cmd("RSET")
        If @error Then Return SetError(@error, 0, 0)
        Return 1
    Else
        Return SetError($POP3_ERROR_NO_AUTH, 0, 0)
    EndIf
EndFunc   ;==>_POP3_RFC_RSET

; #FUNCTION# ===================================================================
; Name ..........: _POP3_RFC_STAT
; Description ...: Gets the number of messages in the pop3 account (array[1]) and the size(array[2]) in octets
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3_RFC_STAT()
; Parameter(s): .: -
; Return Value ..: Success - array
; Failure - array[-1,-1]
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Fri Jan 15 09:54:17 CET 2010
; Link ..........:
; Related .......: _POP3_MsgCnt
; Example .......: No
; ==============================================================================
Func _POP3_RFC_STAT()
    Local $aRet[2] = [-1, -1]
    If $g__vPOP3_ISAUTH Then
        ; Send STAT
        Local $sRet = __POP3_Cmd("STAT")
        If @error Then Return SetError(@error, 0, 0)

        $sRet = StringStripWS($sRet, 3)
        $aRet = StringSplit($sRet, " ", 2)
        If IsArray($aRet) Then
            Return $aRet
        Else
            Return SetError($POP3_ERROR_STAT_BADRESPONSE, 0, $aRet)
        EndIf
    Else
        Return SetError($POP3_ERROR_NO_AUTH, 0, $aRet)
    EndIf
EndFunc   ;==>_POP3_RFC_STAT

; #FUNCTION# ===================================================================
; Name ..........: _POP3_RFC_TOP
; Description ...: Retreives the mail headers, and the X first lines of the message
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3_RFC_TOP([$iMsg = -1[, $iLines = 0]])
; Parameter(s): .: $iMsg - Optional: (Default = -1) :
; | -1 : newest
; $iLines - Optional: (Default = 0) :
; Return Value ..: Success - string
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 17:26:42 CET 2010
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3_RFC_TOP($iMsg = -1, $iLines = 0)
    If $g__vPOP3_ISAUTH Then
        If $iMsg = -1 Then
            Local $aStat = _POP3_RFC_STAT()
            If Not @error Then $iMsg = $aStat[0]
        EndIf
        ; Send Top
        Local $sRet = __POP3_Cmd("TOP " & $iMsg & " " & $iLines)
        If @error Then Return SetError(@error, 0, 0)

        ; Downloading until final dot and cariage return.
        While Not StringRegExp($sRet, "\r\n\.\r\n")
            $sRet = $sRet & __POP3_WaitTcpResponse()
            If @error Then Return SetError($POP3_ERROR_NO_TCP_RESPONSE, 0, 0)
        WEnd
        Return $sRet
    Else
        Return SetError($POP3_ERROR_NO_AUTH, 0, 0)
    EndIf
EndFunc   ;==>_POP3_RFC_TOP

; #FUNCTION# ===================================================================
; Name ..........: _POP3_RFC_UIDL
; Description ...: Same as _POP3_RFC_LIST(), but with UIDL identifiers instead of message size.
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3_RFC_UIDL([$iMsg = -1])
; Parameter(s): .: $iMsg - Optional: (Default = -1) :
; Return Value ..: Success - array[n][2]
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 16:51:30 CET 2010
; Link ..........:
; Related .......: _POP3_RFC_LIST
; Example .......: No
; ==============================================================================
Func _POP3_RFC_UIDL($iMsg = -1)
    If $g__vPOP3_ISAUTH Then
        Local $aRet[1][2], $aTMP2
        Local $sAddMsg = ""

        If $iMsg <> -1 Then $sAddMsg = " " & $iMsg

        ; Send List
        Local $sRet = __POP3_Cmd("UIDL " & $sAddMsg)
        If @error Then Return SetError(@error, 0, 0)

        While $iMsg = -1 And Not StringRegExp($sRet, "\r\n\.\r\n")
            $sRet = $sRet & __POP3_WaitTcpResponse()
            If @error Then Return SetError($POP3_ERROR_NO_TCP_RESPONSE, 0, 0)
        WEnd

        ; Stripping useless infos for complete listing
        If $iMsg = -1 Then
            $sRet = StringMid($sRet, 2, StringLen($sRet) - 6)
        Else
            $sRet = StringMid($sRet, 1, StringLen($sRet) - 2)
        EndIf

        Local $aTMP = StringSplit(StringStripCR($sRet), @LF)

        Local $iE = UBound($aTMP)
        ReDim $aRet[$iE][2]
        $aRet[0][0] = $iE - 1
        For $i = 1 To $iE - 1
            $aTMP2 = StringSplit($aTMP[$i], " ", 2)
            $aRet[$i][0] = $aTMP2[0]
            $aRet[$i][1] = $aTMP2[1]
        Next

        Return $aRet
    Else
        Return SetError($POP3_ERROR_NO_AUTH, 0, 0)
    EndIf
EndFunc   ;==>_POP3_RFC_UIDL

#EndRegion POP3 - rfc1939 Functions ; http://www.ietf.org/rfc/rfc1939.txt

#Region POP3 - #INTERNAL_USE_ONLY#
; #INTERNAL_USE_ONLY# ==========================================================
; Name ..........: __POP3_Cmd
; Description ...:
; AutoIt Version : V3.3.2.0
; Syntax ........: __POP3_Cmd($sMSg)
; Parameter(s): .: $sMSg -
; Return Value ..: Success - string
; Failure - 0
; @ERROR -
; Author(s) .....: Thorsten Willert
; Date ..........: Thu Jan 14 17:07:08 CET 2010
; ==============================================================================
Func __POP3_Cmd($sMSg)
    If $g__bPOP3_TRACE Then _ConsoleDebug(">: " & $sMSg & @CRLF)
    TCPSend($g__iPOP3_SOCKET, $sMSg & @CRLF)
    If @error Then Return SetError($POP3_ERROR_USER_REFUSED, 0, 0)
    Local $sRet = __POP3_WaitForOK()
    If @error Then Return SetError($POP3_ERROR_NO_OK_RESPONSE, 0, 0)
    Return $sRet
EndFunc   ;==>__POP3_Cmd

; #INTERNAL_USE_ONLY# ==========================================================
; Name ..........: __POP3_WaitForOK
; Description ...: Returns the server response if it starts with "+OK"
; AutoIt Version : V3.3.2.0
; Syntax ........: __POP3_WaitForOK()
; Parameter(s): .: -
; Return Value ..: Success - string
; Failure - ""
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 11:50:34 CET 2010
; ==============================================================================
Func __POP3_WaitForOK()
    ; Wait for server response.
    Local $sRet
    Local $T = TimerInit()
    While TimerDiff($T) < $g__iPOP3_SERVER_RESPONSE_TIMEOUT
        $sRet = __POP3_WaitTcpResponse()
        If Not @error And StringRegExp($sRet, '\+OK') Then Return StringRegExpReplace($sRet, '\+OK\s?', "")
        If StringRegExp($sRet, '\-ERR\s?') Then Return SetError($POP3_ERROR_ERR_RESPONSE, 0, "")
        Sleep(100)
    WEnd

    Return SetError($POP3_ERROR_SERVER_RESPONSE_TIMEOUT, 0, "")
EndFunc   ;==>__POP3_WaitForOK

; #INTERNAL_USE_ONLY# ==========================================================
; Name ..........: __POP3_WaitTcpResponse
; Description ...: Returns the server response
; AutoIt Version : V3.3.2.0
; Syntax ........: __POP3_WaitTcpResponse([$iTimeOut = 30000])
; Parameter(s): .: $iTimeOut - Optional: (Default = 30000) :
; Return Value ..: Success - string
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 11:51:07 CET 2010
; ==============================================================================
Func __POP3_WaitTcpResponse($iTimeOut = 30000)
    ;Timeout to 30 s, should be enough in most cases. Overwright it if needed.
    Local $sRet = ""
    Local $T = TimerInit()
    While TimerDiff($T) < $iTimeOut
        $sRet = TCPRecv($g__iPOP3_SOCKET, 512)
        If $g__bPOP3_TRACE And $sRet Then _ConsoleDebug("<: " & $sRet)
        If $sRet <> "" Then Return $sRet
        Sleep(100)
    WEnd

    Return SetError($POP3_ERROR_TCPRECV_TIMEOUT, 0, 0)
EndFunc   ;==>__POP3_WaitTcpResponse

#EndRegion POP3 - #INTERNAL_USE_ONLY#

#Region POP3 - UDF Function
; #FUNCTION# ===================================================================
; Name ..........: _POP3_Info
; Description ...: Returns an array with the specified informations about all mails
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3_Info([$vInfo = ""])
; Parameter(s): .: $vInfo - Optional: (Default = "") : string or array
; Return Value ..: Success - array (Default: array[date,from,to,subject])
; Failure - 0
; @ERROR -
; Author(s) .....: Thorsten Willert
; Date ..........: Fri Jan 15 18:17:54 CET 2010
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3_Info($vInfo = "")
    If $g__vPOP3_ISAUTH Then
        Local $vInf
        If $vInfo = "" Then
            Local $vInf[4] = ["Date", "From", "To", "Subject"]
        ElseIf IsArray($vInfo) Then
            $vInf = $vInfo
        Else
            Local $vInf[1] = [$vInfo]
        EndIf

        Local $iCnt = _POP3_MsgCnt()
        If @error Then Return SetError(@error, 0, 0)

        Local $sTMP, $aTMP
        Local $iInf = UBound($vInf)

        If $iCnt > 0 Then
            Local $aRet[$iCnt + 1][$iInf]
            $aRet[0][0] = $iCnt
            For $i = 1 To $iCnt
                $sTMP = _POP3_RFC_TOP($i)
                If @error Then Return SetError(@error, 0, 0)
                For $j = 0 To $iInf - 1
                    $aTMP = StringRegExp($sTMP, '(?i)\n' & $vInf[$j] & ':\s*(.*?)\r', 3)
                    If Not @error Then $aRet[$i][$j] = $aTMP[0]
                Next
            Next
            Return $aRet
        EndIf

        Return SetError($POP3_ERROR, 0, 0)
    Else
        Return SetError($POP3_ERROR_NO_AUTH, 0, 0)
    EndIf
EndFunc   ;==>_POP3_Info

; #FUNCTION# ===================================================================
; Name ..........: _POP3_Connect
; Description ...: Conects to the according pop3 server.
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3_Connect($sLogin, $sPasswd[, $sServer = ""[, $iPort = 110]])
; Parameter(s): .: $sLogin -
; $sPasswd -
; $sServer - Optional: (Default = "") : pop3 server
; $iPort - Optional: (Default = 110) :
; Return Value ..: Success - 1
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Fri Jan 15 18:37:29 CET 2010
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3_Connect($sLogin, $sPasswd, $sServer = "", $iPort = 110)
    If $g__vPOP3_ISCONNECTED = 0 Then

        If $iPort = 995 Then
            _ConsoleDebugError("_POP3_Connect: Error: SSL not supported ..." & @CRLF)
            Return SetError(1, 0, 0)
        EndIf

        If Not StringInStr($sServer, ".") Then
            Local $aTMP = StringRegExp($sLogin, '.*?@(.*?)\.(.*)', 3)
            If UBound($aTMP) Then
                $sServer = $aTMP[0]
                Local $sD = "." & $aTMP[1]
            Else
                _ConsoleDebugError("_POP3_Connect: Error: Can't find domain in login-name." & @CRLF)
                Return SetError(1, 0, 0)
            EndIf
            Select
                Case Ping("pop3." & $sServer & $sD, 2000)
                    $sServer = "pop3." & $sServer & $sD
                Case Ping("pop." & $sServer & $sD, 2000)
                    $sServer = "pop." & $sServer & $sD
                Case Ping("pop3." & $sServer & ".com", 2000)
                    $sServer = "pop3." & $sServer & ".com"
                Case Ping("pop." & $sServer & ".com", 2000)
                    $sServer = "pop." & $sServer & ".com"
                Case Else
                    _ConsoleDebugError("_POP3_Connect: Error: Can't find POP3-server." & @CRLF)
                    Return SetError(1, 0, 0)
            EndSelect
        EndIf

        TCPStartup()

        ; Basic name to IP conversion
        _ConsoleDebug("_POP3_Connect: connecting to: (" & $sServer & ") ")
        If StringRegExp($sServer, "[a-zA-Z]") Then $sServer = TCPNameToIP($sServer)
        _ConsoleDebug($sServer & ":" & $iPort & @CRLF)
        $g__iPOP3_SOCKET = TCPConnect($sServer, $iPort)
        If @error Then
            $g__vPOP3_ISCONNECTED = 0
            _ConsoleDebugError("_POP3_Connect: Error: " & @error & @CRLF)
            Return SetError($POP3_ERROR_TCPCONNECT_FAILED, 0, 0)
        Else
            $g__vPOP3_ISCONNECTED = 1
        EndIf

        ; We need a first OK from pop3 server
        __POP3_WaitForOK()
        If @error Then Return SetError($POP3_ERROR_NO_OK_RESPONSE, 0, 0)

        ; Send user
        __POP3_Cmd("USER " & $sLogin)
        If @error Then Return SetError(@error, 0, 0)

        ; Send passwd
        __POP3_Cmd("PASS " & $sPasswd)
        If @error Then Return SetError(@error, 0, 0)

        $g__vPOP3_ISAUTH = 1
        Return 1
    Else
        Return SetError($POP3_ERROR_ALREADY_CONNECTED, 0, 0)
    EndIf
EndFunc   ;==>_POP3_Connect

; #FUNCTION# ===================================================================
; Name ..........: _POP3_Disconnect
; Description ...: Shuts down connection.
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3_Disconnect()
; Parameter(s): .: -
; Return Value ..: Success - 1
; Failure - 0
; @ERROR -
; Author(s) .....: Luc HENNINOT, Thorsten Willert
; Date ..........: Thu Jan 14 11:15:16 CET 2010
; Remark(s) .....: Use _POP3_RFC_QUIT to exit !!
; Link ..........:
; Related .......:
; Example .......: No
; ==============================================================================
Func _POP3_Disconnect()
    If $g__vPOP3_ISCONNECTED <> 0 Then
        TCPCloseSocket($g__iPOP3_SOCKET)
        TCPShutdown()
        $g__vPOP3_ISCONNECTED = 0
        Return 1
    Else
        Return SetError($POP3_ERROR_NOT_CONNECTED, 0, 0)
    EndIf
EndFunc   ;==>_POP3_Disconnect


; #FUNCTION# ===================================================================
; Name ..........: _POP3_MsgCnt
; Description ...: Returns the number of messages
; AutoIt Version : V3.3.2.0
; Syntax ........: _POP3_MsgCnt()
; Parameter(s): .: -
; Return Value ..: Success - number of messages
; Failure - -1
; @ERROR -
; Author(s) .....: Thorsten Willert
; Date ..........: Fri Jan 15 09:56:20 CET 2010
; Link ..........:
; Related .......: _POP3_RFC_STAT
; Example .......: NO
; ==============================================================================
Func _POP3_MsgCnt()
    Local $a = _POP3_RFC_STAT()
    Return SetError(@error, 0, $a[0])
EndFunc   ;==>_POP3_MsgCnt

#EndRegion POP3 - UDF Function

#Region POP3 - mLipok Contributrion
Func _POP3_SaveToFile($iMsg, $sEML_FileFullPath)
    Local $sEML = _POP3_RFC_RETR($iMsg)
    If Not @error Then
        _ConsoleDebug(StringStripCR($sEML) & @CR)
        Local $hEML_File = FileOpen($sEML_FileFullPath, $FO_OVERWRITE + $FO_CREATEPATH + $FO_UTF8_FULL)
        FileWrite($hEML_File, $sEML)
        FileClose($hEML_File)
    Else

    EndIf
EndFunc   ;==>_POP3_SaveToFile

Func _ConsoleDebugError($sText)
    #forceref $sText
    If _ConsoleDebugSetup() Then
        ConsoleWriteError("Retr command failed" & @CR)
    EndIf
EndFunc   ;==>_ConsoleDebugError

Func _ConsoleDebug($sText)
    #forceref $sText
    If _ConsoleDebugSetup() Then
        ConsoleWrite("Retr command failed" & @CR)
    EndIf
EndFunc   ;==>_ConsoleDebug

Func _ConsoleDebugSetup($bDebug = Default)
    Local Static $bDebugStatic = False
    If $bDebug <> Default Then
        If $bDebug <> True Then
            $bDebugStatic = True
        Else
            $bDebugStatic = False
        EndIf
    EndIf
    Return $bDebugStatic
EndFunc   ;==>_ConsoleDebugSetup

Func _EML_Subject($sEML_Content)
    Local $aSubject = StringRegExp($sEML_Content, '(?i)(?:\A|\R)Subject: (\V+)', 3)
    If UBound($aSubject) Then
        Return SetError(0, 0, $aSubject[0])
    EndIf
    Return SetError(1, 0, '')
EndFunc   ;==>_EML_Subject

Func _EML_ThreadTopic($sEML_Content)
    Local $aSubject = StringRegExp($sEML_Content, '(?i)(?:\A|\R)Thread-Topic: (\V+)', 3)
    If UBound($aSubject) Then
        Return SetError(0, 0, $aSubject[0])
    EndIf
    Return SetError(1, 0, '')
EndFunc   ;==>_EML_ThreadTopic

Func _EML_From($sEML_Content)
    Local $aFrom = StringRegExp($sEML_Content, '(?i)(?:\A|\R)From: (\V+)', 3)
    If UBound($aFrom) Then
        Return SetError(0, 0, $aFrom[0])
    EndIf
    Return SetError(1, 0, '')
EndFunc   ;==>_EML_From

Func _EML_To($sEML_Content)
    Local $aTo = StringRegExp($sEML_Content, '(?i)(?:\A|\R)To: (\V+)', 3)
    If UBound($aTo) Then
        Return SetError(0, 0, $aTo[0])
    EndIf
    Return SetError(1, 0, '')
EndFunc   ;==>_EML_To

Func _EML_Date($sEML_Content)
    Local $aDate = StringRegExp($sEML_Content, '(?i)(?:\A|\R)Date: (\V+)', 3)
    If UBound($aDate) Then
        Return SetError(0, 0, $aDate[0])
    EndIf
    Return SetError(1, 0, '')
EndFunc   ;==>_EML_Date

#EndRegion POP3 - mLipok Contributrion

_pop3.zip

Edited by mLipok

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API Forum Rules *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

11

Signature last update: 2018-10-31

Share this post


Link to post
Share on other sites

I post my new modified version Here.

Edited by mLipok
link modified

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API Forum Rules *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

11

Signature last update: 2018-10-31

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...