One of the most important aspects of a web based application is its use of all the channels of communication. And with the internet, the most important channel of communication is email. So how easy is generating and sending an email in rails? Quite simple, allow me to cover the few basic steps necessary:
The first thing you will need to generate and send email in rails is a model that extends the ActionMailer::Base class. You can either create this by hand or by typing this at your console:
ruby script/generate mailer telegram
In either case your model should look just like this (assuming you named your model "telegram" like I did):
class Telegram < ActionMailer::Base
end
If you used the script/generate method to create your model, you may have noticed that it also created a folder in your views directory. This folder will hold all of your email templates so you can easily manage your email as if they were just regular views. However, the one difference with the Rails email template and a normal view is:
An ActionMailer template does not require a layout!
So, lets create a very basic template to use for our emails... but wait, what do we name the new rhtml file? Well, I am a bit ahead of myself, so let me explain:
The rhtml file will correspond to some method in your ActionMailer Model (in my case my Telegram Model).
So, although we dont have the method in there yet, lets just assume I am going to name it new_comment. So the view in the telegram directory will be named new_comment.rhtml.
And the email that rails will generate will be completely pulled from this template. So here is my basic new_comment.rhtml email template:
Hello Admin a new comment was posted: <%= @comment %>
Very simple, right? Well, now how do we get that message to our favorite email account? With rails it is actually quite simple, all we need to do is add a method in our Model (Telegram) that matches the template we just created. In this method we will add all the necessary to and from stuff before we send the message. So here is what the method in the Telegram Model looks like:
def new_comment(comment)
recipients "charles@nospam.com"
from "charles@nospam.com"
subject "New Comment on ForTheCode.com"
body "comment" => comment
end
This is pretty straight forward, you simply set the recipients, from, subject and body values with strings or hashes. So in the above example you pass in the comment and it is available as a variable in your template (in this case named comment).
But how do you call this method? Well, with ActionMailer classes you dont really instantiate any objects, instead you just access the class methods with a small built in twist. To let rails know you want to send an email you simply add "deliver_" in front of the method you want to call to create and send the email. So, in my controller I added this line of code to create and send an email using the new_comment method and template:
Telegram.deliver_new_comment(myvar)
Where myvar is a variable containing the comment I want to add into my email. Rails automagically maps deliver_new_comment to your new_comment method and then sends the generated email as you would expect. However, I did stumble across one annoyance (that may be a bug):
Using rails version 1.1.6 - I am have problems calling this code in my controller.
Once I take the time to test what is going on I will post my findings. But strangely enough - if I place the exact same code in one of my views - the email generates just fine. But in the controller, rails seems to forget about sending my email. Strange indeed, but in any case - it just gives me more of a reason to learn more about handling email in rails. =)
