This is updated from my old post on the subject. It’s a process cheat sheet for me.
I’m not gonna explain it too much. Here’s the code.
script/plugin install git://github.com/technoweenie/restful-authentication.git
script/generate authenticated user sessions --include-activation
1
2
3
| #Add an observer to config/environment.rb
config.active_record.observers = :user_observer
#put it just before the very last 'end' on the page |
1
2
3
4
5
| #Add routes to these resources. In config/routes.rb, insert routes like:
map.signup '/signup', :controller => 'users', :action => 'new'
map.login '/login', :controller => 'sessions', :action => 'new'
map.logout '/logout', :controller => 'sessions', :action => 'destroy'
map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate', :activation_code => nil |
1
2
3
4
5
6
7
8
9
10
11
| # create new file config/initializers/mail.rb
require "smtp_tls"
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:authentication => :plain,
:user_name => "you@gmail.com",
:password => 'yourpassword'} |
1
2
3
4
| # edit config/environments/production.rb & put these lines at the top
SITE_NAME = "Your Site"
SITE_URL = "www.yourdomain.com"
SITE_EMAIL = "admin@yourdomain.com" |
1
2
3
4
| # edit config/environments/development.rb & put these lines at the top
SITE_NAME = "ToolTime"
SITE_URL = "localhost:3000"
SITE_EMAIL = "scott@scottmotte.com" |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| # Make app/models/user_mailer.rb look like the following:
class UserMailer < ActionMailer::Base
def signup_notification(user)
setup_email(user)
@subject += 'Please activate your new account'
@body[:url] = "http://#{SITE_URL}/activate/#{user.activation_code}"
end
def activation(user)
setup_email(user)
@subject += 'Your account has been activated!'
@body[:url] = "http://#{SITE_URL}/"
end
protected
def setup_email(user)
@recipients = "#{user.email}"
@from = "#{SITE_EMAIL}"
@subject = "#{SITE_NAME}"
@sent_on = Time.now
@body[:user] = user
end
end |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
| # create file lib/smtp_tls.rb with following code in it
require "openssl"
require "net/smtp"
Net::SMTP.class_eval do
private
def do_start(helodomain, user, secret, authtype)
raise IOError, 'SMTP session already started' if @started
check_auth_args user, secret, authtype if user or secret
sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
@socket = Net::InternetMessageIO.new(sock)
@socket.read_timeout = 60 #@read_timeout
check_response(critical { recv_response() })
do_helo(helodomain)
if starttls
raise 'openssl library not installed' unless defined?(OpenSSL)
ssl = OpenSSL::SSL::SSLSocket.new(sock)
ssl.sync_close = true
ssl.connect
@socket = Net::InternetMessageIO.new(ssl)
@socket.read_timeout = 60 #@read_timeout
do_helo(helodomain)
end
authenticate user, secret, authtype if user
@started = true
ensure
unless @started
# authentication failed, cancel connection.
@socket.close if not @started and @socket and not @socket.closed?
@socket = nil
end
end
def do_helo(helodomain)
begin
if @esmtp
ehlo helodomain
else
helo helodomain
end
rescue Net::ProtocolError
if @esmtp
@esmtp = false
@error_occured = false
retry
end
raise
end
end
def starttls
getok('STARTTLS') rescue return false
return true
end
def quit
begin
getok('QUIT')
rescue EOFError
end
end
end |
Restart your server, visit http://yourapp.com/signup and give it a shot.
Once that works. Then put before_filter :login_required in your controllers where needed.
1 Comment | August 18, 2008
Scott Motte » Blog Archive » Using Gmail with IMAP to Receive Email in Rails (cron job version) | November 19, 2008
[...] Your rails app should already have the ability to send out emails 2. Your Rails app must already be capified via Capistrano or similar with some type of deployment [...]