Sendmail example using API

Share your advanced PureBasic knowledge/code with the community.
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.

Its not Perfect, but could be a start to a nice app.:)
Who is writing the first complete e-mail client in PB?

New example little bit down, just deleted this one tho give Leigh's server more space...
Hope he dont go down so much anymore :)

Regards,

Berikco

http://www.benny.zeb.be
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.

Berikco,

Am I wrong or hsould not the first server reply be 220 instead of 250 ?

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 Berikco.
Originally posted by fweil
Am I wrong or should not the first server reply be 220 instead of 250 ?
Oooops, good a wrote 'not perfect'
I forgot :)


Regards,

Berikco

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

Sendmail example using API

Post by BackupUser »

Code updated For 5.20+ (same As SendMail())

Restored from previous forum. Originally posted by Berikco.

Here example that checks the 220 reply , thx fweil :)
I use the RegisterWindowMessage_ API to generate my owne message , PB_TCP_MESSAGE.
So you get a message when there is data to be read.

Code: Select all

Global SMTPserver$, Sender$, Receiver$,domain$

SMTPserver$ = "192.168.4.1" ; ip SMTP server
Sender$="somebody@server.com" ; Sender e-mail
Domain$="server.com"; Sender domain, everyting after the @
Receiver$="nobody@far.west" ; receiver e-mail



#sockaddr_size = 16
#SOCK_STREAM = 1
#AF_INET = 2
#WSA_DESCRIPTIONLEN = 256
#WSA_DescriptionSize = 257
#WSA_SYS_STATUS_LEN = 128
#WSA_SysStatusSize = 129
#MAXGETHOSTSTRUCT = 1024

Structure sockaddress
  sin_family.w
  sin_port.w
  sin_addr.l
  sin_zero.s
EndStructure

Structure WSADataType
  wVersion.w
  wHighVersion.w
  szDescription.s[#WSA_DescriptionSize]
  szSystemStatus.s[#WSA_SysStatusSize]
  iMaxSockets.w
  iMaxUdpDg.w
  lpVendorInfo.l
EndStructure

#FD_READ = $1
#FD_ACCEPT = $8
#FD_CONNECT = $10
#FD_CLOSE = $20



Global Socket_Number.w
Global Read_Sock.l
Global Read_Sock_Buffer.sockaddress
Global Start_up_Data.WSADataType
Global Socket_Buffer.sockaddress
Global Read_Buffer$
Global ontvangen$
Global timer1
#timeout=10

Global crlf$
crlf$=Chr(13)+Chr(10)

Global RC.l, X.l, Bytes.l
Global RemoteIP.l
Global RemotePort.l
remoteport = 25

PB_TCP_MESSAGE = RegisterWindowMessage_("WM_TCPMESSAGE_PB")

Debug "message is :"+Str(PB_TCP_MESSAGE)

#hostent_size = 16
#receive=1
Global hwnd

hwnd= OpenWindow(1, 200, 200, 673, 455,"test" ,#PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)



Procedure Connect_winsock()
  Socket_Number.w = SOCKET_(#AF_INET, #SOCK_STREAM, 0)
  Debug "Socket_Number: " + Str(Socket_Number.w)
  If Socket_Number.w <> 0
    X = WSACleanup_()
    ProcedureReturn 0
  EndIf
EndProcedure

Procedure cleanup_winsock()
  RC = WSACleanup_()
  Debug "WSACleanup() = " + Str(RC)
EndProcedure

Procedure  Zend(zbuf$)
  Debug  zbuf$
  send=send_(Socket_Number.w, @zbuf$, Len(zbuf$), 0)
EndProcedure

Procedure receive_TCP()
  Read_Buffer$=Space(1024)
  Bytes = recv_(Socket_Number, @Read_Buffer$, 1024, 0)
  If Bytes <> 0
    ontvangen$ =  Left(Read_Buffer$, Bytes)
    Debug ontvangen$
  EndIf
EndProcedure

Procedure check_smtp(reply$)
  timer1=0
  Repeat
    WaitWindowEvent()
    If Timer1 > #timeout
      ProcedureReturn 0
    EndIf
  Until Left(ontvangen$, 4) = reply$
  ontvangen$ = ""
  ProcedureReturn 1
EndProcedure

Procedure SendMail()
  RC = WSACleanup_()
  RC = WSAStartup_($101, @Start_up_Data)
  RemoteIP = inet_addr_(SMTPserver$)
  If RemoteIP > 0
    Connect_winsock()
    If check_smtp("220 ")=1
      zend("HELO "+domain$ + crlf$ )
      If check_smtp("250 ")=1
        zend("MAIL From: " + crlf$)
        If check_smtp("250 ")
          zend("RCPT To: " + crlf$)
          If  check_smtp("250 ")
            zend("DATA"  + crlf$)
            If  check_smtp("354 ")
              zend("X-Mailer: PB testapp" + crlf$)
              zend("Date: Sat, 28 Sep 2002 22:50:23 +0100 (GMT)" + crlf$)
              zend("From: "+Sender$+ crlf$)
              zend("To: "+receiver$ + crlf$)
              zend("Subject: testing mail" + crlf$)
              zend( crlf$ )
              zend("Hello world..." + crlf$)
              zend( crlf$ + "." + crlf$)
              If check_smtp("250 ")
                zend("QUIT" + crlf$ )
                check_smtp("221 ")
                closesocket_(Socket_Number)
                timer1=0
                Repeat
                  rc= WSACleanup_()
                  WindowEvent()
                Until rc=-1 Or timer1>5
                ProcedureReturn 1
              Else
                send_ok=-1 
              EndIf
            Else
              zend("QUIT" + crlf$ )
            EndIf 
          Else
            zend("QUIT" + crlf$ )
          EndIf 
        Else
          zend("QUIT" + crlf$ )
        EndIf 
      Else
        zend("QUIT" + crlf$ )
      EndIf 
    EndIf
  EndIf
  ProcedureReturn -1
EndProcedure



Procedure MyWindowCallback(WindowID, Message, wParam, lParam)
  Result = #PB_ProcessPureBasicEvents
  Select message
    Case PB_TCP_MESSAGE
      receive_TCP()
      result=0
    Case #WM_TIMER
      result=0
      Select wparam
        Case 1
          timer1 +1
          If timer1>$FFFFFF
            timer1=0
          EndIf
      EndSelect
  EndSelect
  ProcedureReturn Result
EndProcedure

If hwnd
  ButtonGadget(1, 25, 11, 50, 19, "TestMail" ,#PB_Text_Center )
  SetWindowCallback(@MyWindowCallback())
  
  ; -------------- timers ----------------
  SetTimer_(hWnd,1,1000,0) ; 1 seconden timer
  ; SetTimer_(hWnd,2,50,0) ; 50 milisecond timer
  
  Repeat
    EventID = WaitWindowEvent()
    
    If EventID = #PB_Event_Gadget
      Select EventGadget()
        Case 1
          SendMail()
      EndSelect
    EndIf   
  Until eventid=#PB_Event_CloseWindow
  
EndIf

End 

Regards,

Berikco

http://www.benny.zeb.be
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.

It works fine Berikco. I replaced values to fit my own PC IP address (I have an internal SMTP).

Just changing constants in your sendmail proceduer with variables makes it a good sendmail program.

If you are going about to enhance it with MIME I will let you do, otherwise I will probably add some things to this.

Rgrds

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 Berikco.
Originally posted by fweil

It works fine Berikco. I replaced values to fit my own PC IP address (I have an internal SMTP).
Also local SMTP server here.
SMTPserver$ = "192.168.4.1" ; ip SMTP server :)

Just changing constants in your sendmail proceduer with variables makes it a good sendmail program.
Its only something a dit, to learn to use API in PureBasic , i'm happy you like it.
If you are going about to enhance it with MIME I will let you do, otherwise I will probably add some things to this.
Please, enhance as you like Francois, i needed it to send plain ascii, so i'm note going to work further on it.


Regards,

Berikco

http://www.benny.zeb.be
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 Julien Morel.

I have this error message? :


Relaying denied. Proper authentication required

Forum in French
http://www.forumpurebasic.fr.st
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 cor.

Your settings are not correct.

You are trying to send mail from another provider as your current connected provider.

Most providers won't let you do that.

Originally posted by Julien Morel

I have this error message? :


Relaying denied. Proper authentication required

Forum in French
http://www.forumpurebasic.fr.st
Using Windows 98 SE
Registered Purebasic
--------------------------
C. de Visser
Author of Super Guitar Chord Finder
http://www.ready4music.com
http://www.chordplanet.com
Post Reply