I am using the following code:
'==============================================================================
'
' MailFile : Send E-mail with a file attachment using TCP statements
' This example derived from the article in the PowerBASIC Gazette, issue #33
'
' Donated to the Public Domain by PowerBASIC, Inc. 08 January 2003.
'
'==============================================================================
'-----------------------------------------------------------------------------
' Retrieve the current time and date in E-mail header format
'
'
FUNCTION MailDate () AS STRING
GLOBAL gmailhost$,gFile$,gMailFrom$,gMailTo$,gSubject$,gAttachment$
GLOBAL gline6$,gline7$,gline8$,gline9$,gline10$
GLOBAL gline11$,gline12$,gline13$,gline14$,gline15$
GET$ #1, LOF(1), gAttachment$
CLOSE #1
LOCAL szFormat AS ASCIIZ * 40
LOCAL szTemp AS ASCIIZ * 40
LOCAL sResult AS STRING
LOCAL t AS SYSTEMTIME
LOCAL tzone AS TIME_ZONE_INFORMATION
GetSystemTime 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)
FUNCTION = sResult + " " + szTemp + " UT"
END FUNCTION
'-----------------------------------------------------------------------------
' Encode binary file data using Base64 encoding for MIME.
'
SUB MimeEncode (sFileData AS STRING)
LOCAL lBlock AS LONG
LOCAL lcBlocks AS LONG
LOCAL lByte1 AS LONG
LOCAL lByte2 AS LONG
LOCAL lByte3 AS LONG
LOCAL lIndex1 AS LONG
LOCAL lIndex2 AS LONG
LOCAL lIndex3 AS LONG
LOCAL lIndex4 AS LONG
LOCAL pInput AS BYTE PTR
LOCAL pOutput AS BYTE PTR
LOCAL pTable AS BYTE PTR
LOCAL sBase64 AS STRING
LOCAL sResult AS STRING
' Set up Base64 translation table
sBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
' Round up the length of the input data to a multiple of three
lcBlocks = (LEN(sFileData) + 2) \ 3
IF lcBlocks * 3 > LEN(sFileData) THEN
sFileData = LSET$(sFileData, lcBlocks * 3 USING $NUL)
END IF
' Allocate the space for the output string
sResult = SPACE$(lcBlocks * 4)
' Set up pointers so we can treat the data as byte streams
pInput = STRPTR(sFileData)
pOutput = STRPTR(sResult)
pTable = STRPTR(sBase64)
' Loop through our entire input buffer
FOR lBlock = 1 TO lcBlocks
' Get the next three binary data bytes to process
lByte1 = @pInput
INCR pInput
lByte2 = @pInput
INCR pInput
lByte3 = @pInput
INCR pInput
' Translate the three data bytes into four Base64 table indices
lIndex1 = lByte1 \ 4
lIndex2 = (lByte1 AND 3) * 16 + lByte2 \ 16
lIndex3 = (lByte2 AND 15) * 4 + lByte3 \ 64
lIndex4 = lByte3 AND 63
' Use the Base64 table to encode the output string
@pOutput = @pTable[lIndex1]
INCR pOutput
@pOutput = @pTable[lIndex2]
INCR pOutput
@pOutput = @pTable[lIndex3]
INCR pOutput
@pOutput = @pTable[lIndex4]
INCR pOutput
NEXT
sFileData = sResult
END SUB
'-----------------------------------------------------------------------------
' The main application entry point.
'
FUNCTION SendMail AS LONG
LOCAL lERow&
LOCAL lRetry AS LONG
LOCAL lLine AS LONG
LOCAL nTCP AS LONG
LOCAL sLocalHost AS STRING
LOCAL sResponse AS STRING
LOCAL sBoundary AS STRING
gMailHost$="hex.namehub.com"
' Encode the file data in Base64 for MIME
MimeEncode gAttachment$
' Invent a unique file data boundary marker
sBoundary = "Boundary." + REMOVE$(GUIDTXT$(GUID$), ANY "{-}")
' Connect to E-mail server (mailhost)
nTCP = 100
Retry:
INCR lretry
TCP OPEN "smtp" AT gmailhost$ AS nTCP
gErrMsg$ = "Cannot connect to E-mail server: " + gmailhost$
IF ERR THEN
IF lRetry<>3 THEN
GOTO SendError
ELSE
GOTO Retry
END IF
END IF
TCP LINE nTCP, sResponse
IF LEFT$(sResponse, 3) <> "220" THEN
gErrMsg$="Error 220 sending E-mail!" & sResponse
GOTO SendError
END IF
' Get the local host name
HOST NAME TO sLocalHost
' Greet the mailhost
TCP PRINT nTCP, "HELO " + sLocalHost
TCP LINE nTCP, sResponse
IF LEFT$(sResponse, 3) <> "250" THEN
gErrMsg$="Error 250-1 sending E-mail!" + sResponse
GOTO SendError
END IF
' Tell the mailhost who we are
TCP PRINT nTCP, "MAIL FROM: <" + gmailfrom$ + ">"
TCP LINE nTCP, sResponse
IF LEFT$(sResponse, 3) <> "250" THEN
gErrMsg$="Error 250-2 sending E-mail!" + sResponse
GOTO SendError
END IF
' Tell the mailhost who the message is for
TCP PRINT nTCP, "RCPT TO: <" + gMailTo$ + ">"
TCP LINE nTCP, sResponse
IF LEFT$(sResponse, 3) <> "250" THEN
gErrMsg$="Error 250-3 sending E-mail!" + sResponse
GOTO SendError
END IF
' Send the message
TCP PRINT nTCP, "DATA"
TCP LINE nTCP, sResponse
IF LEFT$(sResponse, 3) <> "354" THEN
gErrMsg$="Error 354 sending E-mail!" + sResponse
GOTO SendError
END IF
'The E-mail header
TCP PRINT nTCP, "Date: " + TRIM$(MailDate)
TCP PRINT nTCP, "From: " + TRIM$(gmailfrom$)
TCP PRINT nTCP, "To: " + TRIM$(gMailTo$)
TCP PRINT nTCP, "Subject: " + TRIM$(gSubject$)
TCP PRINT nTCP, "X-Mailer: Company Name"
TCP PRINT nTCP, "MIME-Version: 1.0"
TCP PRINT nTCP, "Content-Type: multipart/mixed; boundary=" + $DQ + sBoundary + $DQ
' Create an HTML section
TCP PRINT nTCP, ""
TCP PRINT nTCP, "--" + sBoundary
TCP PRINT nTCP, "Content-Type: text/html; charset=" + $DQ + "us-ascii" + $DQ
TCP PRINT nTCP, "Content-Transfer-Encoding: 7bit"
TCP PRINT nTCP, ""
OPEN "C:\Output.txt" FOR OUTPUT AS #1
' Create a main E-mail body section
TCP PRINT nTCP, "<html><body><pre>"
FOR lERow&= 1 TO gERow&
gLine(lERow&)=gLine(lERow&) & " "
TCP PRINT nTCP,gLine(lERow&)
PRINT #1,gLine(lERow&)
NEXT lERow&
TCP PRINT nTCP, "</pre></body></html>"
CLOSE #1000
' Now finish the E-mail off as we're done sending the message
TCP PRINT nTCP, "."
TCP LINE nTCP, sResponse
IF LEFT$(sResponse, 3) <> "250" THEN
gErrMsg$="Error 250-4 sending E-mail!" + sResponse
GOTO SendError
END IF
' Say goodbye
TCP PRINT nTCP, "QUIT"
TCP LINE nTCP, sResponse
IF LEFT$(sResponse, 3) <> "221" THEN
gErrMsg$="Error 221 sending E-mail!" + sResponse
GOTO sendError
END IF
TCP CLOSE nTCP
EXIT FUNCTION
SendError:
LOCAL lAns&
CLOSE
gErrMsg$=gErrMsg$ + $CRLF + "Contact Supervisor or Network Administrator{S}"
lAns&=EZ_MsgBox("SL","SYSTEM HALTED!! " & gErrMsg$,"System Error","OK")
END FUNCTION
to email order information. We just changed email hosts, and although I entered the new host information, we can only email to addresses within our own company. Emails sent to other addresses are not going through. What do I need to change in order to email out?
'==============================================================================
'
' MailFile : Send E-mail with a file attachment using TCP statements
' This example derived from the article in the PowerBASIC Gazette, issue #33
'
' Donated to the Public Domain by PowerBASIC, Inc. 08 January 2003.
'
'==============================================================================
'-----------------------------------------------------------------------------
' Retrieve the current time and date in E-mail header format
'
'
FUNCTION MailDate () AS STRING
GLOBAL gmailhost$,gFile$,gMailFrom$,gMailTo$,gSubject$,gAttachment$
GLOBAL gline6$,gline7$,gline8$,gline9$,gline10$
GLOBAL gline11$,gline12$,gline13$,gline14$,gline15$
GET$ #1, LOF(1), gAttachment$
CLOSE #1
LOCAL szFormat AS ASCIIZ * 40
LOCAL szTemp AS ASCIIZ * 40
LOCAL sResult AS STRING
LOCAL t AS SYSTEMTIME
LOCAL tzone AS TIME_ZONE_INFORMATION
GetSystemTime 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)
FUNCTION = sResult + " " + szTemp + " UT"
END FUNCTION
'-----------------------------------------------------------------------------
' Encode binary file data using Base64 encoding for MIME.
'
SUB MimeEncode (sFileData AS STRING)
LOCAL lBlock AS LONG
LOCAL lcBlocks AS LONG
LOCAL lByte1 AS LONG
LOCAL lByte2 AS LONG
LOCAL lByte3 AS LONG
LOCAL lIndex1 AS LONG
LOCAL lIndex2 AS LONG
LOCAL lIndex3 AS LONG
LOCAL lIndex4 AS LONG
LOCAL pInput AS BYTE PTR
LOCAL pOutput AS BYTE PTR
LOCAL pTable AS BYTE PTR
LOCAL sBase64 AS STRING
LOCAL sResult AS STRING
' Set up Base64 translation table
sBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
' Round up the length of the input data to a multiple of three
lcBlocks = (LEN(sFileData) + 2) \ 3
IF lcBlocks * 3 > LEN(sFileData) THEN
sFileData = LSET$(sFileData, lcBlocks * 3 USING $NUL)
END IF
' Allocate the space for the output string
sResult = SPACE$(lcBlocks * 4)
' Set up pointers so we can treat the data as byte streams
pInput = STRPTR(sFileData)
pOutput = STRPTR(sResult)
pTable = STRPTR(sBase64)
' Loop through our entire input buffer
FOR lBlock = 1 TO lcBlocks
' Get the next three binary data bytes to process
lByte1 = @pInput
INCR pInput
lByte2 = @pInput
INCR pInput
lByte3 = @pInput
INCR pInput
' Translate the three data bytes into four Base64 table indices
lIndex1 = lByte1 \ 4
lIndex2 = (lByte1 AND 3) * 16 + lByte2 \ 16
lIndex3 = (lByte2 AND 15) * 4 + lByte3 \ 64
lIndex4 = lByte3 AND 63
' Use the Base64 table to encode the output string
@pOutput = @pTable[lIndex1]
INCR pOutput
@pOutput = @pTable[lIndex2]
INCR pOutput
@pOutput = @pTable[lIndex3]
INCR pOutput
@pOutput = @pTable[lIndex4]
INCR pOutput
NEXT
sFileData = sResult
END SUB
'-----------------------------------------------------------------------------
' The main application entry point.
'
FUNCTION SendMail AS LONG
LOCAL lERow&
LOCAL lRetry AS LONG
LOCAL lLine AS LONG
LOCAL nTCP AS LONG
LOCAL sLocalHost AS STRING
LOCAL sResponse AS STRING
LOCAL sBoundary AS STRING
gMailHost$="hex.namehub.com"
' Encode the file data in Base64 for MIME
MimeEncode gAttachment$
' Invent a unique file data boundary marker
sBoundary = "Boundary." + REMOVE$(GUIDTXT$(GUID$), ANY "{-}")
' Connect to E-mail server (mailhost)
nTCP = 100
Retry:
INCR lretry
TCP OPEN "smtp" AT gmailhost$ AS nTCP
gErrMsg$ = "Cannot connect to E-mail server: " + gmailhost$
IF ERR THEN
IF lRetry<>3 THEN
GOTO SendError
ELSE
GOTO Retry
END IF
END IF
TCP LINE nTCP, sResponse
IF LEFT$(sResponse, 3) <> "220" THEN
gErrMsg$="Error 220 sending E-mail!" & sResponse
GOTO SendError
END IF
' Get the local host name
HOST NAME TO sLocalHost
' Greet the mailhost
TCP PRINT nTCP, "HELO " + sLocalHost
TCP LINE nTCP, sResponse
IF LEFT$(sResponse, 3) <> "250" THEN
gErrMsg$="Error 250-1 sending E-mail!" + sResponse
GOTO SendError
END IF
' Tell the mailhost who we are
TCP PRINT nTCP, "MAIL FROM: <" + gmailfrom$ + ">"
TCP LINE nTCP, sResponse
IF LEFT$(sResponse, 3) <> "250" THEN
gErrMsg$="Error 250-2 sending E-mail!" + sResponse
GOTO SendError
END IF
' Tell the mailhost who the message is for
TCP PRINT nTCP, "RCPT TO: <" + gMailTo$ + ">"
TCP LINE nTCP, sResponse
IF LEFT$(sResponse, 3) <> "250" THEN
gErrMsg$="Error 250-3 sending E-mail!" + sResponse
GOTO SendError
END IF
' Send the message
TCP PRINT nTCP, "DATA"
TCP LINE nTCP, sResponse
IF LEFT$(sResponse, 3) <> "354" THEN
gErrMsg$="Error 354 sending E-mail!" + sResponse
GOTO SendError
END IF
'The E-mail header
TCP PRINT nTCP, "Date: " + TRIM$(MailDate)
TCP PRINT nTCP, "From: " + TRIM$(gmailfrom$)
TCP PRINT nTCP, "To: " + TRIM$(gMailTo$)
TCP PRINT nTCP, "Subject: " + TRIM$(gSubject$)
TCP PRINT nTCP, "X-Mailer: Company Name"
TCP PRINT nTCP, "MIME-Version: 1.0"
TCP PRINT nTCP, "Content-Type: multipart/mixed; boundary=" + $DQ + sBoundary + $DQ
' Create an HTML section
TCP PRINT nTCP, ""
TCP PRINT nTCP, "--" + sBoundary
TCP PRINT nTCP, "Content-Type: text/html; charset=" + $DQ + "us-ascii" + $DQ
TCP PRINT nTCP, "Content-Transfer-Encoding: 7bit"
TCP PRINT nTCP, ""
OPEN "C:\Output.txt" FOR OUTPUT AS #1
' Create a main E-mail body section
TCP PRINT nTCP, "<html><body><pre>"
FOR lERow&= 1 TO gERow&
gLine(lERow&)=gLine(lERow&) & " "
TCP PRINT nTCP,gLine(lERow&)
PRINT #1,gLine(lERow&)
NEXT lERow&
TCP PRINT nTCP, "</pre></body></html>"
CLOSE #1000
' Now finish the E-mail off as we're done sending the message
TCP PRINT nTCP, "."
TCP LINE nTCP, sResponse
IF LEFT$(sResponse, 3) <> "250" THEN
gErrMsg$="Error 250-4 sending E-mail!" + sResponse
GOTO SendError
END IF
' Say goodbye
TCP PRINT nTCP, "QUIT"
TCP LINE nTCP, sResponse
IF LEFT$(sResponse, 3) <> "221" THEN
gErrMsg$="Error 221 sending E-mail!" + sResponse
GOTO sendError
END IF
TCP CLOSE nTCP
EXIT FUNCTION
SendError:
LOCAL lAns&
CLOSE
gErrMsg$=gErrMsg$ + $CRLF + "Contact Supervisor or Network Administrator{S}"
lAns&=EZ_MsgBox("SL","SYSTEM HALTED!! " & gErrMsg$,"System Error","OK")
END FUNCTION
to email order information. We just changed email hosts, and although I entered the new host information, we can only email to addresses within our own company. Emails sent to other addresses are not going through. What do I need to change in order to email out?
Comment