Author Topic: FTP and Email Code Snippets  (Read 3343 times)

Offline Kimaro

  • Mc. Print
  • *
  • Posts: 28
  • That's got the hard drive out!
    • View Profile
FTP and Email Code Snippets
« on: 2010-Jun-30 »
Have any of you brilliant coders out there got any code snippets for FTP'ing or emailing with attachments.  :nw:

MrTAToad

  • Guest
Re: FTP and Email Code Snippets
« Reply #1 on: 2010-Jun-30 »
I've got a Windows only bit of code for emails.  It was originally used for work, but should still work :

Code: GLBasic [Select]
HINSTANCE       hMail = NULL;

        hMail = ::LoadLibraryA("MAPI32.DLL");

        if (hMail == NULL)
        {
                MessageLastError();
                SetCurrentDirectory((char *) &currentDir);

                return 1;
        }

        ULONG (PASCAL *lpfnSendMail)(ULONG, ULONG, MapiMessage*, FLAGS, ULONG);

        (FARPROC&) lpfnSendMail = GetProcAddress(hMail, "MAPISendMail");

        if (lpfnSendMail == NULL)
        {
                MessageLastError();
                SetCurrentDirectory((char *) &currentDir);             
                return 2;
        }
        MapiRecipDesc   rec;
       

        char    szRName[] = "PlasWare Technical Support";
        char    szRAddress[] = "SMTP:support@plasware.co.uk";
        char    szSubject[] = "PlasWare Error Report";

        SecureZeroMemory(&rec,sizeof(rec));

        rec.ulRecipClass = MAPI_TO;
        rec.lpszName = (emailName==NULL ? szRName : emailName);
        rec.lpszAddress = (emailAddress==NULL ? szRAddress : emailAddress);

        MapiMessage message;
        SecureZeroMemory(&message,sizeof(message));

        message.nRecipCount = 1;
        message.lpRecips = &rec;
        message.lpszSubject = (subject==NULL ? szSubject : subject);
        message.lpszNoteText = ErrorText;

        message.nFileCount=0; //(numAttachments>MAX_ATTACHMENTS ? MAX_ATTACHMENTS : numAttachments);

        if (attachments)
        {
        register int loop;

                SecureZeroMemory(&fileDesc,sizeof(fileDesc));
                loop=0;
                while ((attachments) && (strlen(attachments->fileName)>0) && loop<MAX_ATTACHMENTS)
                {                      
                        fileDesc[loop].flFlags=0; // NOT an OLE object
                        fileDesc[loop].nPosition=-1; //loop;
                        fileDesc[loop].lpszPathName=attachments->fileName;
                        fileDesc[loop].lpszFileName=NULL;
                        fileDesc[loop].lpFileType=NULL; // Let the system cope with the extension
                        message.nFileCount++;
                        attachments++;                 
                        loop++;
                }

                message.lpFiles=(message.nFileCount>0 ? (lpMapiFileDesc) fileDesc : NULL);
        }
        else
        {
                message.lpFiles=NULL;
        }

        int nError;
        if (showDialog == 1)
        {
                nError = lpfnSendMail(0, (ULONG) NULL, &message, MAPI_LOGON_UI | MAPI_DIALOG, 0);
        }
        else           
        {
                nError = lpfnSendMail(0, (ULONG) NULL, &message, MAPI_LOGON_UI, 0);
        }

        if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
        {
                windowWarnWithParam_S(WARNING_SENDMAILFAILED,false,
                                                                (nError==MAPI_E_INSUFFICIENT_MEMORY ? "Insufficient Memory" : \
                                                                 nError==MAPI_E_AMBIGUOUS_RECIPIENT ? "Ambiguous recipient" : \
                                                                 nError==MAPI_E_ATTACHMENT_NOT_FOUND ? "Attachment(s) not found" : \
                                                                 nError==MAPI_E_ATTACHMENT_OPEN_FAILURE ? "Unable to open attachment" : \
                                                                 nError==MAPI_E_BAD_RECIPTYPE ? "Invalid Parameters Passed" : \
                                                                 nError==MAPI_E_FAILURE ? "Unspecified Error" : \
                                                                 nError==MAPI_E_INVALID_RECIPS ? "Invalid Recipients" : \
                                                                 nError==MAPI_E_TEXT_TOO_LARGE ? "Text in E-Mail is too large" : \
                                                                 nError==MAPI_E_TOO_MANY_FILES ? "Too many attachments" : \
                                                                 nError==MAPI_E_TOO_MANY_RECIPIENTS ? "Too many recipients" : \
                                                                 nError==MAPI_E_UNKNOWN_RECIPIENT ? "Unknown recipients" : "Unknown"));
        }
        else
        {
                nError=ERROR_SUCCESS;
        }

        ::FreeLibrary(hMail);
        SetCurrentDirectory((char *) &currentDir);
        return (nError);

