[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

simple mail with Ruby

Peter Bailey

1/15/2008 7:54:00 PM

Hi,
I just want Ruby to enable me to send simple e-mails, sometimes with an
attachment, if possible. Here's all I want:

To: pbailey@bna.com (that's me, as a test)
From: my account, whatever
Message: Some test text. More test text. And so on.
Attach: c:\simplestuff.pdf

The e-mail server here at my company is Lotus Notes. I've used it for
years as an SMTP server.

Thanks,
Peter
--
Posted via http://www.ruby-....

4 Answers

ara.t.howard

1/15/2008 8:57:00 PM

0


On Jan 15, 2008, at 12:54 PM, Peter Bailey wrote:

> Hi,
> I just want Ruby to enable me to send simple e-mails, sometimes
> with an
> attachment, if possible. Here's all I want:
>
> To: pbailey@bna.com (that's me, as a test)
> From: my account, whatever
> Message: Some test text. More test text. And so on.
> Attach: c:\simplestuff.pdf
>
> The e-mail server here at my company is Lotus Notes. I've used it for
> years as an SMTP server.
>
> Thanks,
> Peter
> --
> Posted via http://www.ruby-....
>


require "net/smtp"

class Email
class Error < ::StandardError; end
USER = ENV["USER"] || ENV["USERNAME"] || ENV["LOGNAME"]

attr_accessor "user"
attr_accessor "from"
attr_accessor "to"
attr_accessor "subject"
attr_accessor "message"
attr_accessor "sent"
alias_method "sent?", "sent"

def initialize *a, &b
options, messages = a.partition{|x| Hash === x}
opts = options.inject({}){|h,o| h.update o}
@user = opts["user"] || opts[:user] || USER
@to = opts["to"] || opts[:to] || "#{ @user }@localhost.localdomain"
@from = opts["from"] || opts[:from] || "#{ @user }
@localhost.localdomain"
@subject = opts["subject"] || opts[:subject] || "[RUBY EMAIL]"
@sent = false
@message =
if messages.empty?
opts["message"] || opts[:message]
else
messages.join
end
@message = b.call(self) if b
email if @message
end

def email
raise Error, "already sent!" if @sent
msg = "To: %s\nFrom: %s\nSubject: %s\n\n%s" % [@to, @from,
@subject, @message]
Net::SMTP.start("localhost"){|smtp| smtp.send_message msg, @to,
@from }
@sent = true
self
end
alias_method "email!", "email"
end

EMail = Email

def EMail *a, &b
EMail::new *a, &b
end
def Email *a, &b
Email::new *a, &b
end
def email *a, &b
Email::new *a, &b
end

if $0 == __FILE__
p(EMail("test 1 @ #{ Time::now }", "to" =>
"ara.t.howard@noaa.gov", "from" => "ara.t.howard@noaa.gov"))
p(EMail("to" => "ara.t.howard@noaa.gov", "from" =>
"ara.t.howard@noaa.gov"){ "test 2 @ #{ Time::now }" })
p(EMail("to" => "ara.t.howard@noaa.gov"){ "test 3 @ #
{ Time::now }" })
end


a @ http://codeforp...
--
it is not enough to be compassionate. you must act.
h.h. the 14th dalai lama




Peter Bailey

1/16/2008 8:57:00 PM

0

Wow! I need to look closely at this. This looks incredibly complicated
to me. How come you literally had to go in and create your own class?
Isn't there anything already available?

I'll dive in. Thanks a lot,
Peter


--
Posted via http://www.ruby-....

ara.t.howard

1/16/2008 9:03:00 PM

0


On Jan 16, 2008, at 1:57 PM, Peter Bailey wrote:

> Wow! I need to look closely at this. This looks incredibly complicated
> to me. How come you literally had to go in and create your own class?
> Isn't there anything already available?
>
> I'll dive in. Thanks a lot,
> Peter
>
>
> --
> Posted via http://www.ruby-....
>

sure.

msg = "To: %s\nFrom: %s\nSubject: %s\n\n%s" % [@to, @from,
@subject, @message]
Net::SMTP.start("localhost"){|smtp| smtp.send_message msg, @to,
@from }

you'll need to encode you attachments by hand though. the class i
posted is just a wrapper on doing the above by hand each time.
mailfactory is really nice for building the actual email body too.
as far as built in is concerned though - net/smtp is as high level as
it gets.

regards.

a @ http://draw...
--
sleep is the best meditation.
h.h. the 14th dalai lama




ggolebio

1/20/2008 3:10:00 PM

0

Peter Bailey napisaÅ?(a):
> Hi,
> I just want Ruby to enable me to send simple e-mails, sometimes with an
> attachment, if possible. Here's all I want:
>
> To: pbailey@bna.com (that's me, as a test)
> From: my account, whatever
> Message: Some test text. More test text. And so on.
> Attach: c:\simplestuff.pdf
>
> The e-mail server here at my company is Lotus Notes. I've used it for
> years as an SMTP server.
>
> Thanks,
> Peter

Install actionmailer gem and every task with email will be easy. :D
Examples are in web.

Regards
/Grzegorz