Sending emails

Previous topic - Next topic

MrTAToad

Quick routine for sending an email (attachments aren't dealt with).

I need to tidy this up, and re-post later on, but the first version is :

Code (glbasic) Select
DEBUG "Sent : "+sendEmail("njk@un-map.com","spots@un-map.com","Test","This is a test","<<put your outgoing server address here>>")

FUNCTION sendEmail%:from$,to$,subject$,message$,server$
LOCAL socket%
LOCAL port%=26
LOCAL emailServer%

IF SOCK_INIT()=FALSE THEN RETURN FALSE

socket%=SOCK_TCPCONNECT(server$,port%)
DEBUG "Resulting socket : "+socket%+"\r\n"
IF socket%>=0

DEBUG SOCK_TCPSEND(socket%,"HELO test\r\n")+"\r\n"
DEBUG SOCK_TCPSEND(socket%,"MAIL FROM: "+from$+"\r\n")+"\r\n"
SOCK_TCPSEND(socket%,"RCPT TO: "+to$+"\r\n");
SOCK_TCPSEND(socket%,"DATA\r\n");
SOCK_TCPSEND(socket%,"From: "+from$+"\r\n")
SOCK_TCPSEND(socket%,"To: "+to$+"\r\n")
SOCK_TCPSEND(socket%,"Subject: "+subject$+"\r\n");
SOCK_TCPSEND(socket%,message$+"\r\n");
SOCK_TCPSEND(socket%,".\r\n");
SOCK_TCPSEND(socket%,"QUIT\r\n");

checkResult(socket%)
SOCK_CLOSE(socket%)
SOCK_SHUTDOWN

RETURN TRUE
ENDIF

RETURN FALSE
ENDFUNCTION

FUNCTION checkResult%:socket%
LOCAL result%,data$,count%

count%=0
result%=SOCK_RECV(socket%,data$,1024)
WHILE result%<>0 AND count%<128
IF result%>0 THEN DEBUG data$+"\r\n"
result%=SOCK_RECV(socket%,data$,1024)
INC count%
WEND
ENDFUNCTION

ampos

check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

Ian Price

Very useful. Cheers :)
I came. I saw. I played.

MrTAToad

A tidier version :

Code (glbasic) Select
DEBUG "Sent : "+sendEmail("njk@un-map.com","spots@un-map.com","Test 3","This is a test","<<email server here>>",26)

FUNCTION sendEmail%:from$,to$,subject$,message$,server$,port%
LOCAL socket%
LOCAL emailServer%
LOCAL LFCR$="\r\n"
LOCAL result%

IF SOCK_INIT()=FALSE THEN RETURN FALSE

socket%=SOCK_TCPCONNECT(server$,port%)
IF socket%>=0
SOCK_TCPSEND(socket%,"HELO "+from$+LFCR$)
SOCK_TCPSEND(socket%,"MAIL FROM: "+from$+LFCR$)
SOCK_TCPSEND(socket%,"RCPT TO: "+to$+LFCR$)
SOCK_TCPSEND(socket%,"DATA"+LFCR$)
SOCK_TCPSEND(socket%,"From: "+from$+LFCR$)
SOCK_TCPSEND(socket%,"To: "+to$+LFCR$)
SOCK_TCPSEND(socket%,"Subject: "+subject$+LFCR$)
SOCK_TCPSEND(socket%,message$+LFCR$)
SOCK_TCPSEND(socket%,"."+LFCR$)
SOCK_TCPSEND(socket%,"QUIT"+LFCR$)

SOCK_CLOSE(socket%)

result%=TRUE
ELSE
result%=FALSE
ENDIF

SOCK_SHUTDOWN

RETURN result%
ENDFUNCTION


I've removed the checking of the servers response as I dont feel it's really needed...

It should also be noted that using this routine can send emails without user intervention - so be nice and let the user know if you are about to send an email!

Ian Price

Sending emails without letting the user know... Naughty, naughty!! :P
I came. I saw. I played.

