PureSMTP library : mail & attachments (AUTH supported)

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

Moderator: gnozal

Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post by Beach »

I use this lib all the time, thanks Gnozal!!
-Beach
stnien

could run in unicode mode?

Post by stnien »

Thanks Gnozal ! I like your Pure_SMTP library. But I have a trouble in using this library. It seems don't work in unicode mode?
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: could run in unicode mode?

Post by gnozal »

stnien wrote:Thanks Gnozal ! I like your Pure_SMTP library. But I have a trouble in using this library. It seems don't work in unicode mode?
No.
Well, I can compile the lib in unicode mode (works), but the SMTP server I tested doesn't understand unicode, so it doesn't work. I guess I would have to translate all the strings to ASCII before sending them and translate the strings received to UNICODE ...

BTW, I have just updated PureSMTP for PB4 with a threadsafe version.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
stnien

Post by stnien »

Hi Gnozal,

I've try to run the code with unicode mode..

Code: Select all

If InitNetwork()
    MailID=OpenNetworkConnection("myserver",25)
    *a = AllocateMemory(9999)
    res = ReceiveNetworkData(MailID, *a, 9999)
    response.s = ""
    For i = 0 To res - 1
    	response + Chr(PeekB(*a+i))
    Next
    Debug response
    
    CloseNetworkConnection(MailID)
