FTP and Email Code Snippets

Previous topic - Next topic

Kimaro

Have any of you brilliant coders out there got any code snippets for FTP'ing or emailing with attachments.  :nw:

MrTAToad

#1
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/

Kimaro

Thanks for the reply. I was wondering if there was anything that could be used from GLB possibly using third party dll's.

MrTAToad

#3
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

Moru

Or... you could just do it with GLBasics built in sockets commands... :-)

MrTAToad


Kitty Hello

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

Ah - thats how you do it :)