GLBasic forum

Codesnippets => Code Snippets => Topic started by: MrTAToad on 2011-Apr-17

Title: Sending emails
Post by: MrTAToad 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
Title: Re: Sending emails
Post by: ampos on 2011-Apr-17
Nice!
Title: Re: Sending emails
Post by: Ian Price on 2011-Apr-17
Very useful. Cheers :)
Title: Re: Sending emails
Post by: MrTAToad 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!
Title: Re: Sending emails
Post by: Ian Price on 2011-Apr-17
Sending emails without letting the user know... Naughty, naughty!! :P
Title: Re: Sending emails
Post by: MrTAToad 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...

QuoteSending emails without letting the user know... Naughty, naughty!!
Very!  :P
Title: Re: Sending emails
Post by: ampos 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...
Title: Re: Sending emails
Post by: MrTAToad on 2011-Apr-18
I had no trouble with webOS :)
Title: Re: Sending emails
Post by: ampos on 2011-Apr-18
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
Title: Re: Sending emails
Post by: ampos on 2011-Apr-18
Look at this entry:

http://en.wikipedia.org/wiki/SMTP (http://en.wikipedia.org/wiki/SMTP)

http://en.wikipedia.org/wiki/SMTP-AUTH (http://en.wikipedia.org/wiki/SMTP-AUTH)
Title: Re: Sending emails
Post by: ampos 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
Title: Re: Sending emails
Post by: MrTAToad on 2011-Apr-19
Looks okay!
Title: Re: Sending emails
Post by: Hark0 on 2011-Apr-21
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...

Title: Re: Sending emails
Post by: MrTAToad on 2011-Apr-21
Oh yes - the only problem is you do have to mention it somewhere.
Title: Re: Sending emails
Post by: Hark0 on 2011-Apr-22
Quote from: MrTAToad on 2011-Apr-21
Oh yes - the only problem is you do have to mention it somewhere.

yes..... its true.

:S

Title: Re: Sending emails
Post by: Hark0 on 2011-Jul-14
HI!


I'm triing to test these function with Google Mail...

DEBUG shows "EMAIL: 1EMAIL: 1Sent : 1"...

I no receive any email.... I use this config for Gmail... http://mail.google.com/support/bin/answer.py?answer=13287

I call the function with this data:

from$="my google account @gmail.com"
to$="destination email account @server.com"
subject$="Test email"
message$="Hello World"
server$="smtp.gmail.com"
pass$="my gmail account pass"
port%=587 (tried with 465 too)



Any help are welcome!!!

;)

Title: Re: Sending emails
Post by: MrTAToad on 2011-Jul-14
Google Mail requires SSL/TLS authentation and would require extra stuff to be sent in the message header.  Unfortunately that bit was beyond me :)
Title: Re: Sending emails
Post by: Hark0 on 2011-Jul-14
Thanks for the answer...

hmmmmmm Thinking another solution....  :o
Title: Re: Sending emails
Post by: Kitty Hello on 2011-Jul-14
you want to send a mail? Why not use a webserver with a tiny php script? Make it secure, please! Or you get spambots using it.
Title: Re: Sending emails
Post by: Hark0 on 2011-Jul-15
Quote from: Kitty Hello on 2011-Jul-14
you want to send a mail? Why not use a webserver with a tiny php script? Make it secure, please! Or you get spambots using it.

Well... really Im are interested in email implementation with image attached (for iOS devices)  =D

but

I read some forum post due difficulty implementation of this feature....


Im thinking in your solution... thanks!
Title: Re: Sending emails
Post by: Kitty Hello on 2011-Jul-15
you can send an image with php through a form easily. Need code? Or don't you have a php server?
Title: Re: Sending emails
Post by: Hark0 on 2011-Jul-16
Quote from: Kitty Hello on 2011-Jul-15
you can send an image with php through a form easily. Need code? Or don't you have a php server?

All two!!!

I no have code...

I no have php server...
:(


Title: Re: Sending emails
Post by: Neurox on 2011-Sep-11
Hi,
how to is possible implementing iOS MFMailComposeViewController function for sending EMail from iPhone ?
Title: Re: Sending emails
Post by: ampos on 2011-Sep-11
Netwebend "mailto://the@email.here"
Title: Re: Sending emails
Post by: Neurox on 2011-Sep-11
Quote from: ampos on 2011-Sep-11
Netwebend "mailto://the@email.here"
Thanks Ampos but
I don't want send EMail only until the end of the program.
I also need to insert data processed by the app.