PureSMTP library : mail & attachments (AUTH supported)

All PureFORM, JaPBe, Libs and useful code maintained by gnozal

Moderator: gnozal

gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Update PB4 version only

Changes :
- added user callback

The callback is like this :

Code: Select all

Procedure MyCallback(AttachmentNumber.l, Progression.f)
  ; AttachmentNumber : attachment number [1 - X]
  ; Progression : attachment sending progression [0 - 100 %]
  Debug "Attachement n°"  + Str(AttachmentNumber) + " : " + StrF(Progression) + "%"
EndProcedure
And defined like this :

Code: Select all

PureSMTP_SetAttachmentCallback(@MyCallback())
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

thanks a lot gnozal.

it works like a charm :P



and congrat' for the very quick answer/update.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Post by Kukulkan »

Hi gnozal,

Your library's are really great! Thank you!

Now a little feature-request:
Is it possible to enhance the PureSMTP library with SSL compatibility? Currently, my users can't use it on GMail accounts and other SSL depending accounts. This would be very nice!

Kukulkan
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Kukulkan wrote:Now a little feature-request:
Is it possible to enhance the PureSMTP library with SSL compatibility? Currently, my users can't use it on GMail accounts and other SSL depending accounts. This would be very nice!
Kukulkan
Sorry ...
I am no network guru and I really don't have the time to study SSL/TLS (much real world work), so my answer is still the same :
Kukulkan wrote:Hi gnozal,
I currently use your library on a PB3.94 project. It works fine, but some people ask for using SSL/TLS for sending e-mails (especially those who are using gmail.com). Is it possible to send and receive e-mails using SSL/TLS? Actualy, only sending is important for me.
I don't know which API's you are using, but maybe it is possible to use InternetOpenUrl_() with the #INTERNET_FLAG_SECURE flag to connect to the server?
I don't use WinAPI, only the purebasic network library functions.
And I don't know the SSL/TLS protocol (I don't know if it's even possible without API).
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Post by Kukulkan »

Oh, I didn't remember that I asked that feature before. Sorry.

Kukulkan
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Kukulkan wrote:Oh, I didn't remember that I asked that feature before. Sorry.

Kukulkan
No problem.
Many apps use a third party DLL from www.OpenSSL.org, so it doesn't look that easy ...
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Post by Kukulkan »

Hi gnozal,

Any news about the SSL/TLS implementation?

(just a joke :wink: )

I need to convert a little tool to linux. Currently I collect information about the technology and tools available. As PureBasic seem to be usable on Linux, it looks good to transfer the code to linux PB. But the tool uses PureSMTP to be able to send e-mails including status reports (attachments). Is there a Linux-port of PureSMTP available? If not, is there PB source that may compile using Linux?

Kukulkan
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Kukulkan wrote:I need to convert a little tool to linux. Currently I collect information about the technology and tools available. As PureBasic seem to be usable on Linux, it looks good to transfer the code to linux PB. But the tool uses PureSMTP to be able to send e-mails including status reports (attachments). Is there a Linux-port of PureSMTP available? If not, is there PB source that may compile using Linux?
No Linux version planned for my tools : I use windows API, and I don't know linux.
There are some code examples almost without API in the codearchiv, including mail with attachments.
Example :

Code: Select all

; www.purearea.net (Sourcecode collection by cnesm)
; Author: PB (updated for PB 4.00 by Andre)
; Date: 22. November 2003
; OS: Windows
; Demo: No

;Modified code originally posted by Paul IIRC  :) 

;USAGE: 
;PBSendMail( 
;                        RecipientEmailAddress as String 
;                        SenderEmailAddress as String 
;                        MailServerHost as String 
;                        Subject as String 
;                        Message as String 
;                        AttachmentIncluded as Byte (Flag: 0/1) 
;                     ) 

;NOTES: 
;When the 'AttachmentIncluded' flag is set to '1', the mail procedure loops through a linked list 
;called 'Attachments()' then encodes or processes the attachments. So to send attachments 
;you must have a linked list called 'Attachments()'. 

;=============================================== 
;-GLOBAL FLAGS / VARIABLES / STRUCTURES / ARRAYS 
;=============================================== 

Global ConnectionID.l 
Global MailResponse.s 

