ROR: Sending email

The Action Mailer component of Rails 
enables applications to send and receive e-mail. 

In this chapter, We will set the steps to send an 
email using Rails. 
 
1. Create emails project 
issue the following command-line:
C:\ruby\> rails emails
This command creates the related Rails framework, under the created director: C:\ruby\emails. 2. Configure Action Mailer: Open the file: C:\ruby\emails\config\environment.rb and set things as follow: fold & unfold
3. Create a mailer Generate: Issue the command line:
C:\ruby\> cd emails C:\ruby\emails> ruby script/generate mailer Emailer
This creates a file emailer.rb in the directory: C:\ruby\emails\app\models directory. Open this file and define a method (here contact-email).It should look like this:
class Emailer < ActionMailer::Base def contact_email(recipient, subject, message, sent_at = Time.now) @subject = subject @recipients = recipient @from = 'ajaja@sympatico.ca' @sent_on = sent_at @body["title"] = 'Welcome to the scientific sentence!' @body["email"] = 'ajaja@sympatico.ca' @body["message"] = message @headers = {} end end
4. Create the related contact_email.rhtml file: This file is placed in C:\ruby\emails\app\views\emailer\; It will look like this:
Hi! This is an email from <%= @email %> <%= @title %> <%= @message %> Best regards,
5. Create a controller for the application: Issue the command::
C:\ruby\emails> ruby script/generate controller Emailer
This creates the file: emailer_controller.rb in the directory: C:\ruby\emails\app\controllers. Open this file and define a Vontroller method (here: send_email). This method will call Model method to send the actual email. It sould look like this:
class EmailerController < ApplicationController def index render :file => 'app\views\emailer\index.rhtml' end def send_email email = params["email"] recipient = email["recipient"] subject = email["subject"] message = email["message"] Emailer.deliver_contact(recipient, subject, message) return if request.xhr? render :text => 'The message has been sent successfully' end end
6. Set the related file app\views\emailer\index.rhtml from where we will send email. It should look like this:
<h1>Send Email</h1> <%= form_tag :action => 'send_email' %> <p><label for="email_subject">Subject</label>: <%= text_field 'email', 'subject' %></p> <p><label for="email_recipient">Recipient</label>: <%= text_field 'email', 'recipient' %></p> <p><label for="email_message">Message</label><br/> <%= text_area 'email', 'message' %></p> <%= submit_tag "Send" %> <%= form_tag %>
7. Use the application: 1. First: run ruby C:\ruby\emails> ruby script/server We get on the prompt: => Booting WEBrick... => Rails application started on http://0.0.0.0:3000 => Ctrl-C to shutdown server; call with --help for options [2008-09-05 00:47:28] INFO WEBrick 1.3.1 [2008-09-05 00:47:28] INFO ruby 1.8.6 (2007-03-13) [i386-mswin32] [2008-09-05 00:47:28] INFO WEBrick::HTTPServer#start: pid=4504 port=3000 2. Then: Open the URL: http://localhost:3000/Emailer/index This displays the following screen. The application allows sending messages everywhere.

We get on the prompt: => Ctrl-C to shutdown server; call with --help for options [2008-09-05 00:58:02] INFO WEBrick 1.3.1 [2008-09-05 00:58:02] INFO ruby 1.8.6 (2007-03-13) [i386-mswin32] [2008-09-05 00:58:02] INFO WEBrick::HTTPServer#start: pid=4876 port=3000 127.0.0.1 - - [05/Sep/2008:00:58:08 Eastern Daylight Time] "GET /Emailer/index HTTP/1.1" 304 0 - -> /Emailer/index Once the button "Send" is cliked, the message is snet and we get on the prompt: 127.0.0.1 - - [05/Sep/2008:01:00:14 Eastern Daylight Time] "POST /emailer/send_email HTTP/1.1" 200 25 http://localhost:3000/Emailer/index -> /emailer/send_email