How to send a schedule by email in Ruby without saving to disk?

I use a Ruby script and "mail" to send emails.

Question. How can I send a graph by email in Ruby without saving to disk? Is it possible? What graphic tool would you recommend, and how would the "mail" gem-interface somehow sink it? (or this is what you need to save to disk first). If possible / simple string coding of lines of code, how cool it would be ....

+4
source share
2 answers

Your complete answer.

For simplicity, a pure PNG Ruby graph is used; a real-world application is likely to use SVG or a fast native code or graphics API.

#!/usr/bin/env ruby =begin How to send a graph via email in Ruby without saving to disk Example code by Joel Parker Henderson at SixArm, joel@sixarm.com http://stackoverflow.com/questions/9779565 You need two gems: gem install chunky_png gem install mail Documentation: http://rdoc.info/gems/chunky_png/frames https://github.com/mikel/mail =end # Create a simple PNG image from scratch with an x-axis and y-axis. # We use ChunkyPNG because it pure Ruby and easy to write results; # a real-world app would more likely use an SVG library or graph API. require 'chunky_png' png = ChunkyPNG::Image.new(100, 100, ChunkyPNG::Color::WHITE) png.line(0, 50, 100, 50, ChunkyPNG::Color::BLACK) # x-axis png.line(50, 0, 50, 100, ChunkyPNG::Color::BLACK) # y-axis # We do IO to a String in memory, rather than to a File on disk. # Ruby does this by using the StringIO class which akin to a stream. # For more on using a string as a file in Ruby, see this blog post: # http://macdevelopertips.com/ruby/using-a-string-as-a-file-in-ruby.html io = StringIO.new png.write(io) io.rewind # Create a mail message using the Ruby mail gem as usual. # We create it item by item; you may prefer to create it in a block. require 'mail' mail = Mail.new mail.to = ' alice@example.com ' mail.from = ' bob@example.com ' mail.subject = 'Hello World' # Attach the PNG graph, set the correct mime type, and read from the StringIO mail.attachments['graph.png'] = { :mime_type => 'image/png', :content => io.read } # Send mail as usual. We choose sendmail because it bypasses the OpenSSL error. mail.delivery_method :sendmail mail.deliver 
+7
source

I do not understand why you could not. In the mailing documentation you can see this sample code:

 mail = Mail.new do from ' me@test.lindsaar.net ' to ' you@test.lindsaar.net ' subject 'Here is the image you wanted' body File.read('body.txt') add_file :filename => 'somefile.png', :content => File.read('/somefile.png') end mail.deliver! 

You just need to replace the target :content => ... with the contents of your file in memory. And that should be enough. There is no real need to have attached files saved, even temporarily, to disk, as they are re-encoded in base64 and added at the end of your mail.

In the second part of your question there are many graphical / graphical libraries. See this question or this lib for example.

In fact, in fact there is no one libra over the others. There are many libs for many different uses, and you need to choose what suits your needs and your limitations best.

+5
source

Source: https://habr.com/ru/post/1402342/


All Articles