This is a work in progress but maybe helpful to others right now.
I am using the linux sme server 7.04 which comes with a email server.
This piece of code was probably from many different people, but Dave Navarro had his name in it.
The sme server will let you send email by smtp from the local lan(intranet)
This program does just that. The email server will do SMTP emailing from a wlan where alterations are made but the default action is SSMTP from wlan.
This program does not do SSMTP, YET.
The program was written using pbcc40 and it reads a file to get the information to send.
It can log what takes place.
I wanted to use a email program that was called by another program and use a secondary file for emailing instructions.
I left some code in the program that is not being used, mime and authenication.
The program looks for specific response codes coming from the linux sme server 7.04, the program can be altered to accept response code from a range of acceptable answers from the email server, but you will have to code that.
By placing the word VIEWALL OR VIEWSERVER on the command line with the program, you can make the program display logging information. I did not want this program to display anthing while running in a normal way. By using VIEWSERVER and redirecting the output to a file, you can find your servers response codes and adjust the program then recompiling according to your server.
Future use will be add html in a email, authenication, up to 5 attachments, version tracking, SSMTP, delete the instruction file upon a flag, and/or rename the instruction file upon a flag, and save the emailed information into another file.
The returned errorlevel code is 0 for failure and 1 for success
This program was made to be generic where it could fit most needs by somebody emailing.
Is the code dirty, why yes, top down coding.
example of instruction file, you can name it anything you want
I am using the linux sme server 7.04 which comes with a email server.
This piece of code was probably from many different people, but Dave Navarro had his name in it.
The sme server will let you send email by smtp from the local lan(intranet)
This program does just that. The email server will do SMTP emailing from a wlan where alterations are made but the default action is SSMTP from wlan.
This program does not do SSMTP, YET.
The program was written using pbcc40 and it reads a file to get the information to send.
It can log what takes place.
I wanted to use a email program that was called by another program and use a secondary file for emailing instructions.
I left some code in the program that is not being used, mime and authenication.
The program looks for specific response codes coming from the linux sme server 7.04, the program can be altered to accept response code from a range of acceptable answers from the email server, but you will have to code that.
By placing the word VIEWALL OR VIEWSERVER on the command line with the program, you can make the program display logging information. I did not want this program to display anthing while running in a normal way. By using VIEWSERVER and redirecting the output to a file, you can find your servers response codes and adjust the program then recompiling according to your server.
Future use will be add html in a email, authenication, up to 5 attachments, version tracking, SSMTP, delete the instruction file upon a flag, and/or rename the instruction file upon a flag, and save the emailed information into another file.
The returned errorlevel code is 0 for failure and 1 for success
This program was made to be generic where it could fit most needs by somebody emailing.
Is the code dirty, why yes, top down coding.
Code:
#COMPILE EXE "SMEEMAIL.EXE" #DIM ALL #INCLUDE "Win32API.inc" GLOBAL gssecuser AS STRING GLOBAL gssecpassword AS STRING '----------------------------------------------------------------------------- ' Retrieve the current time and date in E-mail header format ' FUNCTION MailDate () AS STRING LOCAL szFormat AS ASCIIZ * 40 LOCAL szTemp AS ASCIIZ * 40 LOCAL sResult AS STRING LOCAL t AS SYSTEMTIME LOCAL sUCTOffset AS STRING LOCAL tzi AS TIME_ZONE_INFORMATION GetLocalTime t szFormat = "ddd',' dd MMM yyyy" GetDateFormat %LOCALE_USER_DEFAULT, 0, t, szFormat, szTemp, SIZEOF(szTemp) sResult = szTemp szFormat = "HH':'mm':'ss" GetTimeFormat %LOCALE_USER_DEFAULT, 0, t, szFormat, szTemp, SIZEOF(szTemp) SELECT CASE GetTimeZoneInformation(tzi) CASE %TIME_ZONE_ID_DAYLIGHT sUCTOffset = IIF$((tzi.bias + tzi.DaylightBias) <= 0, "+", "-") _ + FORMAT$((tzi.bias + tzi.DaylightBias) \ 60, "00") _ + FORMAT$((tzi.bias + tzi.DaylightBias) MOD 60, "00") CASE %TIME_ZONE_ID_STANDARD sUCTOffset = IIF$((tzi.bias + tzi.StandardBias) <= 0, "+", "-") _ + FORMAT$((tzi.bias + tzi.StandardBias) \ 60, "00") _ + FORMAT$((tzi.bias + tzi.StandardBias) MOD 60, "00") CASE ELSE sUCTOffset = "-0000" END SELECT FUNCTION = sResult + " " + szTemp + " " + sUCTOffset END FUNCTION FUNCTION base64encode(BYVAL S_FileData AS STRING) AS STRING LOCAL lBlock, lcBlocks, lByte1, lByte2, lByte3, lIndex1, lIndex2, lIndex3, lIndex4 AS LONG LOCAL pInput, pOutput , pTable AS BYTE PTR LOCAL sBase64, sResult, Pad AS STRING sBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ' Base64 table Pad = STRING$(2 - (LEN(S_FileData) - 1) MOD 3, "=") ' Calculate padding on Base64 stream lcBlocks = (LEN(S_FileData) + 2) \ 3 ' Round up the length of the input IF lcBlocks * 3 > LEN(S_FileData) THEN ' data to a multiple of three S_FileData = LSET$(S_FileData, lcBlocks * 3 USING $NUL) END IF sResult = SPACE$(lcBlocks * 4) ' Allocate space for output string pInput = STRPTR(S_FileData) ' Set up pointers so we can treat pOutput = STRPTR(sResult) ' the data as byte streams pTable = STRPTR(sBase64) FOR lBlock = 1 TO lcBlocks ' Loop through the input buffer lByte1 = @pInput : INCR pInput ' Get the next three binary data- lByte2 = @pInput : INCR pInput ' bytes to process lByte3 = @pInput : INCR pInput lIndex1 = lByte1 \ 4 ' Translate 3 databytes to 4 Base64 lIndex2 = (lByte1 AND 3) * 16 + lByte2 \ 16 ' table indices lIndex3 = (lByte2 AND 15) * 4 + lByte3 \ 64 lIndex4 = lByte3 AND 63 @pOutput = @pTable[lIndex1] : INCR pOutput ' Use Base64 table to encode the @pOutput = @pTable[lIndex2] : INCR pOutput ' output string @pOutput = @pTable[lIndex3] : INCR pOutput @pOutput = @pTable[lIndex4] : INCR pOutput NEXT lBlock RSET ABS sResult = Pad ' Merge in the padding bytes FUNCTION = sResult END FUNCTION FUNCTION PBMAIN () AS LONG 'this is the main code section '----------------------------------------------------------- DIM localhost AS STRING DIM smtphost AS STRING DIM htcp AS LONG DIM sline AS STRING DIM e AS LONG DIM buffer AS STRING DIM emailmessage AS STRING DIM semailinfofile AS STRING DIM lefile AS LONG DIM ltemp AS LONG DIM semailfile AS STRING DIM sheader AS STRING DIM semailtxthtml AS STRING DIM semailtxt AS STRING DIM semailhtml AS STRING DIM stemp AS STRING DIM lviewall AS LONG ' use to view the happening while sending a email DIM lviewserver AS LONG DIM semailinternaluse AS STRING DIM semailversion AS STRING DIM semailreturncode AS STRING DIM semailreturnfile AS STRING DIM semailmethod AS STRING DIM semailsecureuser AS STRING DIM semailsecurepassword AS STRING DIM semailsmtpserver AS STRING DIM semailfromwho AS STRING DIM semailtowho AS STRING DIM semailsubject AS STRING DIM semailattachment1 AS STRING DIM semailattachment2 AS STRING DIM semailattachment3 AS STRING DIM semailattachment4 AS STRING DIM semailattachment5 AS STRING DIM lsmtperror AS LONG DIM lipaddress AS LONG semailinfofile=UCASE$(" "+COMMAND$+" ") IF TRIM$(semailinfofile)="" THEN STDOUT "place a file in the command line(tail) that has" STDOUT " the email information to send" STDOUT "the structure of the file is listed" STDOUT "----------------------------------------------------------" STDOUT "<internaluse>" STDOUT "</internaluse>" STDOUT "<header>" STDOUT "version==1" STDOUT "returncode==01" STDOUT "returnfile==xxxxxxxx.ext" STDOUT "method==smtp" STDOUT "secureuser==myaccountname" STDOUT "securepassword==myaccountpassword" STDOUT "smtpserver==mysmtpserver.com or a ip address 192.168.0.2" STDOUT "[email protected]" STDOUT "[email protected]" STDOUT "subject==hello beautiful" STDOUT "attachment1==futureuse" STDOUT "attachment2==futureuse" STDOUT "attachment3==futureuse" STDOUT "attachment4==futureuse" STDOUT "attachment5==futureuse" STDOUT "</header> STDOUT "<mainemailtxtmessage> STDOUT "</mainemailtxtmessage>" STDOUT "<mainemailhtmlmessage>" STDOUT "</mainemailhtmlmessage>" STDOUT "<endofcrazyemailstuff>" STDOUT "----------------------------------------------------------" STDOUT "Currently this program does not support" STDOUT " file attachments, html, returncode,version checking," STDOUT " authenication or method" STDOUT "Place the word VIEWALL OR VIEWSERVER view text for logging" STDOUT "The word VIEWSERVER will log only interaction with the email server" STDOUT "An example woud be" STDOUT "sendmail.exe c:\email\emailfile.001 VIEWALL > emailfile.001.txt" STDOUT "This program is designed to be called from another program or batch file" STDOUT "Ordinarily there is no returned text for automation purposes" STDOUT "An errorlevel 0 is returned for failure and a 1 for success." PRINT "To save these instructions, redirect this program to a file" PRINT "Like sendmail.exe > sendmail.doc" FUNCTION=0 GOTO DONE END IF ltemp=INSTR(semailinfofile," VIEWALL ") IF ltemp THEN REPLACE " VIEWALL " WITH " " IN semailinfofile: lviewall=1& ltemp=INSTR(semailinfofile," VIEWSERVER ") IF ltemp THEN REPLACE " VIEWSERVER " WITH " " IN semailinfofile: lviewserver=1& IF lviewall THEN lviewserver=1& semailinfofile=TRIM$(semailinfofile) lefile=FREEFILE REM open the file that contains the email information IF lviewall THEN STDOUT "opening file "+semailinfofile TRY OPEN semailinfofile FOR BINARY AS lefile EXIT TRY CATCH IF lviewall THEN STDOUT "cannot find or open the file"+semailinfofile IF lviewall THEN STDOUT "this program is aborting" GOTO DONE END TRY REM read the whole email information file ltemp=LOF(lefile) IF ltemp=0& THEN GOTO SENDERROR GET$ #lefile,ltemp,semailfile CLOSE #lefile IF lviewall THEN STDOUT "file was read " REM strip the internal use info, header and email txt/html from the whole file in two separte parts REM strip the internal use information if any is found IF lviewall THEN STDOUT "started parsing data from file" ltemp=INSTR(UCASE$(semailfile),$CRLF+"<HEADER") IF ltemp THEN semailinternaluse=TRIM$(MID$(semailfile,1,ltemp+1)) REM strip the header and email txt/html f ltemp=INSTR(UCASE$(semailfile),"</HEADER") ltemp=INSTR(ltemp+3&,semailfile,$CRLF) sheader=MID$(semailfile,1&,ltemp+1&) sheaderclean: REPLACE $CRLF+" " WITH $CRLF IN sheader IF INSTR(sheader,$CRLF+" ") GOTO sheaderclean semailtxthtml=TRIM$(MID$(semailfile,ltemp+2&,(INSTR(ltemp+2&,UCASE$(semailfile),"<ENDOFCRAZYEMAILSTUFF")-ltemp-19&))) ltemp=INSTR(UCASE$(semailfile),"<HEADER") sheader=TRIM$(MID$(sheader,ltemp,-1)) REM assigning variables from email info file ltemp=INSTR(UCASE$(sheader),$CRLF+"VERSION==")+10& IF ltemp THEN semailversion=MID$(sheader,ltemp+1&,INSTR(ltemp,sheader,$CRLF)-ltemp-1&) END IF ltemp=INSTR(UCASE$(sheader),$CRLF+"RETURNCODE==")+13& IF ltemp THEN semailreturncode=MID$(sheader,ltemp+1&,INSTR(ltemp,sheader,$CRLF)-ltemp-1&) END IF ltemp=INSTR(UCASE$(sheader),$CRLF+"RETURNFILE==")+13& IF ltemp THEN semailreturnfile=MID$(sheader,ltemp+1&,INSTR(ltemp,sheader,$CRLF)-ltemp-1&) END IF ltemp=INSTR(UCASE$(sheader),$CRLF+"METHOD==")+9& IF ltemp THEN semailmethod=MID$(sheader,ltemp+1&,INSTR(ltemp,sheader,$CRLF)-ltemp-1&) END IF ltemp=INSTR(UCASE$(sheader),$CRLF+"SECUREUSER==")+13& IF ltemp THEN semailsecureuser=MID$(sheader,ltemp+1&,INSTR(ltemp,sheader,$CRLF)-ltemp-1&) END IF ltemp=INSTR(UCASE$(sheader),$CRLF+"SECUREPASSWORD==")+17& IF ltemp THEN semailsecurepassword=MID$(sheader,ltemp+1&,INSTR(ltemp,sheader,$CRLF)-ltemp-1&) END IF ltemp=INSTR(UCASE$(sheader),$CRLF+"SMTPSERVER==")+13& semailsmtpserver=MID$(sheader,ltemp+1&,INSTR(ltemp,sheader,$CRLF)-ltemp-1&) ltemp=INSTR(UCASE$(sheader),$CRLF+"FROM==")+7& semailfromwho=MID$(sheader,ltemp+1&,INSTR(ltemp,sheader,$CRLF)-ltemp-1&) ltemp=INSTR(UCASE$(sheader),$CRLF+"TO==")+5& semailtowho=MID$(sheader,ltemp+1&,INSTR(ltemp,sheader,$CRLF)-ltemp-1&) ltemp=INSTR(UCASE$(sheader),$CRLF+"SUBJECT==")+10& semailsubject=MID$(sheader,ltemp+1&,INSTR(ltemp,sheader,$CRLF)-ltemp-1&) ltemp=INSTR(UCASE$(sheader),$CRLF+"ATTACHMENT1==")+14& IF ltemp THEN semailattachment1=MID$(sheader,ltemp+1&,INSTR(ltemp,sheader,$CRLF)-ltemp-1&) END IF ltemp=INSTR(UCASE$(sheader),$CRLF+"ATTACHMENT2==")+14& IF ltemp THEN semailattachment2=MID$(sheader,ltemp+1&,INSTR(ltemp,sheader,$CRLF)-ltemp-1&) END IF ltemp=INSTR(UCASE$(sheader),$CRLF+"ATTACHMENT3==")+14& IF ltemp THEN semailattachment3=MID$(sheader,ltemp+1&,INSTR(ltemp,sheader,$CRLF)-ltemp-1&) END IF ltemp=INSTR(UCASE$(sheader),$CRLF+"ATTACHMENT4==")+14& IF ltemp THEN semailattachment4=MID$(sheader,ltemp+1&,INSTR(ltemp,sheader,$CRLF)-ltemp-1&) END IF ltemp=INSTR(UCASE$(sheader),$CRLF+"ATTACHMENT5==")+14& IF ltemp THEN semailattachment5=MID$(sheader,ltemp+1&,INSTR(ltemp,sheader,$CRLF)-ltemp-1&) END IF ltemp=INSTR(UCASE$(semailtxthtml),"</MAINEMAILTXTMESSAGE") IF ltemp THEN semailtxt=MID$(semailtxthtml,1&,ltemp-1) ltemp=INSTR(UCASE$(semailtxt),"</MAINEMAILTXTMESSAGE") ltemp=INSTR(ltemp+20,semailtxt,$CRLF) semailtxt=MID$(semailtxt,ltemp+2&,-1) END IF ltemp=INSTR(UCASE$(semailtxthtml),"</MAINEMAILHTMLMESSAGE") IF ltemp THEN semailhtml=MID$(semailtxthtml,1&,ltemp-1) ltemp=INSTR(UCASE$(semailhtml),"<MAINEMAILHTMLMESSAGE") ltemp=INSTR(ltemp+20,semailhtml,$CRLF) semailhtml=MID$(semailhtml,ltemp+2&,-1) END IF stemp=TRIM$(semailtxt) IF stemp=$CRLF THEN semailtxt="" stemp=TRIM$(semailhtml) IF stemp=$CRLF THEN semailhtml="" IF INSTR(UCASE$(stemp),"<HTML")=0& THEN semailhtml="" IF INSTR(UCASE$(stemp),"</HTML")=0& THEN semailhtml="" IF lviewall THEN STDOUT "started parsing data finished" IF lviewall THEN STDOUT "version="+semailversion STDOUT "returncode="+semailreturncode STDOUT "returnfile="+semailreturnfile STDOUT "method="+semailmethod STDOUT "secureuser="+semailsecureuser STDOUT "securepassword="+semailsecurepassword STDOUT "smtpserver="+semailsmtpserver STDOUT "from="+semailfromwho STDOUT "to="+semailtowho STDOUT "subject="+semailsubject STDOUT "attachment1="+semailattachment1 STDOUT "attachment2="+semailattachment2 STDOUT "attachment3="+semailattachment3 STDOUT "attachment4="+semailattachment4 STDOUT "attachment5="+semailattachment5 END IF IF lviewall AND LEN(semailinternaluse)THEN STDOUT "-----start of internaluse text------" STDOUT semailinternaluse STDOUT "-----end of internaluse text------" END IF IF lviewall AND LEN(semailtxt)THEN STDOUT "-----start of email text------" STDOUT semailtxt STDOUT "-----end of email text------" END IF IF lviewall AND LEN(semailhtml)THEN STDOUT "-----start of html text------" STDOUT semailhtml STDOUT "-----end of html text------" END IF gssecuser=semailsecureuser gssecpassword=semailsecurepassword smtphost=semailsmtpserver hTcp = FREEFILE lsmtperror=0& HOST ADDR TO lipaddress HOST NAME lipaddress TO localhost IF lviewserver THEN STDOUT "trying to connect to "+smtphost TCP OPEN "smtp" AT SmtpHost AS hTCP TIMEOUT 20000 IF ERR THEN IF lviewserver THEN STDOUT "cannot connect to "+smtphost lsmtperror=10&:GOTO SENDERROR END IF DO WHILE NOT EOF(hTCP) TCP LINE hTCP, sline e = VAL(LEFT$(sline, 3)) LOOP IF lviewserver THEN STDOUT "server returned "+TRIM$(sline) IF e <> 220 THEN lsmtperror=11&:GOTO SENDERROR IF lviewserver THEN STDOUT "sending EHLO" TCP PRINT hTCP, "EHLO " & LOCALHOST DO WHILE NOT EOF(hTCP) TCP LINE hTCP, sline e = VAL(LEFT$(sline, 3)) LOOP IF lviewserver THEN STDOUT "server returned "+TRIM$(sline) IF e <> 250 THEN lsmtperror=12&:GOTO SENDERROR IF UCASE$(semailmethod)<> "SMTPAUTH" THEN GOTO DONOTAUTHENICATEUSER REM sme server should return "AUTH PLAIN LOGIN" here if IF INSTR(UCASE$(sline),"AUTH") =0& THEN GOTO DONOTAUTHENICATEUSER IF INSTR(UCASE$(sline),"PLAIN") =0& THEN GOTO DONOTAUTHENICATEUSER IF INSTR(UCASE$(sline),"LOGIN") =0& THEN GOTO DONOTAUTHENICATEUSER IF lviewserver THEN STDOUT "sending AUTH PLAIN " TCP PRINT hTcp, "AUTH PLAIN" DO WHILE NOT EOF(hTCP) TCP LINE hTCP, sline e = VAL(LEFT$(sline, 3)) LOOP IF lviewserver THEN STDOUT "server returned "+TRIM$(sline) IF e <> 334 THEN lsmtperror=21&:GOTO SENDERROR stemp = base64encode(CHR$(0)+TRIM$(gssecuser)+CHR$(0)+TRIM$(gssecpassword)) TCP PRINT htcp, stemp DO WHILE NOT EOF(hTCP) TCP LINE hTCP, sline e = VAL(LEFT$(sline, 3)) LOOP IF lviewserver THEN STDOUT "server returned "+TRIM$(sline) IF e <> 235 THEN lsmtperror=21&:GOTO SENDERROR DONOTAUTHENICATEUSER: IF lviewserver THEN STDOUT "sending MAIL FROM: <"+semailfromwho+">" TCP PRINT hTcp, "MAIL FROM: <" + semailfromwho+">" DO WHILE NOT EOF(hTCP) TCP LINE hTCP, sline e = VAL(LEFT$(sline, 3)) LOOP IF lviewserver THEN STDOUT "server returned "+TRIM$(sline) IF e <> 250 THEN lsmtperror=21&:GOTO SENDERROR IF lviewserver THEN STDOUT "sending RCPT TO: <"+semailtowho+">" TCP PRINT hTcp, "RCPT TO: <" + semailtowho+">" DO WHILE NOT EOF(hTCP) TCP LINE hTCP, sline e = VAL(LEFT$(sline, 3)) LOOP IF lviewserver THEN STDOUT "server returned "+TRIM$(sline) IF e <> 250 THEN lsmtperror=22&:GOTO SENDERROR IF lviewserver THEN STDOUT "sending the word DATA" TCP PRINT hTcp, "DATA" DO WHILE NOT EOF(hTCP) TCP LINE hTCP, sline e = VAL(LEFT$(sline, 3)) LOOP IF lviewserver THEN STDOUT "server returned "+TRIM$(sline) IF e <> 354 THEN lsmtperror=23&:GOTO SENDERROR IF lviewserver THEN STDOUT "sending Date: " + MailDate() STDOUT "sending From: " + semailfromwho STDOUT "sending To: " + semailtowho STDOUT "sending Subject: " + semailsubject STDOUT "sending as single blank line" END IF TCP PRINT hTCP, "Date: " + MailDate() TCP PRINT hTcp, "From: " + semailfromwho TCP PRINT hTcp, "To: " + semailtowho TCP PRINT hTcp, "Subject: " + semailsubject TCP PRINT hTcp, "" IF LEN(semailtxt) THEN IF lviewserver THEN STDOUT "sending email txt below" STDOUT semailtxt END IF emailmessage=semailtxt REPLACE $CRLF WITH $CR IN emailmessage WHILE LEN(emailmessage) Buffer = EXTRACT$(emailmessage, $CR) ' Make sure there are no lines containing a single period IF ASC(Buffer) = 46 THEN Buffer = "." + Buffer TCP PRINT hTcp, Buffer emailmessage = MID$(emailmessage, LEN(Buffer) + 2) WEND END IF ' Signal the end of the message IF lviewserver THEN STDOUT "sending a single PERIOD (.)" TCP PRINT hTcp, "." DO WHILE NOT EOF(hTCP) TCP LINE hTCP, sline e = VAL(LEFT$(sline, 3)) LOOP IF lviewserver THEN STDOUT "server returned "+TRIM$(sline) IF e <> 250 THEN lsmtperror=25&:GOTO SENDERROR ' Say goodbye IF lviewserver THEN STDOUT "sending a word QUIT" TCP PRINT hTcp, "QUIT" DO WHILE NOT EOF(hTCP) TCP LINE hTCP, sline e = VAL(LEFT$(sline, 3)) LOOP IF lviewserver THEN STDOUT "server returned "+TRIM$(sline) IF e <> 221 THEN lsmtperror=30&:GOTO SENDERROR IF lviewserver THEN STDOUT "connection to email closing" TCP CLOSE hTcp IF lviewserver THEN STDOUT "connection to email closed" FUNCTION=1 Done: EXIT FUNCTION SendError: FUNCTION=0 TCP CLOSE hTcp IF lviewserver THEN STDOUT "error" IF lviewserver THEN STDOUT "smtp program error code "+STR$(lsmtperror) GOTO Done END FUNCTION
Code:
<internaluse> </internaluse> <header> version==1 returncode==01 returnfile==mail.txt.rec method==smtpauth secureuser==johndoe securepassword==johndoe1234 smtpserver==mysmtpserver.com [email protected] [email protected] subject==What is happening girl attachment1==futureuse attachment2==futureuse attachment3==futureuse attachment4==futureuse attachment5==futureuse </header> <mainemailtxtmessage> Hi Mary! Are you going to the redeo tommorrow night? If so, I will be there too. </mainemailtxtmessage> <mainemailhtmlmessage> </mainemailhtmlmessage> <endofcrazyemailstuff>
Comment