Jump to content

Help with SetError()


Recommended Posts

Hi all,

I'm working on a UDF version of the SMTP mailsend script posted by Asimzameer in Scripts and Scraps. I've almost got a simple but functioning version working......except the SetError function doesn't seem to work. I'm sure I'm overlooking something obvious here. Can anyone see what it is?

;===============================================================================
;
; Function Name:    _INetSmtpMail()
; Description:    Sends an email using SMTP over TCP IP.
; Parameter(s):  $s_SmtpServer  - SMTP server to be used for sending email
;                  $s_FromName      - Name of sender
;                  $s_FromAddress   - eMail address of sender
;                  $s_ToAddress - Address that email is to be sent to
;                  $s_Subject       - Subject of eMail
;                   $as_Body        - Single dimension array containing the body of eMail as strings
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                  On Failure - 0  and sets
;                                           @ERROR = 1  -   Invalid Parameters
;                                           @ERROR = 2  -   Unable to start TCP
;                                           @ERROR = 3  -   Unable to resolve IP
;                                           @ERROR = 4  -   Unable to create socket
;                                           @ERROR = 5  -   Data send error
; Authors:      Original function to send email via TCP - asimzameer
;                   Conversion to UDF                       - Walkabout
;
;===============================================================================
Func _INetSmtpMail($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body)
    
    Local $i_ReturnErrorCode
    Local $i_LocalErrorCode
    Local $v_Socket
    Local $s_IPAddress
    Local $i_SendReturn
    Local $i_Count
    Local $s_Send[10]
    
    If $s_SmtpServer = "" Or $s_FromAddress = "" Or $s_ToAddress = "" Or $s_FromName = "" Or StringLen($s_FromName) > 256 Then
        SetError(1)
        Return 0
    EndIf
    $i_LocalErrorCode = TCPStartup()
    If $i_LocalErrorCode = 0 Then
        SetError(2)
        Return 0
    EndIf
    StringRegExp($s_SmtpServer, "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)")
    If @extended Then
        $s_IPAddress = $s_SmtpServer
    Else
        $s_IPAddress = TCPNameToIP($s_SmtpServer)
    EndIf
    If $s_IPAddress = "" Then
        TCPShutdown()
        SetError(3)
        Return 0
    EndIf
    $v_Socket = TCPConnect($s_IPAddress, 25)
    If $v_Socket = -1 Then
        TCPShutdown()
        SetError(4)
        Return (0)
    EndIf
    Sleep(100)
    $s_Send[0] = "HELO " & $s_FromAddress & @CRLF
    $s_Send[1] = "MAIL FROM: <" & $s_FromAddress & ">" & @CRLF
    $s_Send[2] = "RCPT TO: <" & $s_ToAddress & ">" & @CRLF
    $s_Send[3] = "DATA" & @CRLF
    $s_Send[4] = "From:" & $s_FromName & "< " & $s_FromAddress & " >" & @CRLF
    $s_Send[5] = "To:" & $s_ToAddress & @CRLF
    $s_Send[6] = "Subject:" & $s_Subject & @CRLF
;$s_Send[7] = "Sender: AutoIt3" & @CRLF
    $s_Send[7] = "Mime-Version: 1.0" & @CRLF
    $s_Send[8] = "Content-Type: text/plain; charset=US-ASCII" & @CRLF
    $s_Send[9] = @CRLF
    
    For $i_Count = 0 To UBound($s_Send) - 1
        $i_SendReturn = TCPSend($v_Socket, $s_Send[$i_Count])
        If $i_SendReturn = 0 Then
            TCPCloseSocket($v_Socket)
            TCPShutdown()
            SetError(5)
            Return 0
        EndIf
        Sleep(100)
        $s_Receive = TCPRecv($v_Socket, 1000)
    Next
    Sleep(100)
    For $i_Count = 0 To UBound($as_Body) - 1
        $i_SendReturn = TCPSend($v_Socket, $as_Body[$i_Count] & @CRLF)
        Sleep(100)
        If $i_SendReturn = 0 Then
            TCPCloseSocket($v_Socket)
            TCPShutdown()
            SetError(5)
            Return 0
        EndIf
        $s_Receive = TCPRecv($v_Socket, 1000)
        Sleep(100)
    Next
    $i_SendReturn = TCPSend($v_Socket, @CRLF & "." & @CRLF)
    If $i_SendReturn = 0 Then
        TCPCloseSocket($v_Socket)
        TCPShutdown()
        SetError(5)
        Return 0
    EndIf
    Sleep(100)
    TCPCloseSocket($v_Socket)
    TCPShutdown()
    Return 1
EndFunc  ;==>_INetSmtpMail

A down and dirty wrapper is:

#include <INetSmptMail.au3>
$s_SmtpServer = "mysmtpserver.com.au"
$s_FromName = "My Name"
$s_FromAddress = "From eMail Address"
$s_ToAddress = "To eMail Address"
$s_Subject = "My Test UDF"
Dim $s_Body[2]
$s_Body[0] = "Testing the new email udf"
$s_Body[1] = "Second Line"
msgbox(0,"email",_INetSmtpMail("", $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $s_Body))
$err = @error
msgbox(0,"error",$err)

You can see that I've deliberately substituted one of the parameters to try to force an error, but I keep getting "0" back for the Return and "0" for the error.

Any help anyone can give me will be greatly appreciated.

Ta

Walkabout

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...