; #FUNCTION# ====================================================================================================================; Name...........: _INetSmtpMail
; Description ...: Sends an email using SMTP over TCP IP.
; Parameters ....: $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
; $s_helo - Helo identifier (default @COMPUTERNAME) sometime needed by smtp server
; $s_first - send before Helo identifier (default @CRLF) sometime needed by smtp server
; $b_trace - trace on a splash window (default 0 = no trace)
; Return values .: 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 = 5x - Cannot open SMTP session
; @ERROR = 50x - Cannot send body
; @ERROR = 5000 - Cannot close SMTP session
; Author ........: Asimzameer, Walkabout
; Modified.......: Jpm
; ===============================================================================================================================
Func _INetSmtpMail($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_helo = "", $s_first=" ", $b_trace = 0)
If $s_SmtpServer = "" Or $s_FromAddress = "" Or $s_ToAddress = "" Or $s_FromName = "" Or StringLen($s_FromName) > 256 Then Return SetError(1, 0 ,0)
If $s_helo = "" Then $s_helo = @ComputerName
If TCPStartup() = 0 Then Return SetError(2, 0 ,0)
Local $s_IPAddress, $i_Count
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()
Return SetError(3, 0 ,0)
EndIf
Local $v_Socket = TCPConnect($s_IPAddress, 25)
If $v_Socket = -1 Then
TCPShutdown()
Return SetError(4, 0 ,0)
EndIf
Local $s_Send[6], $s_ReplyCode[6] ; Return code from SMTP server indicating success
$s_Send[0] = "HELO " & $s_helo & @CRLF
If StringLeft($s_helo,5) = "EHLO " Then $s_Send[0] = $s_helo & @CRLF
$s_ReplyCode[0] = "250"
$s_Send[1] = "MAIL FROM: <" & $s_FromAddress & ">" & @CRLF
$s_ReplyCode[1] = "250"
$s_Send[2] = "RCPT TO: <" & $s_ToAddress & ">" & @CRLF
$s_ReplyCode[2] = "250"
$s_Send[3] = "DATA" & @CRLF
$s_ReplyCode[3] = "354"
Local $aResult = _Date_Time_GetTimeZoneInformation()
Local $bias = -$aResult[1]/60
Local $biasH = Int($bias)
Local $biasM = 0
If $biasH <> $bias Then $biasM = Abs($bias - $biasH) * 60
$bias = StringFormat(" (%+.2d%.2d)", $biasH, $biasM)
$s_Send[4] = "From:" & $s_FromName & "<" & $s_FromAddress & ">" & @CRLF & _
"To:" & "<" & $s_ToAddress & ">" & @CRLF & _
"Subject:" & $s_Subject & @CRLF & _
"Mime-Version: 1.0" & @CRLF & _
"Date: " & _DateDayOfWeek(@WDAY, 1) & ", " & @MDAY & " " & _DateToMonth(@MON, 1) & " " & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & $bias & @CRLF & _
"Content-Type: text/plain; charset=US-ASCII" & @CRLF & _
@CRLF
$s_ReplyCode[4] = ""
$s_Send[5] = @CRLF & "." & @CRLF
$s_ReplyCode[5] = "250"
; open stmp session
If __SmtpSend($v_Socket, $s_Send[0], $s_ReplyCode[0], $b_trace, "220", $s_first) Then Return SetError(50, 0, 0)
; send header
For $i_Count = 1 To UBound($s_Send) - 2
If __SmtpSend($v_Socket, $s_Send[$i_Count], $s_ReplyCode[$i_Count], $b_trace) Then Return SetError(50 + $i_Count, 0 ,0)
Next
; send body records (a record can be multiline : take care of a subline beginning with a dot should be ..)
For $i_Count = 0 To UBound($as_Body) - 1
; correct line beginning with a dot
If StringLeft($as_Body[$i_Count], 1) = "." Then $as_Body[$i_Count] = "." & $as_Body[$i_Count]
If __SmtpSend($v_Socket, $as_Body[$i_Count] & @CRLF, "", $b_trace) Then Return SetError(500 + $i_Count, 0, 0)
Next
; close the smtp session
$i_Count = UBound($s_Send) - 1
If __SmtpSend($v_Socket, $s_Send[$i_Count], $s_ReplyCode[$i_Count], $b_trace) Then Return SetError(5000, 0, 0)
TCPCloseSocket($v_Socket)
TCPShutdown()
Return 1
EndFunc ;==>_INetSmtpMail