EndIf
When I run it, I can get "220 xxxxxx" (ps: SMTP server's banner). So I think SMTP can accept unicode string, but the response data isn't unicode string. Therefore maybe have to do a translation after receive response data.
stnien
New User
New User
Posts: 3
Joined: Sun Nov 26, 2006 6:22 am
Location: Taiwan

Post by stnien »

Sorry, I try again and I confirm that SMTP can't accept unicode string. Whatever send to SMTP server or receive from SMTP server, all of these strings have to use non unicode.
stnien
New User
New User
Posts: 3
Joined: Sun Nov 26, 2006 6:22 am
Location: Taiwan

SMTP send mail with unicode mode

Post by stnien »

Okay, I try to the code send mail with unicode mode and it works.

Code: Select all

Global MailID

Procedure MailDataSend(msg$)
	Debug "Command: "+msg$
	*cmd = AllocateMemory(9999)
	For i = 1 To Len(msg$)
		asc = Asc(Mid(msg$, i, 1))
		PokeB(*cmd+i-1, asc)
	Next
	
	SendNetworkData(MailID, *cmd, Len(msg$))
EndProcedure

Procedure.s MailResponse()
	a$=Space(9999)
	res = ReceiveNetworkData(MailID,@a$,9999)
	response.s = ""
    For i = 0 To res - 1
       response + Chr(PeekB(@a$+i))
    Next
	
	Debug response
	ProcedureReturn Left(response,3)
EndProcedure

Procedure MailSend(ms_server$,ms_port$,ms_recipient$,ms_subject$,ms_body$,ms_from$)
	If InitNetwork()
		MailID=OpenNetworkConnection(ms_server$,Val(ms_port$))
		If MailID
			If MailResponse()="220"
				MailDataSend("HELO Mailer"+#CRLF$)
				If MailResponse()="250"
					MailDataSend("MAIL FROM: <"+ms_from$+">"+#CRLF$)
					If MailResponse()="250"
						MailDataSend("RCPT TO: <"+ms_recipient$+">"+#CRLF$)
						If MailResponse()="250"
							MailDataSend("DATA"+#CRLF$)
							If MailResponse()="354"
								MailDataSend("Date: "+#CRLF$)
								MailDataSend("From: "+ms_from$+#CRLF$)
								MailDataSend("To: "+ms_recipient$+#CRLF$)
								MailDataSend("Subject: "+ms_subject$+#CRLF$)
								MailDataSend(ms_body$) : MailDataSend(""+#CRLF$) : MailDataSend("."+#CRLF$)
								If MailResponse()="250" : MailDataSend("QUIT"+#CRLF$) : result=1 : EndIf
							EndIf
						EndIf
					EndIf
				EndIf
			EndIf
			CloseNetworkConnection(MailID)
		EndIf
	EndIf
	ProcedureReturn result
EndProcedure

MailSend("mail.isp.com.au","25","to@address.com","SubjectGoesHere","BodyGoesHere","from@address.com")  
PS: The original code is post in http://www.purebasic.fr/english/viewtop ... a8d9bbc68a
and programmer is PB.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: SMTP send mail with unicode mode

Post by gnozal »

Something like this should also work (using PokeS/PeekS) [not tested, I don't have PB right here] :
stnien wrote:Okay, I try to the code send mail with unicode mode and it works.

Code: Select all

Global MailID

Procedure MailDataSend(msg$)
	Debug "Command: "+msg$
	*cmd = AllocateMemory(9999)
;	For i = 1 To Len(msg$)
;		asc = Asc(Mid(msg$, i, 1))
;		PokeB(*cmd+i-1, asc)
;	Next
         PokeS(*cmd, msg$, Len(msg$) , #PB_Ascii)
	
	SendNetworkData(MailID, *cmd, Len(msg$))
EndProcedure

Procedure.s MailResponse()
	a$=Space(9999)
	res = ReceiveNetworkData(MailID,@a$,9999)
	response.s = ""
;    For i = 0 To res - 1
;       response + Chr(PeekB(@a$+i))
;    Next
    response =  PeekS(@a$, res , #PB_Ascii)
	
	Debug response
	ProcedureReturn Left(response,3)
EndProcedure

Procedure MailSend(ms_server$,ms_port$,ms_recipient$,ms_subject$,ms_body$,ms_from$)
	If InitNetwork()
		MailID=OpenNetworkConnection(ms_server$,Val(ms_port$))
		If MailID
			If MailResponse()="220"
				MailDataSend("HELO Mailer"+#CRLF$)
				If MailResponse()="250"
					MailDataSend("MAIL FROM: <"+ms_from$+">"+#CRLF$)
					If MailResponse()="250"
						MailDataSend("RCPT TO: <"+ms_recipient$+">"+#CRLF$)
						If MailResponse()="250"
							MailDataSend("DATA"+#CRLF$)
							If MailResponse()="354"
								MailDataSend("Date: "+#CRLF$)
								MailDataSend("From: "+ms_from$+#CRLF$)
								MailDataSend("To: "+ms_recipient$+#CRLF$)
								MailDataSend("Subject: "+ms_subject$+#CRLF$)
								MailDataSend(ms_body$) : MailDataSend(""+#CRLF$) : MailDataSend("."+#CRLF$)
								If MailResponse()="250" : MailDataSend("QUIT"+#CRLF$) : result=1 : EndIf
							EndIf
						EndIf
					EndIf
				EndIf
			EndIf
			CloseNetworkConnection(MailID)
		EndIf
	EndIf
	ProcedureReturn result
EndProcedure

MailSend("mail.isp.com.au","25","to@address.com","SubjectGoesHere","BodyGoesHere","from@address.com")  
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
stnien
New User
New User
Posts: 3
Joined: Sun Nov 26, 2006 6:22 am
Location: Taiwan

Post by stnien »

Hi gnozal,

Yes, it can work. You are a PureBasic Expert. Thank you! :D
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

stnien wrote:Hi gnozal,

Yes, it can work. You are a PureBasic Expert. Thank you! :D
Glad it works.
I will try to update the lib when I have some time.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
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)

Added unicode version.

I lost some time on base64 / unicode issues but should work now.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

GeoTrail wrote:When I enter the from address with name like this: My name <email@addy.com> it doesn't send the name. But if I remove the space like this My name<email@addy.com> it works. But I can't get it to send the name of the recipient.
Works here with or without space.
Could you post a code example ?
GeoTrail wrote: Only seems to work with the senders email addy.
Yes, I will change this.


Could you test this beta lib (ANSI / PB4) : http://people.freenet.de/gnozal/PureSMTP_BETA.zip ?
It should now work for the recipient.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Thanks for you reply gnozal.
I'm testing it now. Just waiting for that darn server to receive the mail. Sooo sloowww hehehe
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

That works great gnozal, thanks alot for the update.
One more thing, is it possible to let the programmer change this line?

Received: from PureSMTP

Would also be great to be able to edit more settings too, like Content-Transfer-Encoding: 7bit

Would that be possible? kinda like with php sendmail?
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

GeoTrail wrote:One more thing, is it possible to let the programmer change this line?
Received: from PureSMTP
Did you check PureSMTP_SetXMailer() ?

Code: Select all

PureSMTP_SetXMailer(XMailer.s) ; Set X-Mailer [Default is "PureSMTP"]
GeoTrail wrote: Would also be great to be able to edit more settings too, like Content-Transfer-Encoding: 7bit
Would that be possible? kinda like with php sendmail?
I don't know php. Why do you need this ?
I send "Content-Transfer-Encoding: 7bit" for text and "Content-Transfer-Encoding: base64" for binary attachments.
Note that you can set the content type with PureSMTP_SetContentType().

Code: Select all

PureSMTP_SetContentType(ContentType.s) ; Set content type [Default is "text/plain; charset=iso-8859-1"]
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Update (both versions)

Changes :
- "My name <email@addy.com>" syntax can now also be used for recipient.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Post Reply