# Rails 2.0 Email (ActionMailer) Example

**URL:** <https://www.ruby-forum.com/t/rails-2-0-email-actionmailer-example/149851>\
**Category:** Rails\
**Created:** [September 30, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/rails-2-0-email-actionmailer-example/149851 "2008-09-30T15:42:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlskii](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@carlskii](https://www.ruby-forum.com/u/carlskii)\
**Post date:** [September 30, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/rails-2-0-email-actionmailer-example/149851/1 "2008-09-30T15:42:56Z")

</div>

Hi,

Does anybody have a simple Rails 2.1 working example of creating an  
email from a simple Web form and sending it via ActionMailer? Most of  
the examples I’ve found so far all seem to be Rails 1.x based and  
don’t work with Rails 2.x.

Best Regards,

Carl

---

<div class="post-metadata">

**Author:** ![carlskii](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@carlskii](https://www.ruby-forum.com/u/carlskii)\
**Post date:** [September 30, 2008, 6:19pm UTC](https://www.ruby-forum.com/t/rails-2-0-email-actionmailer-example/149851/2 "2008-09-30T18:19:29Z")

</div>

ruby script/generate mailer customermailer

models/customer\_mail.rb:

class CustomerMailer \< ActionMailer::Base

def welcome\_message(sent\_at = Time.now)  
@subject = ‘CustomerMailer#welcome\_message’  
@body = {}  
@recipients = ‘’  
@from = ‘’  
@sent\_on = sent\_at  
@headers = {}  
@body[“email”] = ‘’  
end  
end

views/customer\_mailer/welcome\_message.html.erb

This will be your email message

and now send it with this:

customer\_mail = CustomerMailer.create\_welcome\_message  
CustomerMailer.deliver(customer\_mail)

---

<div class="post-metadata">

**Author:** ![carlskii](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@carlskii](https://www.ruby-forum.com/u/carlskii)\
**Post date:** [September 30, 2008, 6:38pm UTC](https://www.ruby-forum.com/t/rails-2-0-email-actionmailer-example/149851/3 "2008-09-30T18:38:53Z")

</div>

Thanks for the reply, I assume I need to define some configuration  
within the environment.rb for this to work?

Regrds,

Carl

---

<div class="post-metadata">

**Author:** ![carlskii](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@carlskii](https://www.ruby-forum.com/u/carlskii)\
**Post date:** [October 1, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/rails-2-0-email-actionmailer-example/149851/4 "2008-10-01T16:45:16Z")

</div>

Hiemdull,  
I find this thread wonderfully useful and interesting…is there a  
chance you could show an example of the views/customer\_mailer/  
welcome\_message.html.erb ?  
Is customer\_mailer a controller also?  
I am grateful for a comprehensive example.  
Thank you,  
Kathleen

---

<div class="post-metadata">

**Author:** ![carlskii](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@carlskii](https://www.ruby-forum.com/u/carlskii)\
**Post date:** [October 1, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/rails-2-0-email-actionmailer-example/149851/5 "2008-10-01T17:57:35Z")

</div>

Put this in the environment.rb file(I use the google smtp server so I  
had to install the action\_mailer\_tls plugin):

config.action\_mailer.raise\_delivery\_errors = false  
config.action\_mailer.delivery\_method = :smtp  
config.action\_mailer.default\_content\_type = “text/html”  
config.action\_mailer.default\_url\_options = { :host =\>  
“[domainname.com](http://domainname.com)” }  
config.action\_mailer.smtp\_settings = {  
:tls =\> true,  
:address =\> “[smtp.gmail.com](http://smtp.gmail.com)”,  
:port =\> 587,  
:domain =\> “[domainname.com](http://domainname.com)”,  
:authentication =\> :plain,  
:user\_name =\> “username”,  
:password =\> “password”  
}

Here is a snip from new\_question\_message.html.erb I have:

Question from…: \<%= @question.name %\>  
  
Email…: \<%= @question.email %\>  
  
\<% if @question.phonenumber -%\>  
Phonenumber…: \<%= @question.phonenumber %\>  
  
\<% end -%\>  
Subject…: \<%= @question.subject %\>  
  
=========================================  
  
\<%= @question.description %\>

Then when I send this I have to pass the object:

mail = CustomerMailer.create\_new\_question\_message(@question)  
CustomerMailer.deliver(mail)

customer\_mailer is not a controller its a actionmailer and it lives in  
the /app/models directory

When you use the ruby script/generate mailer customermailer command it  
creates that for you.

So the steps would be:

- Create the mailer with generate mailer
- Add a mailer action like welcome\_message
- Setup your environment.rb with the correct smtp setup
- add the deliver action to your action in your controller (Imprtant  
is to use CustomerMailer.create\_ ← )

That should be all you need.

If you don’t want to wait for the email to be sent (Sometimes the app  
waits a second or 3 to send an email which does not make for a snappy  
interface) Watch this

> **[\#128 Starling and Workling - RailsCasts](http://railscasts.com/episodes/128-starling-and-workling)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Starling and Workling works great to offload the email delivery.
