# Simple mail with Ruby

**URL:** <https://www.ruby-forum.com/t/simple-mail-with-ruby/126150>\
**Category:** Ruby\
**Created:** [January 15, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/simple-mail-with-ruby/126150 "2008-01-15T20:54:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 15, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/simple-mail-with-ruby/126150/1 "2008-01-15T20:54:04Z")

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

To: removed\_email\_address@domain.invalid (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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 15, 2008, 9:57pm UTC](https://www.ruby-forum.com/t/simple-mail-with-ruby/126150/2 "2008-01-15T21:57:34Z")

</div>

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

> 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/).

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” =\>  
“removed\_email\_address@domain.invalid”, “from” =\> “removed\_email\_address@domain.invalid”))  
p(EMail(“to” =\> “removed\_email\_address@domain.invalid”, “from” =\>  
“removed\_email\_address@domain.invalid”){ “test 2 @ #{ Time::now }” })  
p(EMail(“to” =\> “removed\_email\_address@domain.invalid”){ “test 3 @ #  
{ Time::now }” })  
end

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 16, 2008, 9:57pm UTC](https://www.ruby-forum.com/t/simple-mail-with-ruby/126150/3 "2008-01-16T21:57:20Z")

</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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 16, 2008, 10:04pm UTC](https://www.ruby-forum.com/t/simple-mail-with-ruby/126150/4 "2008-01-16T22:04:02Z")

</div>

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

> 

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 20, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/simple-mail-with-ruby/126150/5 "2008-01-20T16:10:29Z")

</div>

Peter B. napisaÅ‚(a):

> years as an SMTP server.
> 
> Thanks,  
> Peter

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

Regards  
/Grzegorz
