Action Mailer allows you to send email from your application using a mailer
model and views.
This tutorial is
about how to send an email using gmail free service in Ruby on Rails 3 and above
Creating Demo project
rails new mail_demo –d mysql
Creating a User model
rails g scaffold User name:string email:string content:string
Creating a Mailer model
rails g mailer user_mailer
Setting up the development.rb for configuring application settings to access mail smtp server
In development.rb
config.action_mailer.delivery_method = :smtp
# Defaults to:
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'your.domain.com',
:authentication => :login,
:content_type => "text/html",
:user_name => 'username',
:password => 'password'
}
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
Remember
these are the basic settings needed to send mails from gmail ,other server may
have different settings .
Creating the mailer
Now
we will write the method in user_mailer model to send the mail .Remember in the
mailer model
def send_email(user)recipients user.emailfrom "webmaster@example.com"subject "Thank you for Registering"part :content_type => "text/html",:body => render_message("registration_confirmation", :user => user)end
The
from field can be your own or your domains email address. Please check if the
from for field has invalid email the user will not be able to reply .
Creating Email Template
Now
we will create a view which would form a the email template .It is named as
same as the method in the model user_mailer ie. Send_email.html.erb
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css" media="screen">h3 { color: #f00; }ul { list-style: none; }</style></head><body><div><h2 id = "banner">Your account signup details are below</h2></div><ul><br /><br /><li><h2>Name: <b><%= @user.name %></b> </h2></li><li>Login: <%= @user.email %></li><br /><br /><li>Image: <%= image_tag 'rails.png' %></li><br /><br /><li>Content: <%= @user.content %></li></ul></body></html>
In
the end add a line to the create method of the Users_controller
UserMailer.send_email(@user).deliver
The
method is referred to as deliver_send_email because rails appends it to a
mailer method.
Now
run the server and try creating a new user . The email is sent to the address
the user enters for the new record.
is it necesary to get a domain for sending email in ruby ????
ReplyDeleteDomain name is needed incase the SENDTO email you are using belongs to your website.
ReplyDeleteEg. www.xyz.com and the email you are sending from this site are sent as 123@xyz.com
Then you should use a domain name.
Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.Ruby on Rails Online Training
ReplyDelete