Problem reading attachments in emails

I read the attachments in an email in the following manner:

def receive(email)
if email.has_attachments?
email.attachments.each do |a|
filename=a.original_filename“\”+filename,“w+”) { |f|
puts f.syswrite( # i tried f.write also
end # end of attachments
end # end if
end # end def

The attachments are pdf files,images,.doc,.xls files.
The problem now is the size of the files stored on the file system is
the same as that of the attachment. But the data that gets written to
the file is junk. Obviously i need to decode the content of the
attachment. Any suggestion.

I did try to read the parts of the mail as described in the post:

but in this case, only 2 kb of data gets written

Any suggestions?

Phew! Panicked too soon i guess, i figured what was wrong!
Since the attachments were binary files, the mode should have been
After changing the mode, it works perfectly fine."\"+filename,“wb”)