MrTAToad

#5
I've tried it on my email system (which uses port 26) - you would need to change that to whatever is needed for your email system.

I dont know about SMTP-AUTH - no source code I found used that (or even mentioned it).  It is surprising it works though!

Its possible the program is being intercepted by my email client or something like that, so I'm going to try with a system that has no email system set-up - and yes, it does work too!

Also tried it on webOS - works there too...

I'm enquiring with my web hosting people about this...

QuoteSending emails without letting the user know... Naughty, naughty!!
Very!  :P

ampos

#6
It works on port 25 and 26.

Also, it works in Win and iOS, but not on WebOS.

I am amazed it does work so easily. So easy that someone could use my mail server to sent tons of emails... it looks I will have to check my web panel configuration...
check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

MrTAToad

I had no trouble with webOS :)

ampos

Quote from: MrTAToad on 2011-Apr-18
I had no trouble with webOS :)

Yes, now it works.

I have checked my host control panel, and the option about server configuration can not be changed. In fact, it only display an informative text.

Navigating more, there is a section about "how to configure your email client", and a text that says:

Manual configuration: server xxxx.xxx.xxx port 26 (requires autentification)

As I have checked, no, it does not require it :D
check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

ampos

check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

ampos

I have modified the email routine to accept emails with server password...

It worked for me, but I dont know if it works because the previous version works...

I just modified the HELO with EHLO for "new protocol" and add the AUTH PLAIN line...

Code (glbasic) Select

FUNCTION sendEmail%:from$,to$,subject$,message$,server$,pass$,port%
LOCAL socket%
LOCAL emailServer%
LOCAL LFCR$="\r\n"
LOCAL result%

IF SOCK_INIT()=FALSE THEN RETURN FALSE

socket%=SOCK_TCPCONNECT(server$,port%)
IF socket%>=0
// SOCK_TCPSEND(socket%,"HELO "+from$+LFCR$)
SOCK_TCPSEND(socket%,"EHLO "+from$+LFCR$)
SOCK_TCPSEND(socket%,"AUTH PLAIN "+pass$+LFCR$)
SOCK_TCPSEND(socket%,"MAIL FROM: "+from$+LFCR$)
SOCK_TCPSEND(socket%,"RCPT TO: "+to$+LFCR$)
SOCK_TCPSEND(socket%,"DATA"+LFCR$)
SOCK_TCPSEND(socket%,"From: "+from$+LFCR$)
SOCK_TCPSEND(socket%,"To: "+to$+LFCR$)
SOCK_TCPSEND(socket%,"Subject: "+subject$+LFCR$)
SOCK_TCPSEND(socket%,message$+LFCR$)
SOCK_TCPSEND(socket%,"."+LFCR$)
SOCK_TCPSEND(socket%,"QUIT"+LFCR$)

SOCK_CLOSE(socket%)

result%=TRUE
ELSE
result%=FALSE
ENDIF

DEBUG "EMAIL: "+result%

SOCK_SHUTDOWN

RETURN result%
ENDFUNCTION
check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

MrTAToad


Hark0

Quote from: Ian Price on 2011-Apr-17
Sending emails without letting the user know... Naughty, naughty!! :P

Hmm... this can be used for trap piracy....

The app can send me/us an email in first exec app...

http://litiopixel.blogspot.com
litiopixel.blogspot.com - Desarrollo videojuegos Indie · Pixel-Art · Retroinformática · Electrónica Development Indie Videogames · Pixel-Art · Retrocomputing · Electronic

MrTAToad

Oh yes - the only problem is you do have to mention it somewhere.

Hark0

Quote from: MrTAToad on 2011-Apr-21
Oh yes - the only problem is you do have to mention it somewhere.

yes..... its true.

:S

http://litiopixel.blogspot.com
litiopixel.blogspot.com - Desarrollo videojuegos Indie · Pixel-Art · Retroinformática · Electrónica Development Indie Videogames · Pixel-Art · Retrocomputing · Electronic