From: Christopher Pickslay Date: 2010-09-21T09:12:19+09:00 Subject: Re: Send email with attachments in Ruby Here's updated code using sendmail and Dir.glob instead of FileList: require 'rubygems' require 'action_mailer' require 'mime/types' ActionMailer::Base.delivery_method = :sendmail ActionMailer::Base.default_content_type = "text/plain" class Mailer < ActionMailer::Base def message (from, to, title, body, attachments) from from recipients to subject title body body Dir.glob(attachments).each do |path| file = File.basename(path) mime_type = MIME::Types.of(file).first content_type = mime_type ? mime_type.content_type : 'application/binary' attachment (content_type) do |a| a.body = File.read(path) a.filename = file a.transfer_encoding = 'quoted-printable' if content_type =~ /^text\// end end end end Mailer.deliver_message 'Christopher Pickslay ', 'to@somedomain.com', 'Some reports', "See attached tab-delimited reports\n\n", '/tmp/reports/*.txt' Dave Baldwin wrote: > On 3 Sep 2008, at 18:09, loominator1970 wrote: > >> 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 > > > I use this: > > > require 'rubygems' > require 'action_mailer' > require 'mime/types' > > ActionMailer::Base.smtp_settings = { :address => > '10.209.3.26', :domain => '3dlabs.com'} > > class Mailer < ActionMailer::Base > def message (title, body) > from 'Dave Baldwin ' > recipients 'dave.baldwin@3dlabs.com' > subject title > body body > > # Include all the pdf files in the PDF subdirectory as attachments. > FileList['PDF/*.pdf'].each do |path| > file = File.basename(path) > mime_type = MIME::Types.of(file).first > content_type = mime_type ? mime_type.content_type : 'application/ > binary' > attachment (content_type) do |a| > a.body = File.read(path) > a.filename = file > a.transfer_encoding = 'quoted-printable' if content_type =~ > /^text > \// > end > end > end > end > > Mailer.deliver_message('some title', 'the body message') > > > Dave. -- Posted via http://www.ruby-forum.com/.