;Example linked list 
Global NewList Attachments.s() 
InsertElement(Attachments()) 
Attachments() = "C:\Documents And Settings\User\Desktop\Image.jpg" 
;InsertElement(Attachments()) 
;Attachments() = "C:\Documents And Settings\User\Desktop\Archive.zip" 
;InsertElement(Attachments()) 
;Attachments() = "C:\Documents And Settings\User\Desktop\ObscureText.fff" 

;=============================================== 
;-PROCEDURES 
;=============================================== 

;Check to see if the file is binary 
Procedure IsBinary(File.s) 
    If ReadFile(0, File) 
        While Loc(0) <> Lof(0) 
            CurrentByte.b = ReadByte(0) 
            If CurrentByte <= 9 Or CurrentByte = 127 
                CloseFile(0) 
                ProcedureReturn 1 
            EndIf 
            If CurrentByte > 10 And CurrentByte < 13 
                CloseFile(0) 
                ProcedureReturn 1 
            EndIf 
            If CurrentByte > 13 And CurrentByte < 32 
                CloseFile(0) 
                ProcedureReturn 1 
            EndIf 
        Wend 
    EndIf 
EndProcedure 

;Find the MIME type for a given file extension 
Procedure.s GetMIMEType(Extension.s) 
    Extension = "." + Extension 
    hKey.l = 0 
    KeyValue.s = Space(255) 
    datasize.l = 255 
    If RegOpenKeyEx_(#HKEY_CLASSES_ROOT, Extension, 0, #KEY_READ, @hKey) 
        KeyValue = "application/octet-stream" 
    Else 
        If RegQueryValueEx_(hKey, "Content Type", 0, 0, @KeyValue, @datasize) 
            KeyValue = "application/octet-stream" 
        Else 
            KeyValue = Left(KeyValue, datasize-1) 
        EndIf 
        RegCloseKey_(hKey) 
    EndIf 
    ProcedureReturn KeyValue 
EndProcedure 

;Send a piece of mail data 
Procedure SendMailData(msg.s) 
    SendNetworkData(ConnectionID, @msg, Len(msg)) 
EndProcedure 

;Check the server responses 
Procedure.s MailResponse() 
    MailResponse=Space(9999) 
    ReceiveNetworkData(ConnectionID,@MailResponse,9999) 
    MailResponse=Left(MailResponse,3) 
    ProcedureReturn MailResponse 
EndProcedure 

;Send the mail 
Procedure PBSendMail(RecipientEmailAddress.s, SenderEmailAddress.s, MailServerHost.s, Subject.s, Message.s, AttachmentIncluded.b) 
    If InitNetwork() 
        ConnectionID = OpenNetworkConnection(MailServerHost, 25) 
        If ConnectionID <> 0 
            MailResponse() 
            If MailResponse = "220" 
                Index = FindString(MailServerHost, ".", 1) 
                MailServerDomain.s = Mid(MailServerHost, Index + 1, Len(MailServerHost)) 
                SendMailData("HELO "+MailServerDomain+Chr(13)+Chr(10)) 
                MailResponse() 
                If MailResponse="250" 
                    Sleep_(125) 
                    SendMailData("MAIL FROM: <"+SenderEmailAddress+">"+Chr(13)+Chr(10)) 
                    MailResponse() 
                    If MailResponse="250" 
                        SendMailData("RCPT TO: <"+RecipientEmailAddress+">"+Chr(13)+Chr(10)) 
                        MailResponse() 
                        If MailResponse="250" 
                            SendMailData("DATA"+Chr(13)+Chr(10)) 
                            MailResponse() 
                            If MailResponse="354" 
                                Sleep_(125) 
                                SendMailData("X-Mailer: PBSendMail v1.0" + Chr(13) + Chr(10)) 
                                SendMailData("To: " + RecipientEmailAddress + Chr(13) + Chr(10)) 
                                SendMailData("From: " + SenderEmailAddress + Chr(13) + Chr(10)) 
                                SendMailData("Reply-To:" + SenderEmailAddress + Chr(13) + Chr(10)) 
                                SendMailData("Date: " + FormatDate("%dd/%mm/%yyyy @ %hh:%ii:%ss", Date()) + Chr(13) + Chr(10)) 
                                SendMailData("Subject: " + Subject + Chr(13) + Chr(10)) 
                                SendMailData("MIME-Version: 1.0" + Chr(13) + Chr(10)) 
                                ;Handle any attachments 
                                If AttachmentIncluded 
                                    Debug "Processing 'multipart/mixed' Email..." 
                                    Boundry.s = "PBSendMailv1.0_Boundry_"+ FormatDate("%dd%mm%yyyy%hh%ii%ss", Date()) 
                                    SendMailData("Content-Type: multipart/mixed; boundary=" + Chr(34) + Boundry + Chr(13) + Chr(10) + Chr(34)) 
                                    SendMailData(Chr(13) + Chr(10)) 
                                    ;Main message 
                                    Debug "Processing Messsage..." 
                                    SendMailData("--" + Boundry + Chr(13) + Chr(10)) ; Boundry 
                                    SendMailData("Content-Type: text/plain; charset=" + Chr(34) + "iso-8859-1" + Chr(34) + Chr(13) + Chr(10)) 
                                    SendMailData("Content-Transfer-Encoding: 7bit" + Chr(13) + Chr(10)) 
                                    SendMailData(Chr(13) + Chr(10)) 
                                    Sleep_(125) 
                                    SendMailData(Message + Chr(13) + Chr(10)) 
                                    SendMailData(Chr(13) + Chr(10)) 
                                    Sleep_(125) 
                                    Debug "Processing Attachments..." 
                                    ResetList(Attachments()) 
                                    While(NextElement(Attachments())) 
                                        ;Attachment headers 
                                        SendMailData("--" + Boundry + Chr(13) + Chr(10)) ; Boundry 
                                        SendMailData("Content-Type: " + GetMIMEType(GetExtensionPart(Attachments())) + "; name=" + Chr(34) + GetFilePart(Attachments()) + Chr(34) + Chr(13) + Chr(10)) 
                                        If IsBinary(Attachments()) 
                                            SendMailData("Content-Transfer-Encoding: base64" + Chr(13) + Chr(10)) 
                                            SendMailData("Content-Disposition: Attachment; filename=" + Chr(34) + GetFilePart(Attachments()) + Chr(34) + Chr(13) + Chr(10)) 
                                            SendMailData(Chr(13) + Chr(10)) 
                                            Sleep_(125) 
                                            ;Encode the Attachments using Base64 
                                            If ReadFile(0, Attachments()) 
                                                InputBufferLength.l = Lof(0) 
                                                *memin = AllocateMemory(InputBufferLength) 
                                                If *mem
                                                    OutputBufferLength.l = InputBufferLength + InputBufferLength/3 + 2 
                                                    If OutputBufferLength < 64 : OutputBufferLength = 64 : EndIf 
                                                    *memout = AllocateMemory(OutputBufferLength) 
                                                    If *memout
                                                        ReadData(0, *memin, InputBufferLength) 
                                                        Base64Encoder(@memin, InputBufferLength, @memout, OutputBufferLength) 
                                                        SendMailData(PeekS(*memout, OutputBufferLength) + Chr(13) + Chr(10)) 
                                                        Debug GetFilePart(Attachments()) + " (base64) Encoded" 
                                                    Else 
                                                        Debug "ERROR: Unable to allocate memory for Bank 1 to process " + GetFilePart(Attachments()) 
                                                        ProcedureReturn 0 
                                                    EndIf 
                                                Else 
                                                    Debug "ERROR: Unable to allocate memory for Bank 0 to process " + GetFilePart(Attachments()) 
                                                    ProcedureReturn 0 
                                                EndIf 
                                            Else 
                                                Debug "ERROR: Unable to read file: " + GetFilePart(Attachments()) 
                                                ProcedureReturn 0 
                                            EndIf 
                                            CloseFile(0) : FreeMemory(*memin) : FreeMemory(*memout) 
                                        Else 
                                            SendMailData("Content-Transfer-Encoding: 7bit" + Chr(13) + Chr(10)) 
                                            SendMailData("Content-Disposition: Attachment; filename=" + Chr(34) + GetFilePart(Attachments()) + Chr(34) + Chr(13) + Chr(10)) 
                                            SendMailData(Chr(13) + Chr(10)) 
                                            Sleep_(125) 
                                            If ReadFile(0, Attachments()) 
                                                InputBufferLength.l = Lof(0) 
                                                *memin = AllocateMemory(InputBufferLength)
                                                If *memin
                                                    ReadData(0, *memin, InputBufferLength) 
                                                    SendMailData(PeekS(*memin, InputBufferLength) + Chr(13) + Chr(10)) 
                                                    Debug GetFilePart(Attachments()) + " (7bit) Processed" 
                                                Else 
                                                    Debug "ERROR: Unable to allocate memory for Bank 0 to process " + GetFilePart(Attachments()) 
                                                    ProcedureReturn 0 
                                                EndIf 
                                            Else 
                                                Debug "ERROR: Unable to read file: " + GetFilePart(Attachments()) 
                                                ProcedureReturn 0 
                                            EndIf 
                                        EndIf 

                                        Sleep_(125) 
                                        SendMailData(Chr(13) + Chr(10)) 
                                    Wend 
                                    SendMailData("--" + Boundry + "--" + Chr(13) + Chr(10)) ; End Boundry 
                                Else 
                                    Debug "Processing messsage..." 
                                    SendMailData("Content-Type: text/plain; charset=" + Chr(34) + "iso-8859-1" + Chr(34) + Chr(13) + Chr(10)) 
                                    SendMailData("Content-Transfer-Encoding: 7bit" + Chr(13) + Chr(10)) 
                                    SendMailData(Chr(13) + Chr(10)) 
                                    Sleep_(125) 
                                    SendMailData(Message + Chr(13) + Chr(10)) 
                                EndIf 
                                Sleep_(125) 
                                SendMailData(Chr(13)+Chr(10)) 
                                SendMailData("."+Chr(13)+Chr(10)) 
                                MailResponse() 
                                If MailResponse="250" 
                                    Sleep_(125) 
                                    SendMailData("QUIT"+Chr(13)+Chr(10)) 
                                    MailResponse() 
                                    Debug "Mail sent successfully." 
                                    ProcedureReturn 1 
                                EndIf 
                            EndIf 
                        EndIf 
                    EndIf 
                EndIf 
            EndIf 
            CloseNetworkConnection(ConnectionID) 
        EndIf 
    EndIf 
EndProcedure 

;Testing: 
PBSendMail("theirmail@server.com", "yourmail@server.com", "smtp.server.com", "Subject Line", "Lorem Ipsum Dolar Sit Amet...", 0) 
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Post by Kukulkan »

Hi Gnozal,

Thank your for your reply and the example-code. But currently there is only one example of SMTP using authentication available in PureArea. But this source is using windows API for registry-access. It seems like I have to implement this by myself for a linux version of my program. Let's see...

Kukulkan
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post by RichardL »

Hi Gnozal,

Pure_SMPT looks like it will solve a problem for me, but I'm having a problem with my SMPT Server that did not appear when I ran you demo code. I could get your demo to work, or my own network functions... but not both...then I found SkipInitNetwork() and all appeared to make sense. However I cannot understand the value of SkipInit.l in the docs...

>> PureSMTP_SkipInitNetwork(SkipInit.l)

Could you kindly explain it for me?
Should it be #True or #False or a value from elsewhere?

Thanks in advance.

Richard L
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

RichardL wrote:PureSMTP_SkipInitNetwork(SkipInit.l)

Could you kindly explain it for me?
Should it be #True or #False or a value from elsewhere?
PureSMTP_SkipInitNetwork(#FALSE) [Default] : PureSMTP calls InitNetwork() at library initialization
PureSMTP_SkipInitNetwork(#TRUE) : PureSMTP does not call InitNetwork() at library initialization ; you have to do it before calling any PureSMTP function.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post by RichardL »

Many thanks...
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Many thanks also from me, Gnozal! :!:
I have a question to this lib; can be sent 2000 email messages to different email addresses from a hotmail account or from any other? :?:
Thanks again !

EDIT: BTW, it is not to spread spam, but to spread email messages to a society for the mobility optimization in the cities (i.e. more use of bicycles, etc.).
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Psychophanta wrote:Many thanks also from me, Gnozal! :!:
I have a question to this lib; can be sent 2000 email messages to different email addresses from a hotmail account or from any other? :?:
Thanks again !
I don't know about hotmail.
All I can say is that it works with freenet.de for example. Note that this lib does not support SSL. So gmail.com for example won't work.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

gnozal wrote:
Psychophanta wrote:Many thanks also from me, Gnozal! :!:
I have a question to this lib; can be sent 2000 email messages to different email addresses from a hotmail account or from any other? :?:
Thanks again !
I don't know about hotmail.
All I can say is that it works with freenet.de for example. Note that this lib does not support SSL. So gmail.com for example won't work.
Thanks!
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply