firstruby

Master ruby with us

How to Send email through Ruby On Rails application

Posted by firstruby on February 28, 2007

Configuration to send email through our Ruby On Rails application

1) My UserNotify.rb model with signup method as follows,

class UserNotify < ActionMailer::Base
  def signup(user, password, url=nil)
  setup_email(user)# Email header info
  @subject += “Welcome to #{UserSystem::CONFIG[:app_name]}!”# Email body

substitutions
  @body[“name”] = “#{user.firstname} #{user.lastname}”
  @body[“login”] = user.login
  @body[“password”] = password
  @body[“url”] = url || UserSystem::CONFIG[:app_url].to_s
  @body[“app_name”] = UserSystem::CONFIG[:app_name].to_s
  end

2) Call to signup method of model from controller UserController.rb is,

UserNotify.deliver_signup(@user, @params[‘user’][‘password’], url)
3) Create template to send with email as /user_notify/welcome.rhtml

 Welcome to <%= @app_name%>, <%= @name%>.

Your login credentials are:

login: <%= @login %>password: <%= @password %>

Please click on the following link to confirm your registration:

<a href=”<%= @url%>”>Click me!</a>

<%= @url %>

4) You also need to add the following at the end of your config/environment.rb file,

require ‘environments/user_environment’

5) Also, you must properly configure ActionMailer for your mail settings. For example, I have the following in config/environment.rb

ActionMailer::Base.server_settings = {

:address => “IP address”,        # Enter your public IP address here

:port => 25                                   # Specify your SMTP port

}

6) Create  ‘environments/user_environment.rb’ file as,

module UserSystem
  CONFIG = {
    # Source address for user emails
    :email_from => ‘craig@gmail.com’,

    # Destination email for system errors
    :admin_email => ‘craig@gmail.com’
}
end

 —————————————————————————–

 This is all to send email through Ruby On Rails application. Check the email you have entered at the time of signup.

Fortunately, you won’t experience issues. I may forget some parts or putting some steps that are not exactly in accordance with your system configuration. Please forgive me if I made some mistakes, I’m no “RubyOnRails guru”
I’m ready to hear comments, suggestions or troubles !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: