Send email with attachments in Ruby

L

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

## send email
require 'net/smtp'

# set up the email addresses
user_from = "(e-mail address removed)"
user_to = "(e-mail address removed)"

the_email = "From: (e-mail address removed)\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
 
B

Bertram Scharpf

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, "(e-mail address removed)"
m.headers.add :to, "(e-mail address removed)"
m.headers.add :bcc, %w([email protected] (e-mail address removed))
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
 
D

Dave Baldwin

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 <[email protected]>'
recipients '(e-mail address removed)'
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.
 
S

Slartibartfast

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

Ollivier Robert

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 :(

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

rmail has it on its TODO for years :(

Yet another are where Ruby is lacking compared to other languages...
 
C

Christopher Pickslay

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 <[email protected]>',
'(e-mail address removed)',
'Some reports',
"See attached tab-delimited reports\n\n",
'/tmp/reports/*.txt'


Dave said:
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 <[email protected]>'
recipients '(e-mail address removed)'
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top