Here we go. I just ported this from some Blitz source I wrote (based on Mark Sibly's simple original version)... read the comments in the source before trying to run it! There are two versions here, one debug version that prints all the commands/responses to a console, and another that just sends without any feedback.
Code: Select all
; -----------------------------------------------------------------------
; PBMail -- james @ hi-toro.com -- public domain source code...
; -----------------------------------------------------------------------
; IMPORTANT!!! Fill in the details below, then scroll down to the demo section at
; the bottom, and enter the email address you want send a test message to...
; -----------------------------------------------------------------------
; Configuration...
; -----------------------------------------------------------------------
InitNetwork ()
Global mailhost$
Global mailer$
Global mailname$
Global mailfrom$
; IMPORTANT!!! You MUST fill in these details or the program won't work! You can get
; your SMTP server name by going into Outlook Express, choosing the Tools/Accounts
; menu, double-clicking the name of your main email account, and clicking on
; the Server tab. Copy the name of the SMTP server ("Outgoing mail") into the
; first item below...
; Obviously, if you use this is a program for others to use, you'll have to provide
; a way for them to configure this stuff!
mailhost$ = "" ; Enter your SMTP host name (eg. smtp.blahblahblah.net)
mailer$ = "PBMail" ; Name of this program (change to suit yourself)
mailname$ = "" ; Insert your real name here (eg. John Wayne)
mailfrom$ = "" ; Insert your own email address here (this is the FROM address)
; -----------------------------------------------------------------------
; The SendMail function...
; -----------------------------------------------------------------------
; PARAMETERS:
; mailto$ -- the email address you want to send a message to...
; subject$ -- the subject line of the email...
; message$ -- the message you want to send. Separate new lines by using the "|" character, like this:
; Example message string: "Hello||This is a test,|hope you like it!" will look like this:
; -----------------------------------------------------------------------
;Hello
;
;This is a test,
;hope you like it!
; -----------------------------------------------------------------------
Procedure.s SendMail (mailto$, subject$, message$)
If mailhost$ = ""
MessageRequester (mailer$, "Error -- no SMTP host defined!", #MB_ICONWARNING)
End
EndIf
If mailer$ = ""
MessageRequester (mailer$, "Error -- no program name defined!", #MB_ICONWARNING)
End
EndIf
If mailname$ = ""
MessageRequester (mailer$, "Error -- no sender name defined!", #MB_ICONWARNING)
End
EndIf
If mailfrom$ = ""
MessageRequester (mailer$, "Error -- no sender email address defined!", #MB_ICONWARNING)
End
EndIf
message$ = ReplaceString (message$, "|", Chr (13) + Chr (10))
error$ = "Email sent!"
t = OpenNetworkConnection (mailhost$, 25)
If t
; ---------------------------------------------------------------------
; Service available?
; ---------------------------------------------------------------------
code$ = Space (255)
response = ReceiveNetworkData (t, code$, 255))
code3$ = Left (code$, 3)
If code3$ "220"
If code3$ = "421"
error$ = "Service not available"
Else
error$ = code$
EndIf
Goto abortSMTP
EndIf
; ---------------------------------------------------------------------
; Say "Hi"
; ---------------------------------------------------------------------
send$ = "HELO "+ mailer$ + Chr (10)
SendNetworkData (t, send$, Len (send$))
response = ReceiveNetworkData (t, code$, 255))
code3$ = Left (code$, 3)
If code3$ "250"
error$ = code$
Goto abortSMTP
EndIf
; ---------------------------------------------------------------------
; Tell server who's sending
; ---------------------------------------------------------------------
send$ = "MAIL FROM: " + Chr (10)
SendNetworkData (t, send$, Len (send$))
response = ReceiveNetworkData (t, code$, 255))
code3$ = Left (code$, 3)
If code3$ "250"
If code3$ = "501"
error$ = "Email sender not specified (or invalid address)"
Else
error$ = code$
EndIf
Goto abortSMTP
EndIf
; ---------------------------------------------------------------------
; Tell server who it's going to
; ---------------------------------------------------------------------
send$ = "RCPT TO: " + Chr (10)
SendNetworkData (t, send$, Len (send$))
response = ReceiveNetworkData (t, code$, 255))
code3$ = Left (code$, 3)
If code3$ "250"
If code3$ = "501"
error$ = "Email recipient not specified (or invalid address)"
Else
error$ = code$
EndIf
Goto abortSMTP
EndIf
; ---------------------------------------------------------------------
; Email data
; ---------------------------------------------------------------------
SendNetworkData (t, "DATA" + Chr (10), 5)
response = ReceiveNetworkData (t, code$, 255))
code3$ = Left (code$, 3)
If code3$ "354"
error$ = code$
Goto abortSMTP
EndIf
; ---------------------------------------------------------------------
; Headers
; ---------------------------------------------------------------------
send$ = "Date: " + "32 Octember 1901") + Chr (10) ; PB's date commands are really weird!!!
SendNetworkData (t, send$, Len (send$))
send$ = "From: " + mailname$ + " " + Chr (10)
SendNetworkData (t, send$, Len (send$))
send$ = "To: " + mailto$ + " " + Chr (10)
SendNetworkData (t, send$, Len (send$))
send$ = "Subject: " + subject$ + Chr (10)
SendNetworkData (t, send$, Len (send$))
send$ = "X-Mailer: " + mailer$ + Chr (10)
SendNetworkData (t, send$, Len (send$))
; ---------------------------------------------------------------------
; Email message
; ---------------------------------------------------------------------
SendNetworkData (t, message$ + Chr (10), Len (message$) + 1)
; ---------------------------------------------------------------------
; End of message
; ---------------------------------------------------------------------
SendNetworkData (t, Chr (10), 1)
SendNetworkData (t, "." + Chr (10), 2)
response = ReceiveNetworkData (t, code$, 255))
code3$ = Left (code$, 3)
If code3$ "250"
error$ = code$
EndIf
; ---------------------------------------------------------------------
; Say "ciao"
; ---------------------------------------------------------------------
SendNetworkData (t, "QUIT" + Chr (10), 5)
response = ReceiveNetworkData (t, code$, 255))
code3$ = Left (code$, 3)
If code3$ "221"
error$ = code$
EndIf
; ---------------------------------------------------------------------
; Return error message, if any
; ---------------------------------------------------------------------
abortSMTP:
CloseNetworkConnection (t)
If error$ = ""
error$ = "Timeout error"
EndIf
ProcedureReturn error$
Else
; ---------------------------------------------------------------------
; Oops. Forgot to go online (or server isn't there)
; ---------------------------------------------------------------------
ProcedureReturn "Failed to connect to server at " + mailhost$
EndIf
EndProcedure
; -----------------------------------------------------------------------
; DEBUG version (exactly the same, but opens a console and prints info)...
; -----------------------------------------------------------------------
Procedure.s SendMailDebug (mailto$, subject$, message$)
If mailhost$ = ""
MessageRequester (mailer$, "Error -- no SMTP host defined!", #MB_ICONWARNING)
End
EndIf
If mailer$ = ""
MessageRequester (mailer$, "Error -- no program name defined!", #MB_ICONWARNING)
End
EndIf
If mailname$ = ""
MessageRequester (mailer$, "Error -- no sender name defined!", #MB_ICONWARNING)
End
EndIf
If mailfrom$ = ""
MessageRequester (mailer$, "Error -- no sender email address defined!", #MB_ICONWARNING)
End
EndIf
OpenConsole ()
ConsoleColor (15, 0): PrintN (""): PrintN ("Sending..."): PrintN (""): ConsoleColor (7, 0)
message$ = ReplaceString (message$, "|", Chr (13) + Chr (10))
error$ = "Email sent!"
t = OpenNetworkConnection (mailhost$, 25)
If t
; ---------------------------------------------------------------------
; Service available?
; ---------------------------------------------------------------------
PrintN (""): PrintN ("About to check for service...")
code$ = Space (255)
response = ReceiveNetworkData (t, code$, 255))
ConsoleColor (2, 0): PrintN (Left (code$, response - 2)): ConsoleColor (7, 0)
code3$ = Left (code$, 3)
If code3$ "220"
If code3$ = "421"
error$ = "Service not available"
Else
error$ = code$
EndIf
Goto abortSMTPDebug
EndIf
; ---------------------------------------------------------------------
; Say "Hi"
; ---------------------------------------------------------------------
PrintN (""): PrintN ("About to say hello...")
send$ = "HELO " + mailer$ + Chr (10)
SendNetworkData (t, send$, Len (send$))
response = ReceiveNetworkData (t, code$, 255))
ConsoleColor (2, 0): PrintN (Left (code$, response - 2)): ConsoleColor (7, 0)
code3$ = Left (code$, 3)
If code3$ "250"
error$ = code$
Goto abortSMTPDebug
EndIf
; ---------------------------------------------------------------------
; Tell server who's sending
; ---------------------------------------------------------------------
PrintN (""): PrintN ("About to identify sender...")
send$ = "MAIL FROM: " + Chr (10)
SendNetworkData (t, send$, Len (send$))
response = ReceiveNetworkData (t, code$, 255))
ConsoleColor (2, 0): PrintN (Left (code$, response - 2)): ConsoleColor (7, 0)
code3$ = Left (code$, 3)
If code3$ "250"
If code3$ = "501"
error$ = "Email sender not specified (or invalid address)"
Else
error$ = code$
EndIf
Goto abortSMTPDebug
EndIf
; ---------------------------------------------------------------------
; Tell server who it's going to
; ---------------------------------------------------------------------
PrintN (""): PrintN ("About to define recipient...")
send$ = "RCPT TO: " + Chr (10)
SendNetworkData (t, send$, Len (send$))
response = ReceiveNetworkData (t, code$, 255))
ConsoleColor (2, 0): PrintN (Left (code$, response - 2)): ConsoleColor (7, 0)
code3$ = Left (code$, 3)
If code3$ "250"
If code3$ = "501"
error$ = "Email recipient not specified (or invalid address)"
Else
error$ = code$
EndIf
Goto abortSMTPDebug
EndIf
; ---------------------------------------------------------------------
; Email data
; ---------------------------------------------------------------------
PrintN (""): PrintN ("About to give DATA command...")
SendNetworkData (t, "DATA" + Chr (10), 5)
response = ReceiveNetworkData (t, code$, 255))
ConsoleColor (2, 0): PrintN (Left (code$, response - 2)): ConsoleColor (7, 0)
code3$ = Left (code$, 3)
If code3$ "354"
error$ = code$
Goto abortSMTPDebug
EndIf
; ---------------------------------------------------------------------
; Headers
; ---------------------------------------------------------------------
PrintN (""): PrintN ("About to send headers...")
send$ = "Date: " + "32 Octember 1901") + Chr (10) ; PB's date commands are really weird!!!
SendNetworkData (t, send$, Len (send$))
send$ = "From: " + mailname$ + " " + Chr (10)
SendNetworkData (t, send$, Len (send$))
send$ = "To: " + mailto$ + " " + Chr (10)
SendNetworkData (t, send$, Len (send$))
send$ = "Subject: " + subject$ + Chr (10)
SendNetworkData (t, send$, Len (send$))
send$ = "X-Mailer: " + mailer$ + Chr (10)
SendNetworkData (t, send$, Len (send$))
; ---------------------------------------------------------------------
; Email message
; ---------------------------------------------------------------------
PrintN (""): PrintN ("About to send message...")
SendNetworkData (t, message$ + Chr (10), Len (message$) + 1)
; ---------------------------------------------------------------------
; End of message
; ---------------------------------------------------------------------
PrintN (""): PrintN ("About to end message...")
SendNetworkData (t, Chr (10), 1)
SendNetworkData (t, "." + Chr (10), 2)
response = ReceiveNetworkData (t, code$, 255))
ConsoleColor (2, 0): PrintN (Left (code$, response - 2)): ConsoleColor (7, 0)
code3$ = Left (code$, 3)
If code3$ "250"
error$ = code$
EndIf
; ---------------------------------------------------------------------
; Say "ciao"
; ---------------------------------------------------------------------
PrintN (""): PrintN ("About to say goodbye...")
SendNetworkData (t, "QUIT" + Chr (10), 5)
response = ReceiveNetworkData (t, code$, 255))
ConsoleColor (2, 0): PrintN (Left (code$, response - 2)): ConsoleColor (7, 0)
code3$ = Left (code$, 3)
If code3$ "221"
error$ = code$
EndIf
; ---------------------------------------------------------------------
; Return error message, if any
; ---------------------------------------------------------------------
abortSMTPDebug:
CloseNetworkConnection (t)
If error$ = ""
error$ = "Timeout error"
PrintN (error$)
EndIf
PrintN (""): PrintN ("Press ENTER to exit..."): Input ()
CloseConsole ()
ProcedureReturn error$
Else
CloseConsole ()
; ---------------------------------------------------------------------
; Oops. Forgot to go online (or server isn't there)
; ---------------------------------------------------------------------
ProcedureReturn "Failed to connect to server at " + mailhost$
EndIf
EndProcedure
; -----------------------------------------------------------------------
; D E M O (try changing SendMailDebug to SendMail in the example below)
; -----------------------------------------------------------------------
; IMPORTANT!!! You MUST change the global variables at the top of
; this code to suit your SMTP server, etc...
; The address you want to send to...
sendto$ = ""
If sendto$ = ""
MessageRequester (mailer$, "You have to define a recipient in the DEMO section of this code!", #MB_ICONWARNING)
End
EndIf
; The message to be sent, each line separated by the "|" character...
message$ = "Hi there,||I think you'll agree, PBMail rules!||Your friend, John Wayne.|Sent via PBMail"
; Send mail, returning result as a string...
result$ = SendMailDebug (sendto$, "PBMail test message", message$)
; Show result...
MessageRequester ("PBMail", "Result: " + result$, #MB_ICONINFORMATION)
James L Boyd.