sending mails using ruby

There is a very common requirement especially in web services, that of sending notification email for some action say, addition/deletion/updation of database in user signup or product selection in a shopping cart. Ruby handles it beautifully. This is obvious. What is not obvious is that it actually gives user different ways of implementing it.

  • RubyMail
  • TMail
  • ActionMailer (uses TMail)

I used ActionMailer. I had a database driven website. I wanted to notify administrator for any updation in the database (addition/edition of records). So this is how I did it :-

ruby script/generate mailer Notifier

This creates a file in several files :-


test/unit/notifier_test.rb
test/fixtures/notifier
app/models/notifier.rb
app/views/notifier

Of Particular interest is file :- app/models/notifier.rb. Add following method in this file in the class Notifier,


def notifyMyAction( argument )
  # Email header info MUST be added here
  @recipients = "xyz@abc.com, mno@def.com"
  @from = "intelligent@ruby.com"
  @subject = “Action happened” 

  # Email body substitutions go here
  @body[“argument”] = argument
end

And following code in the controller’s method where you want to notify.


def update_db_and_notify
...
  Notifier::deliver_notifyMyAction((arg)
...
end

NOTE: ‘deliver_’ before the function name !!

Now put the mail body as you would like to put, in the view/notifier/notifyMyAction.rhtml


Dear  <%= @argument >, 

Your database just got updated !! 

Thanks
-Notifier

We are just done. Finally, append following text to the config/environment.rb file (if not already there)
and change address to _your_ mail server address. domain is optional but should make senses since some spam filters filter out the mails on the basis of this. username and password are also optional as is the last one which i commented in my case 🙂


ActionMailer::Base.server_settings = {
  :address  => "mail.server.com",
  :port  => 25,
  :domain  => 'www.abc.com',
  :user_name  => "me@abc.com",
  :password  => ‘mypass’,
#  :authentication  => :login
    }

There is more to learn like attachments, HTML messages, and many other tweaks. You can get it here.

Hail ruby !

long time no see :)

Yes, long time. But I have excuses with me. I was ill and after the recovery, had loads of work. really. Now when I am eased from my work, I can blog !!

Just to tell you something about what all happened in this *long* time :-

  • I suffered from minor ‘dengue’. Dengue is a viral fever which decreases the plateletes count drastically. Symptoms are high grade fever (which will last for small durations), weakness in body, pain in body and head, sometimes red rashes on the body.

    Its dangerous sometimes. but good news is that (i hope) by taking utmost care you can avoid any serious problems. patient should :- consult a doctor, have complete bed rest,  a lot of fluid that includes water, juice, cold drinks, milk, ORS and just anything. and you’ll be well.

  • I joined office and had loads of work. got panicked for a while. then started doing things one by one and today I am feeling relaxed with few things in hand. (although i missed deadlines)
  • Came closer to Ruby development. learned more about it. I think this is one language I would like to use for my personal development work, if possible. fast, neat and efficient. I did some ruby on rails stuff this time for the development of a web tool.
  • Read Train to Pakistan by Khuswant Singh. You can read about what I felt here.
  • Started with a long pending book The Mythical Man-Month by Fredrick Brooks.

Rest, I will keep updating. !