|
I am trying SNDM API to send email attachment with base64 encoding but it is not working, I get the mail fine with attachment but attachment contains junk characters. Attachment of small sizes are working fine but PDF are not working at all. Attachment of greater size contains junk characters. I have also set up dtaara in QUSRSYS for not splitting the mail but it has also not helped. Please guide. Attach is the code D***************************************************************** D* IFS PROTOTYPES D***************************************************************** D*** OPEN AN IFS FILE DOPEN PR 10I 0 EXTPROC('open') D fileName * VALUE D OPENFLAGS 10I 0 VALUE D MODE 10U 0 VALUE OPTIONS(*NOPASS) D CODEPAGE 10U 0 VALUE OPTIONS(*NOPASS) D***************************************************************** D*** READ AN IFS FILE DREAD PR 10I 0 EXTPROC('read') D fileHandle 10I 0 VALUE D DATARECEIVED * VALUE D NBYTES 10U 0 VALUE D***************************************************************** D*** WRITE TO AN IFS FILE DWRITE PR 10I 0 EXTPROC('write') D fileHandle 10I 0 VALUE D DATATOWRITE * VALUE D NBYTES 10U 0 VALUE D***************************************************************** D*** CLOSE AN IFS FILE DCLOSE PR 10I 0 EXTPROC('close') D fileHandle 10I 0 VALUE D***************************************************************** D*** ADD RECIPIENT DADDRECIPIENT PR 280 D EmailAddr 256A CONST D RecipType 3A CONST D***************************************************************** D*** BASE 64 ENCODE DBASE64_ENCODE PR 10U 0 D Input * VALUE D InputLen 10U 0 VALUE D Output * VALUE D OutputSize 10U 0 VALUE D***************************************************************** D* IFS CONSTANTS D***************************************************************** D*** FILE ACCESS MODES FOR OPEN() D O_RDONLY S 10I 0 INZ(1) D O_WRONLY S 10I 0 INZ(2) D O_RDWR S 10I 0 INZ(4) D*** OFLAG VALUES FOR OPEN() D O_CREAT S 10I 0 INZ(8) D O_EXCL S 10I 0 INZ(16) D O_TRUNC S 10I 0 INZ(64) D*** FILE STATUS FLAGS FOR OPEN() AND FCNTL() D O_NONBLOCK S 10I 0 INZ(128) D O_APPEND S 10I 0 INZ(256) D*** OFLAG SHARE MODE VALUES FOR OPEN() D O_SHARE_NONE S 10I 0 INZ(2000000) D O_SHARE_RDONLY S 10I 0 INZ(0200000) D O_SHARE_RDWR S 10I 0 INZ(1000000) D O_SHARE_WRONLY S 10I 0 INZ(0400000) D*** FILE PERMISSIONS D S_IRUSR S 10I 0 INZ(256) D S_IWUSR S 10I 0 INZ(128) D S_IXUSR S 10I 0 INZ(64) D S_IRWXU S 10I 0 INZ(448) D S_IRGRP S 10I 0 INZ(32) D S_IWGRP S 10I 0 INZ(16) D S_IXGRP S 10I 0 INZ(8) D S_IRWXG S 10I 0 INZ(56) D S_IROTH S 10I 0 INZ(4) D S_IWOTH S 10I 0 INZ(2) D S_IXOTH S 10I 0 INZ(1) D S_IRWXO S 10I 0 INZ(7) D*** MISC D O_TEXTDATA S 10I 0 INZ(16777216) D O_CODEPAGE S 10I 0 INZ(8388608) D***************************************************************** D* DATA DEFINITIONS D***************************************************************** D*** MISCELLANEOUS DATA DECLARATIONS D fileName S 32 D fileLen S 10I 0 D* D fromPGM S 10 D sender S 255 D senderName S 64 varying D senderLen S 10I 0 D* D CPFNUMBER S LIKE(CPFID) D SUBJECT S 64 varying D MESSAGE S 32000 varying D ASCIICODEPAGE S 10U 0 INZ(819) D*** D toEmail S 256 varying D ccEmail S 256 varying D bccEmail S 256 varying D TOTALRECP S 10I 0 D*** D fileHandle S 10I 0 D bytesWritten S 10I 0 D* D workBuffer S 32000 varying D outBuffer S 32000 D TextData S 76A D bufferLen S 10I 0 C* D EOR S 2A INZ(X'0D25') D NULL S 1A INZ(X'00') D FULLNAME S 512A D RETURNINT S 10I 0 D atAmpersand S 10I 0 D today S D datfmt(*usa) D***************************************************************** D*** MIME HEADER FIELDS D mSender S 256 varying D mDateTime S 256 varying D mFrom S 256 varying D mMimeVersion S 256 varying D mTo S 256 varying D mCC S 256 varying D mCCi S 256 varying D mData S 1000 varying D mSubject S 256 varying D RECIP S 32767A D mBoundary S 256 INZ('--PART.BOUNDARY.1') varying D***************************************************************** D*** API ERROR INFO D APIERROR DS D APIBYTES 1 4B 0 D CPFID 9 15 D***************************************************************** D*** Mime file names D MIMEfile DS dtaara D charA 1 D eNum 9S 0 D***************************************************************** D*** ARRAY OF FILE ATTACHMENTS D ATTACHMENT DS D NBRFILES 1 2B 0 D ATTACHFILE 256A DIM(30) D POS S 5U 0 D SAVEPOS S LIKE(POS) D ATTACHNAME S 256A D BYTESREAD S 10I 0 D LEN_TEXT S 10I 0 D DATAREAD S 9899A D DATA S 9999A D ATTACHDESC S 10I 0 C***************************************************************** C *ENTRY PLIST C***************************************************************** C PARM toEmail C PARM subject C PARM message C PARM fromPgm C PARM Attachment D***************************************************************** C* MAIN LINE C***************************************************************** C *mdy move udate today C* C*** INITIALIZE ERROR STRUCTURE C EVAL APIBYTES = 11 C EVAL TOTALRECP = 0 C eval toEmail = %trim(toEmail) C* C* C* Add EMAILADDR to RECIP DS C eval RECIP = %Trim(ADDRECIPIENT(toEmail: C 'TO')) C* C if %len(bccEmail)> 0 C eval RECIP = %Trim(RECIP) + %Trim(ADDRECIPIENT C (bccEmail:'CCI')) C endif C* C* mime file name in the IFS C *lock in MIMEfile C add 1 eNum C out MIMEfile C eval fileName = '/HOME/TMP/' + mimeFile + '.txt' C eval fileLen = %LEN(%TRIMR(fileName)) C eval %SUBST(fileName:fileLen+1:2) = X'0000' C* C* C** EVAL TOTALRECP = 1 C* C*** WRITE MIME FILE C EXSR WRITEHDR C* C*** CALL API TO SEND E-MAIL C CALLB 'QtmmSendMail' C PARM fileName C PARM fileLen C PARM sender C PARM senderLen C PARM RECIP C PARM TOTALRECP C PARM APIERROR C* C RETURN C***************************************************************** C WRITEHDR BEGSR C***************************************************************** C*** OPEN FILE C EVAL fileHandle = OPEN(%ADDR(fileName) C : O_CREAT + O_WRONLY + O_TRUNC + C O_CODEPAGE C : S_IRWXU + S_IROTH C : ASCIICODEPAGE) C EVAL RETURNINT = CLOSE(fileHandle) C EVAL fileHandle = OPEN(%ADDR(fileName) C : O_TEXTDATA + O_RDWR) C* C*** BUILD MIME HEADER FIELDS C EVAL mSender = C 'SENDER: ' + %trimr(sender) C eval mDateTime = C 'SENT: ' + %char(today) C EVAL mFrom = C 'FROM: ' + C senderName + ' <' + C %TRIMR(sender) + '>' C EVAL mMimeVersion = C 'MIME-VERSION: 1.0' C* C eval mTO = 'TO: ' + toEmail C if %len(bccEmail) > 0 C eval mCCi = 'Cci: ' + bccEmail C endif C* C eval mData = %trim(mTO) + EOR C if mCci <> ' ' C eval mData = %trim(mData) + %trim(mCci) + C EOR C endif C* C* subject: C IF SUBJECT > *BLANKS C EVAL mSubject = C 'SUBJECT: ' + subject C ELSE C EVAL mSubject = C 'SUBJECT: ' C ENDIF C* C eval mSubject = mSubject + ' (' + senderName + ')' C* C* put together the whole buffer: C EVAL workBuffer = C mSender + eor + C mDateTime + eor + C mFrom + eor + C mMimeVersion + eor + C mData + C mSubject + eor + C 'CONTENT-TYPE: MULTIPART/MIXED; BOUNDARY=' + C '"' + mBoundary + '"' + C EOR + C EOR + C 'THIS IS A MULTI-PART MESSAGE IN MIME ' + C 'FORMAT.' + EOR + EOR + C '--' + mBoundary + C EOR + C 'CONTENT-TYPE: TEXT/PLAIN; ' + C 'CHARSET=iso8859-1'+ EOR + C 'CONTENT-TRANSFER-ENCODING: 8BIT' + C* EOR + C* 'CONTENT-DISPOSITION: INLINE' + C EOR + EOR + %trim(message) + C EOR + EOR + EOR + EOR + C '--' + mBoundary C* C*** add atachment file(s) if requested C IF NBRFILES > *ZERO C AND ATTACHFILE(1) <> '*NONE' C exsr doAttachment C ELSE C EVAL workBuffer = workBuffer + C '--' + EOR + EOR C movel workbuffer outbuffer C EXSR WRITEFILE C ENDIF C* C*** CLOSE FILE C EVAL RETURNINT = CLOSE(fileHandle) C*** C ENDSR C***************************************************************** C doAttachment BEGSR C***************************************************************** C*** ADD ATTACHMENT FILE(S) IF REQUESTED C movel workbuffer outbuffer C EXSR WRITEFILE C* C DO NBRFILES Z 5 0 C CLEAR SAVEPOS C EVAL POS = %SCAN('/':ATTACHFILE(Z):1) C* C DOW POS > *ZERO C EVAL SAVEPOS = POS C EVAL POS = %SCAN('/':ATTACHFILE(Z):POS+1) C ENDDO C* C IF SAVEPOS <> *ZERO C EVAL ATTACHNAME = %SUBST(ATTACHFILE(Z):SAVEPOS+1) C ELSE C EVAL ATTACHNAME = ATTACHFILE(Z) C ENDIF C* C EVAL WORKBUFFER = EOR + C 'CONTENT-TYPE: APPLICATION/OCTET' + C '-STREAM; NAME="' + C %TRIMR(ATTACHNAME) + '"' + C EOR + C 'CONTENT-DISPOSITION: ATTACHMENT; + C FILENAME="' + %TRIMR(ATTACHNAME) + '"' + C EOR + C 'CONTENT-TRANSFER-ENCODING: base64' + C EOR + EOR C* C movel workbuffer outbuffer C EXSR WRITEFILE C* C*** OPEN FILE C EVAL FULLNAME = %TRIMR(ATTACHFILE(Z)) + NULL C EVAL ATTACHDESC = OPEN(%ADDR(FULLNAME) C : O_RDONLY) C* C*** READ FROM FILE AND WRITE TO MIME FILE C EVAL BYTESREAD = READ(ATTACHDESC C : %ADDR(DATAREAD) C : %SIZE(DATAREAD)) C* C DOW BYTESREAD > 0 C EVAL LEN_TEXT = BASE64_ENCODE(%ADDR(DATAREAD) C :BYTESREAD C :%ADDR(DATA) C :%SIZE(DATA) ) C EVAL BYTESWRITTEN = WRITE(filehandle C : %ADDR(DATA) C : LEN_TEXT) C EVAL BYTESREAD = READ(ATTACHDESC C : %ADDR(DATAREAD) C : %SIZE(DATAREAD)) C ENDDO C* C* C*** CLOSE ATTACHMENT FILE AND WRITE TO MIME C EVAL RETURNINT = CLOSE(ATTACHDESC) C IF Z >= NBRFILES C EVAL workbuffer = EOR + C '--' + %TRIMR(MBOUNDARY) + '--' + C EOR + EOR C ELSE C EVAL workbuffer = EOR + C '--' + %TRIMR(MBOUNDARY) C ENDIF C movel workbuffer outbuffer C EXSR WRITEFILE C ENDDO C*** C ENDSR C***************************************************************** C WRITEFILE BEGSR C***************************************************************** C EVAL bytesWritten = WRITE(fileHandle C : %ADDR(outBuffer) C : %LEN(workBuffer)) C*** C ENDSR P***************************************************************** P ADDRECIPIENT B P***************************************************************** D ADDRECIPIENT PI 280A D EmailAddr 256A CONST D RecipType 3A CONST D* D*** DATA STRUCTURE OF RECIPIENT INFO. D RECIPIENT DS D OFFSET 1 4B 0 D ADDRLEN 5 8B 0 D FORMAT 9 16 D DISTRTYPE 17 20B 0 D RESERVED 21 24B 0 D ADDRESS 25 280 D*** CONSTANTS D DTO C CONST(0) D DCC C CONST(1) D DBCC C CONST(2) C* C EVAL FORMAT = 'ADDR0100' C EVAL TOTALRECP = TOTALRECP + 1 C* C SELECT C When Reciptype = 'CC' C EVAL DISTRTYPE = DCC C When RecipType = 'CCI' C EVAL DISTRTYPE = DBCC C Other C EVAL DISTRTYPE = DTO C ENDSL C* C EVAL RESERVED = 0 C eval address = EmailAddr C EVAL ADDRLEN = %LEN(%trim(address)) C EVAL OFFSET = 24 + ADDRLEN C* C return Recipient P ADDRECIPIENT E P***************************************************************** P BASE64_ENCODE B P***************************************************************** D BASE64_ENCODE PI 10U 0 D Input * Value D InputLen 10U 0 Value D Output * Value D OutputSize 10U 0 Value D* D B64Alphabet DS Static D Alphabet 64A Inz('- D ABCDEFGHIJKLMNOPQRSTUVWXYZ- D abcdefghijklmnopqrstuvwxyz- D 0123456789+/') D Base64 1A Dim(64) D Overlay(Alphabet) D DS D Numb 1 2U 0 INZ(0) D Byte 2 2A D* D Data DS Based(Input) D B1 1A D B2 1A D B3 1A D* D OutData S 4A Based(Output) D Out S 4A D POS S 10I 0 D OUTLEN S 10I 0 D SAVE S 1A D* C eval Pos =1 C* C DoW Pos <= InputLen C eval Byte = %BITAND(B1:x'FC') C eval Numb /= 4 C eval %Subst(Out:1) = Base64(Numb+1) C* C eval Byte = %BitAnd(B1:X'03') C eval Numb *= 16 C* C If (Pos+1) <= InputLen C eval Save = Byte C eval Byte = %BitAnd(B2:X'F0') C eval Numb /= 16 C eval Byte = %BitOr(Save:Byte) C Endif C* C eval %Subst(Out: 2) = Base64(Numb+1) C* C If (Pos+1 > InputLen) C eval %Subst(Out:3) = '=' C else C eval Byte = %Bitand(B2: X'0F') C eval Numb *= 4 C If (Pos+2 <= InputLen) C eval Save = Byte C eval Byte = %BitAnd(B3:X'C0') C eval Numb /= 64 C eval Byte = %BitOr(Save:Byte) C Endif C eval %Subst(Out:3) = Base64(Numb+1) C Endif C* C If (Pos+2 > InputLen) C eval %Subst(Out:4:1) = '=' C Else C eval Byte = %Bitand(B3 : X'3F') C eval %Subst(Out:4) = Base64(Numb+1) C Endif C* C eval Input += 3 C eval Pos += 3 C eval OutLen += 4 C* C If OutLen <= OutputSize C eval Outdata = Out C eval Output += 4 C Endif C* C Enddo C* C return Outlen P BASE64_ENCODE E
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.