BASIC

Author Topic: Sending emails  (Read 5754 times)

MrTAToad

  • Guest
Sending emails
« on: 2011-Apr-17 »
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

Offline ampos

  • Prof. Inline
  • *****
  • Posts: 1594
    • View Profile
    • AMpostata Website
Re: Sending emails
« Reply #1 on: 2011-Apr-17 »
Nice!
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

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4144
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: Sending emails
« Reply #2 on: 2011-Apr-17 »
Very useful. Cheers :)
I came. I saw. I played.

MrTAToad

  • Guest
Re: Sending emails
« Reply #3 on: 2011-Apr-17 »
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!

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4144
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: Sending emails
« Reply #4 on: 2011-Apr-17 »
Sending emails without letting the user know... Naughty, naughty!! :P
I came. I saw. I played.

MrTAToad

  • Guest
Re: Sending emails
« Reply #5 on: 2011-Apr-17 »
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...

Quote
Sending emails without letting the user know... Naughty, naughty!!
Very!  :P
« Last Edit: 2011-Apr-17 by MrTAToad »

Offline ampos

  • Prof. Inline
  • *****
  • Posts: 1594
    • View Profile
    • AMpostata Website
Re: Sending emails
« Reply #6 on: 2011-Apr-18 »
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...
« Last Edit: 2011-Apr-18 by 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

MrTAToad

  • Guest
Re: Sending emails
« Reply #7 on: 2011-Apr-18 »
I had no trouble with webOS :)

Offline ampos

  • Prof. Inline
  • *****
  • Posts: 1594
    • View Profile
    • AMpostata Website
Re: Sending emails
« Reply #8 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

Offline ampos

  • Prof. Inline
  • *****
  • Posts: 1594
    • View Profile
    • AMpostata Website
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

Offline ampos

  • Prof. Inline
  • *****
  • Posts: 1594
    • View Profile
    • AMpostata Website
Re: Sending emails
« Reply #10 on: 2011-Apr-18 »
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

  • Guest
Re: Sending emails
« Reply #11 on: 2011-Apr-19 »
Looks okay!

Offline Hark0

  • Prof. Inline
  • *****
  • Posts: 1020
  • Geek Developer
    • View Profile
    • LitioPixel - Desarrollo de videojuegos con GLBasic | Videogame development with GLBasic
Re: Sending emails
« Reply #12 on: 2011-Apr-21 »
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

  • Guest
Re: Sending emails
« Reply #13 on: 2011-Apr-21 »
Oh yes - the only problem is you do have to mention it somewhere.

Offline Hark0

  • Prof. Inline
  • *****
  • Posts: 1020
  • Geek Developer
    • View Profile
    • LitioPixel - Desarrollo de videojuegos con GLBasic | Videogame development with GLBasic
Re: Sending emails
« Reply #14 on: 2011-Apr-22 »
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