Ruby Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript



Ruby
Examples

Quick Start
Ruby Unicode
Ruby Byte Array
Ruby Certs
Ruby Email
Ruby Encryption
Ruby FTP
HTML-to-XML
Ruby HTTP
Ruby IMAP
Ruby MHT
Ruby MIME
Ruby S/MIME
Ruby Signatures
Ruby RSA
Ruby Socket
Ruby Spider
Ruby Tar
Ruby Upload
Ruby XML
Ruby XMP
Ruby Zip

More Examples...
String
Email Object
POP3
SMTP
RSS
Atom
Self-Extractor

Unreleased...
Service
PPMD
Deflate
Bzip2
LZW
Bz2
DH Key Exchange
DSA
Icon

 

 

 

 

 

 

 

Access Email Attachment Data In-Memory

Sample Ruby script to access the contents of email attachments in-memory.

Download Ruby Programming Example Scripts

# file: AttachmentData.rb

# Ruby script to access email attachment contents in-memory.

require 'chilkat'

# Create an instance of the mailman object for reading POP3 email.
mailman = Chilkat::CkMailMan.new()
mailman.UnlockComponent("anything for 30-day trial")

# Create an email object and load it with a .eml file.
email = Chilkat::CkEmail.new()
email.LoadEml("testWithAttach.eml")

# Loop over the attachments.
numAttach = email.get_NumAttachments()
filename = Chilkat::CkString.new()
contentType = Chilkat::CkString.new()
textData = Chilkat::CkString.new()
binaryData = Chilkat::CkByteData.new()

for j in 0..(numAttach-1)
	# Get the attachment filename
	email.GetAttachmentFilename(j,filename)
	# Get the attachment content-type
	email.GetAttachmentContentType(j,contentType)
	
	# If the content type begins with "text/", such as text/plain or text/xml,
	# then extract the contents of the attachment into a CkString object.
	if contentType.beginsWith("text/") 
		# When fetching the content of a text attachment, you should only do so
		# if you know the character encoding of the data, such as utf-8 or
		# ANSI (which is typically iso-8859-1, a.k.a. latin1, or windows-1252 for
		# Western-European languages).  If you don't know the charset, you should
		# access the attachment data as binary to prevent the Chilkat component
		# from interpreting the character according to a character encoding.
		email.GetAttachmentString(j,"iso-8859-1",textData)
		
		# Print the filename and contents of the text file:
		print filename.getUtf8() + ":\n" + textData.getUtf8() + "\n-----\n\n"
		
	else
		# This is a binary attachment such as image/gif, application/octet-stream, video/quicktime, etc.
		email.GetAttachmentData(j,binaryData)
		
		print filename.getUtf8() + " (" + contentType.getUtf8() + ")\n"
		
		# print the decimal value of the 1st 2 bytes.
		print binaryData.getByte(0).to_s() + ", " + binaryData.getByte(1).to_s() + "\n"
		
		# Save this attachment to a file.
		binaryData.saveFile("binaryAttachments/" + filename.getUtf8())
	end
	
end





 

Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2007 Chilkat Software, Inc. All Rights Reserved.