WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Winsock
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Base64 Encoding

From: iain iain@caverock.net

Well it was not as hard as it looked. Here is some code that will base64 encode a file so that it can be included in an e-mail. Its not that fast... but it works :)

For those that don't know. The basic internet e-mail systems can only handle 7bit messages, this means that all e-mail must contain only text. This is a bit of an issue when wanting to send files via e-mail so what E-mail clients do it convert the binary (8bit) file into text (7bit). There are a number of encodeing methods that can be used UUEncode, Base64 and a new one called yEnc. I've used base64 which is described in RFC2045.

To add a file to an e-mail you need to encode the file and add the resulting text to the end of the message. and add the following line to the header of the message.

Content-Type: multipart/mixed; boundary="-"

***NOTE*** This is not needed if you are using the Postie Extender. You can use kSendFile to send the file. ***

This is only used if you are writing your own mail client via the WinSock extender sockets functions. It is also not used with the smtpSendMesage (sic) function in the WinSock extender Iain.


#DefineFunction Encode(source_file)
   source_file_size = FileSize(source_file)
   source_buffer = BinaryAlloc(source_file_size)
   dest = ""
   BinaryRead(source_buffer, source_file)
   pointer = 0
   For b = 0 To source_file_size By 3
      by1 = 0
      by2 = 0
      by3 = 0
      by1 = BinaryPeek(source_buffer, b)
      If (b+1) < source_file_size Then
         by2 = BinaryPeek(source_buffer, b+1)
         If (b+2) < source_file_size Then
            by3 = BinaryPeek(source_buffer, b+2)
         EndIf
      EndIf
      by4 = by1 >> 2
      by5 = ((by1&3)<<4)|(by2>>4)
      by6 = ((by2&15)<<2)|(by3>>6)
      by7 = by3&63
      dest = StrCat(dest,Encode_6bit(by4),Encode_6bit(by5))
      If (b+1) < source_file_size Then
         dest = StrCat(dest,Encode_6bit(by6))
      Else
         dest = StrCat(dest,"=")
      EndIf
      If (b+2) < source_file_size Then
         dest = StrCat(dest,Encode_6bit(by7))
      Else
         dest = StrCat(dest,"=")
      EndIf
      If StrLen(ItemExtract(-1,dest,@LF))==76 Then dest = StrCat(dest,@CRLF)
   Next
   file_name = ItemExtract(-1,source_file,"\")
   header = StrCat("---",@CRLF,'Content-Type: application/octet-stream; name="',file_name,'"',@CRLF,'Content-Transfer-Encoding: base64',@CRLF,'Content-Disposition: inline; filename="',file_name,'"',@CRLF,@CRLF)
   dest = StrCat(header,dest,@CRLF,"-----",@CRLF)
   BinaryFree(source_buffer)
   Return dest
#EndFunction


#DefineFunction Encode_6bit(bit)
   encode_list = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,+,/"
   Return ItemExtract(bit+1,encode_list,",")
#EndFunction


source_file = "c:\temp\test.bmp"
dest_file = "c:\temp\mytest.txt"
encoded_result = encode(source_file)
dest_file_handle = FileOpen(dest_file,"WRITE")
IntControl(53,0,0,0,0)
FileWrite(dest_file_handle,encoded_result)
IntControl(53,1,0,0,0)
FileClose(dest_file_handle)


Article ID:   W15877
File Created: 2004:03:30:15:41:20
Last Updated: 2004:03:30:15:41:20