From: Bertram Scharpf Date: 2008-09-04T04:28:56+09:00 Subject: Re: Send email with attachments in Ruby Hi, Am Donnerstag, 04. Sep 2008, 02:09:27 +0900 schrieb loominator1970: > I'm trying to find some code to send an email with attachment in > ruby. I'm using the following code to send an email, but I would like > to send an PDF. Can someone please tell me what code i need to add to > include a file called "the_attachment.pdf"? Thanks, Dave Two years ago I started to write my own mail library. Initially I only thought of filtering mails. Soon I started to generate mails, too. In the meantime I filtered about 100,000 mails and I generated about 25,000. If you like to have a look at it, please do something like: gem fetch --source http://bertram-scharpf.homelinux.com/bs_gems/ cropmail fetch http://bertram-scharpf.homelinux.com/bs_gems/cropmail-1.6.gem It isn't documented very well because nobody uses it but me, but it is thoroughly tested and it is very easy to use. Below I will cite some sample code how I generate a mail. I would be pleased if you like it. In case you don't I wish you good luck anyway. Bertram ---------------------------------------------------------------- require "bs-net/mail" require "stringio" m = BsNet::Mail.new m.headers.add :from, "sender@example.com" m.headers.add :to, "receiver@example.com" m.headers.add :bcc, %w(x@example.net y@example.net) m.headers.add :date, Time.now.rfc822 m.headers.add :subject, "Some useless information" plain = BsNet::Message.new ct = BsNet::ContentType.new "text/plain", :charset => "utf-8" plain.headers.add :content_type, ct plain.headers.add :content_transfer_encoding, "8bit" StringIO.open plain.body do |b| b.puts "This mail is coming with an attachment." end m.push plain attachment = BsNet::Message.new ct = BsNet::ContentType.new "text/comma-separated-values", :charset => "utf-8", :name => "somefile.csv" attachment.headers.add :content_type, ct attachment.headers.add :content_transfer_encoding, "8bit" cf = BsNet::ContentField.new "inline", :filename => "somefile.csv" attachment.headers.add :content_disposition, cf StringIO.new attachment.body do |b| b.puts somedata end m.push attachment m.sendmail -- Bertram Scharpf Stuttgart, Deutschland/Germany http://www.bertram-scharpf.de