'==============================================================================' ' SendFile example for PB/CC 2.0' Copyright (c) 1999 PowerBASIC, Inc.' '============================================================================== #COMPILE EXE '------------------------------------------------------------------------------ $mailhost = "mail.yourserver.com" $mailfrom = "address@yourserver.com" '------------------------------------------------------------------------------ GLOBAL hTcp AS LONG GLOBAL MailTo AS STRING GLOBAL Subject AS STRING GLOBAL file AS STRING '------------------------------------------------------------------------------ FUNCTION SendMail() AS LONG LOCAL Msg AS STRING LOCAL Buffer AS STRING LOCAL InBuff AS STRING LOCAL localhost AS STRING LOCAL i AS LONG LOCAL position AS LONG LOCAL length AS LONG LOCAL hFile AS LONG LOCAL b AS ASCIIZ * 4 LOCAL Enc AS STRING * 64 Enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ' ** Get the local host name HOST ADDR TO hTCP HOST NAME hTCP TO localhost ' ** Connect to mail server hTCP = FREEFILE TCP OPEN "smtp" AT $mailhost AS hTCP IF ERR THEN Buffer = "Error connecting to mailhost" GOTO SendError ELSE TCP LINE hTCP, Buffer IF LEFT$(Buffer, 3) <> "220" THEN GOTO SendError END IF END IF ' ** Greet the mailhost TCP PRINT hTCP, "HELO " + localhost TCP LINE hTCP, Buffer IF LEFT$(Buffer, 3) <> "250" THEN Buffer = "HELO error: " + Buffer GOTO SendError END IF ' ** Tell the mailhost who we are TCP PRINT hTCP, "MAIL FROM: <" + $mailfrom + ">" TCP LINE hTCP, Buffer IF LEFT$(Buffer, 3) <> "250" THEN Buffer = "MAIL FROM error: " + Buffer GOTO SendError END IF ' ** Tell the mailhost who the message is for TCP PRINT hTCP, "RCPT TO: <" + MailTo + ">" TCP LINE hTCP, Buffer IF LEFT$(Buffer, 3) <> "250" THEN Buffer = "RCPT TO error: " + Buffer GOTO SendError END IF ' ** Send the message TCP PRINT hTCP, "DATA" TCP LINE hTCP, Buffer IF LEFT$(Buffer, 3) <> "354" THEN Buffer = "DATA error: " + Buffer GOTO SendError END IF TCP PRINT hTCP, "From: " + $mailfrom TCP PRINT hTCP, "To: " + MailTo TCP PRINT hTCP, "Subject: " + Subject TCP PRINT hTCP, "X-Mailer: PowerBASIC SendFile example v1.0" TCP PRINT hTCP, "" 'end of header TCP PRINT hTCP, "MIME-Version: 1.0" TCP PRINT hTCP, "Content-Type: application/octet-stream; name=" + File TCP PRINT hTCP, "Content-transfer-encoding: base64" TCP PRINT hTCP, "" ' ** Send the file hFile = FREEFILE OPEN File FOR BINARY AS hFile '--- WHILE NOT EOF(hFile) GET$ hFile, 57, InBuff Buffer = "" WHILE LEN(InBuff) b = LEFT$(InBuff, 3) ! mov AL, b[0] ! shr AL, 2 ! movzx i, AL Buffer = Buffer + MID$(Enc, i+1, 1) ! mov AL, b[1] ! mov AH, b[0] ! shr AX, 4 ! and AL, &H3F ! movzx i, AL Buffer = Buffer + MID$(Enc, i+1, 1) IF LEN(InBuff) = 1 THEN Buffer = Buffer + "==" EXIT DO END IF ! mov AL, b[2] ! mov AH, b[1] ! shr AX, 6 ! and AL, &H3F ! movzx i, AL Buffer = Buffer + MID$(Enc, i+1, 1) IF LEN(InBuff) = 2 THEN Buffer = Buffer + "=" EXIT DO END IF ! mov AL, b[2] ! and AL, &H3F ! movzx i, AL Buffer = Buffer + MID$(Enc, i+1, 1) InBuff = MID$(InBuff, 4) WEND TCP PRINT hTCP, Buffer LOCATE ,1 WEND '--- CLOSE hFile TCP PRINT hTCP, "." TCP LINE hTCP, Buffer IF LEFT$(Buffer, 3) <> "250" THEN GOTO SendError END IF ' ** Say goodbye TCP PRINT hTCP, "QUIT" TCP LINE hTCP, Buffer IF LEFT$(Buffer, 3) <> "221" THEN Buffer = "QUIT error: " + Buffer GOTO SendError END IF TCP CLOSE hTCP FUNCTION = -1 Done: EXIT FUNCTION SendError: TCP CLOSE hTCP STDOUT Buffer GOTO Done END FUNCTION '------------------------------------------------------------------------------ FUNCTION PbMain () AS LONG STDOUT "SendFile v1.0 - Email a file from the command line" STDOUT "Copyright (c) 1999 PowerBASIC, Inc." STDOUT "" IF LEN(COMMAND$) = 0 THEN STDOUT "Usage: SENDFILE email@address.dom filename" EXIT FUNCTION END IF ' rudimentary command line parsing MailTo = PARSE$(COMMAND$, " ", 1) file = PARSE$(COMMAND$, " ", 2) Subject = "File attachment: " & file STDOUT "Sending " & file & " to " & MailTo & "..."; SendMail STDOUT $CRLF & "Done." END FUNCTION ------------- PowerBASIC Support