Friday, November 16, 2007

How to use GMail's SMTP server with Rails

How to use GMail's SMTP server with Rails

1. Create a file 'smtp_tls.rb' and save in lib/smtp_tls.rb

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
#@socket.debug_output = STDERR #@debug_output

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
#@socket.debug_output = STDERR #@debug_output
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, OpenSSL::SSL::SSLError
end
end
end

2. Run the command
ruby script/generate mailer Notifier

3. Add the following method

def signup_thanks( user )
# Email header info MUST be added here
recipients user.email
from "accounts@mywebsite.com"
subject “Thank you for registering with our website”

# Email body substitutions go here
body :first_name => user.first_name, :last_name => user.last_name
end

4. Add the following line in a controller

Notifier.deliver_signup_thanks(user)

5. Create a file named 'signup_thanks.rhtml' and save in the directory app/models/notifier

Dear <%= @first_name %> <%= @last_name %>,
Thanks for signing up with My Website!
Your account will allow you to do blah, blah, blah.

6. Add the following lines in the configuration file 'config/environment.rb'

require 'smtp_tls'
ActionMailer::Base.server_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "localhost.localdomain",
:authentication => :plain,
:user_name => "someusername",
:password => "somepassword"
}

Sources
http://www.rubyinside.com/how-to-use-gmails-smtp-server-with-rails-394.html
http://wiki.rubyonrails.org/rails/pages/HowToSendEmailsWithActionMailer
http://21croissants.blogspot.com/2007/08/configuring-rails-to-use-gmails-smtp.html
http://api.rubyonrails.org/classes/ActionMailer/Base.html

Labels: ,

Wednesday, November 14, 2007

manpages-dev package

Today I ran the following command to get the usage of the function 'strlen' and found that there is no programmer manual on my computer.

$ man strlen
No manual entry for strlen

After googling, I got a solution. The cause is that package 'manpages-dev' is not installed by default.
Similarly, no development tools like gcc or g++ are installed by default.