Code : Tout sélectionner
Opt("MustDeclareVars", 1)
Dim $i
If _SendSMTPMail("localhost", _
"me@host.com", _
"youfirstbuddy@host.com, yoursecondbuddy@host.com, yourthirdbuddy@host.com", _
"Test email", _
"This is a test email") Then
MsgBox(0, "_SendSMTP", "Message sent!")
EndIf
Func _SendSMTPMail($sServer, $sSender, $sRecipients, $sSubject, $sBody)
Local $hSocket
Local $sData
Local $aRcptList
Local $nRcptOK = 0
Local $bSent = False
Local $n
; Start TCP services
If Not TCPStartup() Then Return False
; Connect to SMTP server
$hSocket = TCPConnect(TCPNameToIP($sServer), 25)
If $hSocket <> -1 Then
Sleep(100)
; Read 220 response from server
$sData = TCPRecv($hSocket, 1024)
If StringLeft($sData, 3) = "220" Then
Sleep(100)
$sData = "HELO " & @ComputerName & @CRLF
; Send HELO to server
If TCPSend($hSocket, $sData) > 0 Then
Sleep(100)
; Receive 250 response from server
$sData = TCPRecv($hSocket, 1024)
If StringLeft($sData, 3) = "250" Then
Sleep(100)
$sData = "MAIL FROM: <" & $sSender & ">" & @CRLF
; Send MAIL FROM command to server
If TCPSend($hSocket, $sData) > 0 Then
Sleep(100)
; Receive 250 response from server
$sData = TCPRecv($hSocket, 1024)
If StringLeft($sData, 3) = "250" Then
$aRcptList = StringSplit($sRecipients, ",")
For $n = 1 To $aRcptList[0]
Sleep(100)
$sData = "RCPT TO: <" & StringStripWS($aRcptList[$n], 8) & ">" & @CRLF
; Send RCPT TO command to server
If TCPSend($hSocket, $sData) > 0 Then
Sleep(100)
; Receive 250 response from server
$sData = TCPRecv($hSocket, 1024)
If StringLeft($sData, 3) = "250" Then $nRcptOK += 1
EndIf
Next
; Continue if at least 1 recipient was accepted
If $nRcptOK > 0 Then
Sleep(100)
$sData = "DATA" & @CRLF
; Send DATA command to server
If TCPSend($hSocket, $sData) > 0 Then
Sleep(100)
; Receive 354 response from server
$sData = TCPRecv($hSocket, 1024)
If StringLeft($sData, 3) = "354" Then
Sleep(100)
$sData = StringFormat("To: %s\r\nFrom: %s\r\n" & _
"Subject: %s\r\n" & _
"Date: %02d/%02d/%d %02d:%02d:%02d\r\n", _
$sRecipients, $sSender, $sSubject, _
@MDAY, @MON, @YEAR, @HOUR, @MIN, @SEC)
; Send message HEADER
If TCPSend($hSocket, $sData) > 0 Then
Sleep(100)
$sData = $sBody & @CRLF
; Send message BODY
If TCPSend($hSocket, $sData) > 0 Then
Sleep(100)
$sData = @CRLF & "." & @CRLF
; Send end of message indicator
If TCPSend($hSocket, $sData) > 0 Then
Sleep(100)
; Receive 250 response from server
$sData = TCPRecv($hSocket, 1024)
If StringLeft($sData, 3) = "250" Then $bSent = True
EndIf; Send end of message indicator
EndIf; Send message BODY
EndIf; Send message HEADER
EndIf; Receive 354 response from server
EndIf; Send DATA command to server
EndIf; Continue if at least 1 recipient was accepted
EndIf; Receive 250 response from server
EndIf; Send MAIL FROM command to server
EndIf; Receive 250 response from server
EndIf; Send HELO to server
EndIf; Read 220 response from server
; Close TCP socket
TCPCloseSocket($hSocket)
EndIf
; Shutdown TCP services
TCPShutdown()
Return $bSent
EndFunc