Depois de algumas horas quebrando a cabeça, consegui fazer funcionar o envio de e-mails usando o Google Apps.
Vou explicar como configurar. ;-)
Vamos lá!
Configure o config/environment.rb
Configure o seu environment.rb com a sua conta de e-mail do Google Apps, ou conta do Gmail, os e-mails serão enviados via smtp.
1 2 3 4 5 6 7 8 9 10 11 12 | ... Rails::Initializer.run do |config| ... config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { :address => "smtp.gmail.com", :port => 587, :authentication => :plain, :user_name => "seu-usuario@seudominio.com", :password => "sua senha" } end |
Mas só fazendo isso não vai funcionar, o Google Apps utiliza TLS (Transport Layer Security), ou seja, Protocolo de Camada de Sockets Segura.
Para funcionar é necessário adicionar a lib smtp_tls.rb responsável pela camada TLS, você encontra ela no seguinte endereço: http://github.com/patrickespake/SMTP-TLS/tree/master.
smtp_tls.rb
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 | 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 check_auth_args user, 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 |
Copie o arquivo smtp_tls.rb para o diretório lib do seu projeto Ruby on Rails.
Carregando a lib smtp_tls.rb no environment.rb
Volte para o environment.rb e adicione para carregar a lib smtp_tls.rb:
1 2 3 4 5 | ... require 'smtp_tls' Rails::Initializer.run do |config| ... |
Pronto! Fazendo isso você irá conseguir enviar e-mails usando o Google Apps ou Gmail.
Existe um detalhe a ser considerado na lib smtp_tls.rb, se você olhar o código vai encontrar as linhas abaixo:
7 8 9 10 | ...
#check_auth_args user, secret, authtype if user or secret
check_auth_args user, secret
... |
O código por padrão tinha a linha check_auth_args user, secret, authtype if user or secret, mas quando essa linha está habilitado os e-mails não são enviados, você pode entender melhor o por que lendo aqui: http://blog.inspired.no/smtp-error-while-using-gmail-in-rails-271. Desta forma eu deixei apenas check_auth_args user, secret.
Se você precisa entender o básico de como criar um e-mail e enviar leia aqui: http://guides.rubyonrails.org/action_mailer_basics.html.
Se você gostou desse texto e acha que ajudou você, me recomende:
.
