Topic: Sending E-mail Using Gmail SMTP
Hey guys, I did not write this tutorial, but I think it should be shared here.
I was having issues with ActionMailer and Gmail. I had my configurations looking about like this:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "oursite.com",
:authentication => :plain,
:user_name => "info@oursite.com",
:password => "*******"
}Obviously that didn't work right out of the box, though it would have been plain sexy if it did.
Now, the messages were getting processed gorgeously. I know this because they came up in my logs. I then did a little bit of googling and found this link:
http://godbit.com/forum/viewtopic.php?id=876
Which basically says " GMail supports only SSL SMTP mailing service, meaning if you cannot create a SSL connection to its SMTP server, you cannot send email through them. "
So, he tells us to roll our own itty bitty plugin. It was a process of creating two files and one folder.
Here is a zip of the files so you don't have to recreate them: http://lakedenman.com/files/action_mailer_tls.zip
Here is the source:
#vendor/plugins/action_mailer_tls/init.rb
require_dependency 'smtp_tls'
vendor/plugins/action_mailer_tls/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 secretsock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
@socket = Net::InternetMessageIO.new(sock)
@socket.read_timeout = 60 #@read_timeout
@socket.debug_output = STDERR #@debug_outputcheck_response(critical { recv_response() })
do_helo(helodomain)raise 'openssl library not installed' unless defined?(OpenSSL)
starttls
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)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
enddef 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
enddef starttls
getok('STARTTLS')
enddef quit
begin
getok('QUIT')
rescue EOFError
end
end
end
Then, your environment file should look like this:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.server_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "mycompany.com",
:authentication => :plain,
:user_name => "username",
:password => "password"
}
I really hope this helps some people. Thanks.
Lake