little dll for sending email...

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

little dll for sending email...

Post by BackupUser »

Code updated For 5.20+

Restored from previous forum. Originally posted by Kale.

I've just found a neat little (15kb) dll for sending email http://www.niijel.com/db/index.htm i think its a bit hacked together but could prove useful to someone :)

Code: Select all

If OpenLibrary(0, "dbmail.dll")
  ;usage:
  ; smtpsend(server, from, to, message)
  Return1 = CallFunction(0, "smtpsend", @"smtp.server.com", @"mailto:me@purebasic.com", @"mailto:bill@microsoft.com", @"test message")
  CloseLibrary(0)
EndIf


--Kale

New to PureBasic and falling in Love! :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by LiNuX.

LOL: Die Seite kann nicht angezeigt werden.
Die gewünschte Seite ist zurzeit nicht verfügbar. Möglicherweise sind technische Schwierigkeiten aufgetreten oder Sie sollten die Browsereinstellungen überprüfen.

http://www.niijel.com/db/index.htm !!

JO ech sin deen vun Nondikass.lu :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kale.

hmmm, its under construction at the mo', it'll be back asap!

--Kale

New to PureBasic and falling in Love! :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Blue.

Hi!

The site is down... can u send it to: [url]mailto:master@mail2artist.com[/url] i want to use it thx :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Blue.

Damnit -_-:
[url]mailto:master2@mail2artist.com[/url]
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kale.

Done :)

--Kale

New to PureBasic and falling in Love! :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.

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)


--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.

Oh, note that the date is fixed here because I didn't have time to figure out how the hell PB's date commands work -- they're really weird! Just change the code to suit... :P


--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

What ?! :)

Debug FormatDate("Date: %yyyy/%mm/%dd, Time:%hh:%ii:%ss", Date())

The first is the mask, ie: how to render the date and the second is.... the Date. Wierd ?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.
Debug FormatDate("Date: %yyyy/%mm/%dd, Time:%hh:%ii:%ss", Date())

Weird?
Yeah, I think that's pretty complex just to get the date! Maybe a QuickDate () command would be a good idea -- it would just output the date in a fixed format, eg. "7 Dec 2000"...? I know I could do this in a function, but then you have to search around for the stored code instead of just typing a quick command in...

"MHO" :wink:


--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
Originally posted by Hi-Toro
Yeah, I think that's pretty complex just to get the date! Maybe a QuickDate () command would be a good idea -- it would just output the
How about a command called LazyProgrammersWhoCantTakeTenSecondsToReadTheManual_Date()?

;p

Seriously, I know the manual is sketchy in places, but the Date stuff is OK. It describes Date which in turn tells you to use FormatDate to display it, which in turn desribes the mask (and gives an example).

If Fred adds a QuickDate command, you'll only get people moan that it doesn't show the date the way they want :)

Something which might be useful is a command like LocaleDate which is either just the string to use with FormatDate, or an equivalent to James's QuickDate. It would of course return the date/mask in the users current locale format.


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fweil.

...,

I would prefer :

LazyProgrammersWhoCantTakeTenSecondsToReadTheManual("Date")

which is more generic !



Francois Weil
14, rue Douer
F64100 Bayonne
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kale.

lol :)

--Kale

New to PureBasic and falling in Love! :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Berikco.

ROFL

Regards,

Berikco

http://www.benny.zeb.be/purebasic.htm
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.

Yes, I was so lazy I ported the above code from Blitz to PB just because someone was looking for a way to send email... but IMHO Basic is supposed to be straightforward! Why not have a default format if you call QuickDate (or whatever), and offer to allow it to be changed using that complex method (ie. FormatDate("Date: %yyyy/%mm/%dd, Time:%hh:%ii:%ss", Date()))? Not that it really matters that much -- I don't think I've ever needed to print the date in a program, except in the above example! Just thought I'd offer some constructive criticism and a suggestion.


--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
Post Reply