Send email with attachments in Ruby

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

send email

require ‘net/smtp’

set up the email addresses

user_from = “[email protected]
user_to = “[email protected]

the_email = “From: [email protected]\n” +
“Subject: Email with attachment\n\n” +
“See attached PDF.\n\n”

handling exceptions

begin
Net::SMTP.start(‘localhost’) do |smtp|
smtp.send_message(the_email, user_from, user_to)
end

rescue Exception => e
print "Exception occured: " + e
end

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

http://www.ruby-forum.com/topic/60504#new
http://www.ruby-forum.com/topic/64747#new

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, “[email protected]
m.headers.add :to, “[email protected]
m.headers.add :bcc, %w([email protected] [email protected])
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

I don’t think there is an easy way to do so with net/smtp, put
actionmailer can handle attacments, I believe.

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 B. [email protected]
recipients ‘[email protected]
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.

In article [email protected],
Bob Schäfer [email protected] wrote:

if there’s a ruby way to do this, i’d love to know what it is. i
always feel a little dirty doing it like this.

There is no easy way to play with this :frowning:

You could try SimpleMail (http://simplemail.rubyforge.org/doc/) from
RubyForge.

rmail has it on its TODO for years :frowning:

Yet another are where Ruby is lacking compared to other languages…

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
[snip code example]
/usr/ports/mail/mutt-lite installed.

well, i know it’s low-brow, but i’ve been doing it like this:

system(“cat #{bodyfile} | mutt -s #{subjectstring} -a #{attachmentfile}
#{recipient}”)

keep in mind, this is on FreeBSD servers with mutt installed.

cd /usr/ports/mail/mutt-lite && make install clean

also, if you call this from cron (who wouldn’t) you’ll need to put the
full path of mutt in your system call, typically /usr/local/bin/mutt.

if there’s a ruby way to do this, i’d love to know what it is. i
always feel a little dirty doing it like this.

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 P. [email protected]’,
[email protected]’,
‘Some reports’,
“See attached tab-delimited reports\n\n”,
‘/tmp/reports/*.txt’

Dave B. 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 B. [email protected]
recipients ‘[email protected]
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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs