# Simple mail with Ruby

**URL:** <https://rubytalk.org/t/simple-mail-with-ruby/43792>\
**Category:** ruby-talk\
**Created:** [15 January 2008 19:54 UTC](https://rubytalk.org/t/simple-mail-with-ruby/43792 "2008-01-15T19:54:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guest16](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guest16](https://rubytalk.org/u/Guest16)\
**Post date:** [15 January 2008 19:54 UTC](https://rubytalk.org/t/simple-mail-with-ruby/43792/1 "2008-01-15T19:54:00Z")

</div>

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

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

> **···**
>
> To: [pbailey@bna.com](mailto:pbailey@bna.com) (that's me, as a test)  
> From: my account, whatever  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [15 January 2008 20:56 UTC](https://rubytalk.org/t/simple-mail-with-ruby/43792/2 "2008-01-15T20:56:41Z")

</div>

require "net/smtp"

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

&nbsp;&nbsp;&nbsp;attr\_accessor "user"  
&nbsp;&nbsp;&nbsp;attr\_accessor "from"  
&nbsp;&nbsp;&nbsp;attr\_accessor "to"  
&nbsp;&nbsp;&nbsp;attr\_accessor "subject"  
&nbsp;&nbsp;&nbsp;attr\_accessor "message"  
&nbsp;&nbsp;&nbsp;attr\_accessor "sent"  
&nbsp;&nbsp;&nbsp;alias\_method "sent?", "sent"

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

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

EMail = Email

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

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

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> 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-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> it is not enough to be compassionate. you must act.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![ggolebio](https://avatars.discourse-cdn.com/v4/letter/g/97f17d/32.png) [@ggolebio](https://rubytalk.org/u/ggolebio)\
**Post date:** [20 January 2008 15:10 UTC](https://rubytalk.org/t/simple-mail-with-ruby/43792/3 "2008-01-20T15:10:00Z")

</div>

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. 😃  
Examples are in web.

Regards  
/Grzegorz

---

<div class="post-metadata">

**Author:** ![Guest16](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guest16](https://rubytalk.org/u/Guest16)\
**Post date:** [16 January 2008 20:57 UTC](https://rubytalk.org/t/simple-mail-with-ruby/43792/4 "2008-01-16T20:57:27Z")

</div>

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-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [16 January 2008 21:02 UTC](https://rubytalk.org/t/simple-mail-with-ruby/43792/5 "2008-01-16T21:02:46Z")

</div>

sure.

&nbsp;&nbsp;&nbsp;msg = "To: %s\nFrom: %s\nSubject: %s\n\n%s" % [@to, @from, @subject, @message]  
&nbsp;&nbsp;&nbsp;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://drawohara.com/](http://drawohara.com/)

> **···**
>
> 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-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> sleep is the best meditation.  
> h.h. the 14th dalai lama