For Linux and possibly Mac, you could call sendmail : http://www.hosting.com/support/linux/programming/sendmail/
« Last Edit: 2010-Jun-30 by MrTAToad »

Offline Kimaro

  • Mc. Print
  • *
  • Posts: 28
  • That's got the hard drive out!
    • View Profile
Re: FTP and Email Code Snippets
« Reply #2 on: 2010-Jul-01 »
Thanks for the reply. I was wondering if there was anything that could be used from GLB possibly using third party dll's.

MrTAToad

  • Guest
Re: FTP and Email Code Snippets
« Reply #3 on: 2010-Jul-01 »
With Windows, you could get the current mail program from the registry and then call it...  You dont need a DLL for using the previously mentioned code though!

With Linux, you could use SHELLCMD to call the appropriate program
« Last Edit: 2010-Jul-02 by MrTAToad »

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1748
    • View Profile
    • Homepage
Re: FTP and Email Code Snippets
« Reply #4 on: 2010-Jul-02 »
Or... you could just do it with GLBasics built in sockets commands... :-)

MrTAToad

  • Guest
Re: FTP and Email Code Snippets
« Reply #5 on: 2010-Jul-02 »
Good idea!

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10638
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: FTP and Email Code Snippets
« Reply #6 on: 2010-Jul-09 »
I have some... welll. not too good POP3 code if it's of any use...
Code: GLBasic [Select]
Pop3_Login("post.server.com", 110, "username@server.com", "mailpassword")
LOCAL count% = Pop3_GetMailCount()
STDOUT "Mail count: " + count + "\n"
STDOUT  Pop3_GetSubject$(1) + "\n"
Pop3_Quit()

KEYWAIT




GLOBAL gPop3Sock% = -1
FUNCTION Pop3_Login%: server$, port%, username$, password$
        SOCK_INIT()

        gPop3Sock% = SOCK_TCPCONNECT(server$, port%)

        IF gPop3Sock% = -1
                SOCK_SHUTDOWN
                RETURN FALSE
        ENDIF

        // read hello message
        LOCAL reply$
        WHILE TRUE
                LOCAL rv% = SOCK_RECV(gPop3Sock%, reply$, 1024)
                IF rv<>-2 THEN BREAK // anything but "not ready"
        WEND
        STDOUT "Recv: "+reply$+"\n"


        IF MID$(Pop3_Cmd$("USER "+username$), 0,3)= "+OK"
                IF MID$(Pop3_Cmd$("PASS "+password$), 0,3)= "+OK"
                        RETURN TRUE
                ENDIF
        ENDIF

        Pop3_Quit()
        RETURN FALSE
ENDFUNCTION

FUNCTION Pop3_GetMailCount%:
        IF gPop3Sock% = -1 THEN RETURN 0
        LOCAL ret$ = Pop3_Cmd$("STAT")
        IF MID$(ret$, 0,3)= "+OK"
                RETURN INTEGER(MID$(ret$, 4))
        ENDIF
        RETURN 0
ENDFUNCTION


FUNCTION Pop3_GetSubject$: imail%
        IF gPop3Sock% = -1 THEN RETURN

        LOCAL ret$ = Pop3_Cmd$("TOP "+imail + " 10")
        IF MID$(ret$, 0,3)= "+OK"

                LOCAL lines$[]
                SPLITSTR(ret$, lines$[], "\r\n")
                FOREACH l$ IN lines$[]
                        IF LCASE$(MID$(l$, 0, 9)) = "Subject:"
                                RETURN MID$(l$, 10)
                        ENDIF
                NEXT
        ENDIF
        RETURN ""
ENDFUNCTION




FUNCTION Pop3_Quit%:
        IF gPop3Sock% = -1 THEN RETURN
        Pop3_Cmd$("QUIT");

        SOCK_CLOSE(gPop3Sock%)
        gPop3Sock% = -1
        SOCK_SHUTDOWN
ENDFUNCTION




FUNCTION Pop3_Cmd$: cmd$
LOCAL reply$
        STDOUT "Send: "+cmd$+"\n"
        IF SOCK_TCPSEND(gPop3Sock%, cmd$+"\r\n") > 0
                WHILE TRUE
                        LOCAL rv% = SOCK_RECV(gPop3Sock%, reply$, 32767)
                        IF rv=-1 THEN RETURN "-FAIL ?" // error
                        IF rv>=0 THEN BREAK // read or disconnected
                WEND
                STDOUT "Recv: "+reply$+"\n"
        ELSE
                STDOUT "Send failed\n"
        ENDIF
        RETURN reply$
ENDFUNCTION
 

MrTAToad

  • Guest
Re: FTP and Email Code Snippets
« Reply #7 on: 2010-Jul-09 »
Ah - thats how you do it :)