[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Send email with attachments in Ruby

loominator1970

9/3/2008 5:12:00 PM

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 = "my_email@here.com"
user_to = "their_email@there.com"

the_email = "From: my_email@here.com\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
6 Answers

Lloyd Linklater

9/3/2008 6:31:00 PM

0

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-...topic...
http://www.ruby-...topic...
--
Posted via http://www.ruby-....

Bertram Scharpf

9/3/2008 7:29:00 PM

0

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.co... cropmail
fetch http://bertram-scharpf.homelinux.co...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-...

Dave Baldwin

9/4/2008 7:23:00 AM

0


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 <dave.baldwin@3dlabs.com>'
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.


Slartibartfast

9/4/2008 10:29:00 AM

0

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

Bob Schäfer

9/4/2008 1:13:00 PM

0

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.

Ollivier Robert

9/5/2008 8:49:00 AM

0

In article <48bfdeb2$0$23194$ec3e2dad@news.usenetmonster.com>,
Bob Schäfer <bob-usenet@removeme.sonicpond.com> 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 :(

You could try SimpleMail (http://simplemail.rubyforg...) from
RubyForge.

rmail has it on its TODO for years :(

Yet another are where Ruby is lacking compared to other languages...
--
Ollivier ROBERT -=- EEC/RIF/SEU -=-
Systems Engineering Unit