Question

I use below function to create a hash of the whole content of a directory so I can send all files as attachments.

def get_attachments_from_directory(dir)
    attachment_to_send = Hash.new   
    Dir[dir.gsub("\\","/")+"/*"].each {|file|
        file_to_send = File.read(file)
        #file_to_send = File.read(file, :binmode => true)
        attachment_to_send[File.basename(file)]=file_to_send
    }
    return attachment_to_send
end

and then I use below function to send the attachments out

def email_it(body, subject, to, from, attachment_to_send)

  $smtp = 'mail.com'
  $smtp_port = 25


    Pony.mail(
        :to => to, 
        :from => from,
        :subject => subject, 
        :body => Nokogiri::HTML(body).text, 
        :html_body =>  body
        :attachments => attachment_to_send,
        :via => :smtp, 
        :via_options => {
                :address     => $smtp,
                :port     => $smtp_port,
                :enable_starttls_auto => false
        }
    )
end

There are two files in my testing directory: .log and .png. Both of them are sent and received but .png is corrupted. gmail said that the image file cannot be displayed because it contains errors. The file name of .png file is correct in my gmail account. The file size is wrong. Much much smaller.

Show original in gmail gives me

----==_mimepart_4fd9515347359_fc1e853c88342d
Date: Thu, 14 Jun 2012 12:49:55 +1000
Mime-Version: 1.0
Content-Type: image/png;
 charset=UTF-8;
 filename="error_when_time_out - login at 2012-06-14  12.48.55.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="error_when_time_out - login at 2012-06-14  12.48.55.png"
Content-ID: <4fd95153648c7_fc1e853c883518@RATionalxp.mail>

iVBORwq/XNErlnbxOOrmtdDZDYaMWm16lTatQptSpk4t12RW6HNq6IJGvvyB
sabbUovDe5+loc9U3yPX9Yr1vWJDv9Q4KNcPydUDQnkfV9LNFrTTOc2GrEZd
Rr0uo06fUUdn1jOZ9RxRnZBZJ2bXG3M4yoTplFqeWJFGFoVBjDanha5JoWOM
bx3hi0aTQPSQNcikNoMVeYrndSi9YeVl1jxLr07oNrrn11F1kv3AeL9C8Mpi
bkTrjvku73RaeOP6/KvXVv5yzfC6vfCqHf/H64Y/XNf//obujzf0f7lp+PMt
... it continues ....
Xvjq8X//p/Ocdy68s2/DZ//5/Muvf/rvt319XzQf8p9J+7wpSTTguXYPo3Dy
TYiIaNAvYXs5ir9gv4akEz5MOO6DxGPf150oPfApIe6Yu5SVblRBYgL1TrWq
QqWsUnFag5rYTagbCD4lJCgO2hYdpGzQteqR9NCgo3ZTmh0=


----==_mimepart_4fd9515347359_fc1e853c88342d--

inspect of the hash outputs

{"error_when_time_out - login at 2012-06-14  12.50.12.png"=>"\211PNG\n\277\\\321
+\226v\3618\352\346\265\320\331\r\206\214Zmz\2256\255B\233R\246N-\327dV\350sj\35
0\202F\276\374\201\261\246\333R\213\303{\237\245\241\317T\337#\327\365\212\365\2
75bC\277\3248(\327\017\311\325\003By\037W\322\315\026\264\3239\315\206\254F]F\27
5.\243N\237QGg\3263\231\365\034Q\235\220Y'f\327\es8\312\204\351\224Z\236X\221F\0
26\205A\2146\247\205\256I\241c\214o\035\341\213F\223@\364\2205\310\2446\203\025y

If I try to read the file with #file_to_send = File.read(file, :binmode => true) I get an error: TypeError - can't convert Hash into Integer:

  • ruby 1.8.7 (2010-08-16 patchlevel 302) [i386-mingw32]
  • mime-types (1.16)
  • pony (1.3)
Was it helpful?

Solution

The conventional way to read binary data without any CR+LF translation is:

File.open(file, 'rb').read

Ruby 1.9 introduces a few new ways to do this that you might be inadvertently trying in your 1.8.7 environment. The second argument to read is the number of bytes you want to read, not the mode of the file.

Be sure to read the documentation on any method you're unfamiliar with. Sometimes things aren't quite what you'd expect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top