使用Ruby通过Pony Mail发送时的Net :: SMTPAuthenticationError

时间:2016-06-30 05:08:34

标签: ruby email authentication sinatra pony

我正在尝试为客户端配置联系表单。他从他的电子邮件帐户中提供了正确的密码和用户名。出于某种原因,我总是得到身份验证错误。

以下是代码:

post '/contact-us' do
    require 'pony'
    first_name  = params[:first_name]
    last_name = params[:last_name]
    mail = params[:mail]
    subject = params[:subject]
    body  = params[:body]

Pony.options = {
  :via => :smtp,
  :via_options => {
    :address              => 'smtp.zoho.com',
    :location             => '/c/sendmail/./sendmail',
    :ssl                  => true,
    :arguments            => '-t',
    :port                 => '465',
    # :enable_starttls_auto => true,
    :user_name            => 'username',
    :password             => "password",
    :authentication       => :login, # :plain, :login, :cram_md5, no auth by default
    :domain               => "localhost",
    # :tls                => true
  }
}


    Pony.mail(
        :to => 'sales@nlm-marketing.com', 
        :from => first_name + '<' + mail + '>',
        :reply_to => mail,
        :subject => subject,
        :body => body)



    redirect '/success'
end

这是回溯:

c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/net/smtp.rb in check_auth_response
        raise SMTPAuthenticationError, res.message
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/net/smtp.rb in auth_plain
      check_auth_response res
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/net/smtp.rb in authenticate
      send auth_method(authtype), user, secret
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/net/smtp.rb in do_start
      authenticate user, secret, (authtype || DEFAULT_AUTH_TYPE) if user
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/net/smtp.rb in start
          do_start helo, user, secret, authtype
main.rb in block in <main>
    Pony.mail(
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb in service
      si.service(req, res)
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb in run
          server.service(req, res)
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/server.rb in block in start_thread
          block ? block.call(sock) : run(sock)
GET
No GET data.
POST
Variable    Value
body    
"Testing"
first_name  
"Michael"
last_name   
"Stokes"
mail    
"mjstokes1986@att.net"
subject 
"Test"
COOKIES
No cookie data.
Rack ENV
Variable    Value
CONTENT_LENGTH  
89
CONTENT_TYPE    
application/x-www-form-urlencoded
GATEWAY_INTERFACE   
CGI/1.1
HTTP_ACCEPT 
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
HTTP_ACCEPT_ENCODING    
gzip, deflate
HTTP_ACCEPT_LANGUAGE    
en-US,en;q=0.8
HTTP_CACHE_CONTROL  
max-age=0
HTTP_CONNECTION 
keep-alive
HTTP_HOST   
localhost:4567
HTTP_ORIGIN 
http://localhost:4567
HTTP_REFERER    
http://localhost:4567/contact-us
HTTP_UPGRADE_INSECURE_REQUESTS  
1
HTTP_USER_AGENT 
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
HTTP_VERSION    
HTTP/1.1
PATH_INFO   
/contact-us
QUERY_STRING    
REMOTE_ADDR 
::1
REMOTE_HOST 
MSTOKES
REQUEST_METHOD  
POST
REQUEST_PATH    
/contact-us
REQUEST_URI 
http://localhost:4567/contact-us
SCRIPT_NAME 
SERVER_NAME 
localhost
SERVER_PORT 
4567
SERVER_PROTOCOL 
HTTP/1.1
SERVER_SOFTWARE 
WEBrick/1.3.1 (Ruby/2.1.5/2014-11-13)
rack.errors 
#<Object:0x3471090>
rack.hijack 
#<Proc:0x5dab6a8@c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/handler/webrick.rb:76 (lambda)>
rack.hijack?    
true
rack.hijack_io  
nil
rack.input  
#<StringIO:0x5dab768>
rack.logger 
#<Logger:0x5dd4e08 @progname=nil, @level=1, @default_formatter=#<Logger::Formatter:0x5dd4df0 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x5dd4dc0 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDERR>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x5dd4da8 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x5dd4d78>>>>
rack.multiprocess   
false
rack.multithread    
true
rack.request.cookie_hash    
{}
rack.request.form_hash  
{"first_name"=>"Michael", "last_name"=>"Stokes", "mail"=>"mjstokes1986@att.net", "subject"=>"Test", "body"=>"Testing"}
rack.request.form_input 
#<StringIO:0x5dab768>
rack.request.form_vars  
first_name=Michael&last_name=Stokes&mail=mjstokes1986%40att.net&subject=Test&body=Testing
rack.request.query_hash 
{}
rack.request.query_string   
rack.run_once   
false
rack.url_scheme 
http
rack.version    
[1, 3]
sinatra.accept  
[#<Sinatra::Request::AcceptEntry:0x63e7618 @entry="text/html", @type="text/html", @params={}, @q=1.0>, #<Sinatra::Request::AcceptEntry:0x63e74f8 @entry="application/xhtml+xml", @type="application/xhtml+xml", @params={}, @q=1.0>, #<Sinatra::Request::AcceptEntry:0x63e71f8 @entry="image/webp", @type="image/webp", @params={}, @q=1.0>, #<Sinatra::Request::AcceptEntry:0x63e7408 @entry="application/xml;q=0.9", @type="application/xml", @params={}, @q=0.9>, #<Sinatra::Request::AcceptEntry:0x63e7108 @entry="*/*;q=0.8", @type="*/*", @params={}, @q=0.8>]
sinatra.commonlogger    
true
sinatra.error   
#<Net::SMTPAuthenticationError: 535 Authentication Failed >
sinatra.route   
POST /contact-us

我该怎么做才能解决它?

1 个答案:

答案 0 :(得分:0)

尝试以下代码,这个常规设置适合我:

Pony.mail({
  :to => 'you@example.com',
  :from => first_name + '<' + mail + '>',
  :reply_to => mail,
  :subject => subject,
  :body => body,
  :via => :smtp,
  :via_options => {
    :address              => 'smtp.zoho.com', 
    :port                 => 465,                 
    :user_name            => 'user@email.com',
    :password             => 'a password',         
    :authentication       => :login,
    :ssl                  => true,
    :tls                  => true,
    :enable_starttls_auto => true   
  }
})