2012-06-11 48 views
1

我在通過Ruby代碼發送電子郵件時遇到問題。你可以看到我的full code on GitHub通過Heroku上的Ruby代碼發送電子郵件

UPDATE:下面的代碼進行了修訂,從@Gaurish

UPDATE2反映建議:看起來像gmail的拒絕登錄嘗試 - 我收到一封電子郵件,提醒我一些未知的應用程序試圖訪問我的帳戶,但他們關閉它

的具體類是在這裏:

require 'net/smtp' 

=begin 
    http://apidock.com/ruby/v1_9_3_125/Net/SMTP/start/class 
    https://devcenter.heroku.com/articles/config-vars 

    I added the following config vars to Heroku 
    heroku config:add GM_USR=xxxx 
    heroku config:add GM_PSW=xxxx 
=end 

class Email 
    def initialize (to, from, subject, body) 
      @to = to 
      @from = from 
      @subject = subject 
      @body = body 
      @message = <<MESSAGE_CONTENT 
       From: User <#{@from}> 
       To: Integralist <#{@to}> 
       MIME-Version: 1.0 
       Content-type: text/html 
       Subject: #{@subject} 
       #{@body} 
MESSAGE_CONTENT 

      @smtp = Net::SMTP.new('smtp.gmail.com', 587) 
     end 

     def send_email 
      @smtp.enable_starttls 
      @smtp.start('furious-wind-9309.herokuapp.com', ENV['GM_USR'], ENV['GM_PSW'], :login) do |smtp| 
      @smtp.send_message(@message, @from, @to) 
     end 
    end 
end 

我打電話它像這樣:

email = Email.new('[email protected]', params[:email], 'test subject', params[:message]); 
email.send_mail 

但是,當我執行的代碼,我得到屏幕上顯示的錯誤:535-5.7.1 Please log in with your web browser and then try again. Learn more at

我檢查了日誌,我得到...

2012-06-13T08:01:08+00:00 heroku[router]: POST furious-wind-9309.herokuapp.com/contact dyno=web.1 queue=0 wait=0ms service=628ms status=500 bytes=2060 
2012-06-13T08:01:08+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call' 
2012-06-13T08:01:08+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:200:in `call' 
2012-06-13T08:01:08+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:205:in `context' 
2012-06-13T08:01:08+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/logger.rb:15:in `call' 
2012-06-13T08:01:08+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/commonlogger.rb:20:in `call' 
2012-06-13T08:01:08+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/head.rb:9:in `call' 
2012-06-13T08:01:08+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call' 
2012-06-13T08:01:08+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1334:in `block in call' 
2012-06-13T08:01:08+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1416:in `synchronize' 
2012-06-13T08:01:08+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1334:in `call' 
2012-06-13T08:01:08+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/thin-1.3.1/lib/thin/connection.rb:80:in `block in pre_process' 
2012-06-13T08:01:08+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/thin-1.3.1/lib/thin/connection.rb:78:in `catch' 
2012-06-13T08:01:08+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/thin-1.3.1/lib/thin/connection.rb:78:in `pre_process' 
2012-06-13T08:01:08+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/eventmachine-0.12.10/lib/eventmachine.rb:1060:in `call' 
2012-06-13T08:01:08+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/eventmachine-0.12.10/lib/eventmachine.rb:1060:in `block in spawn_threadpool' 
2012-06-13T08:01:08+00:00 app[web.1]: 82.69.39.185 - - [13/Jun/2012 08:01:08] "POST /contact HTTP/1.1" 500 2060 0.6254 

我知道人們可能會提出的ActionMailer或小馬,但我寧願不使用那些或者請求那些建議給我的人。相反,我希望能夠幫助解決上述問題的解決方案。

回答

4

[更新1]

如果Gmail沒有爲你工作,你可以使用SendGrid Addon,讓你每天高達200emails不收取任何費用。

下面是關於如何使用他們的STMP API與使用Sendgrid mail gem

require 'mail' 
Mail.defaults do 
    delivery_method :smtp, { :address => "smtp.sendgrid.net", 
          :port  => 587, 
          :domain => "yourdomain.com", 
          :user_name => "[email protected]", 
          :password => "yourPassword", 
          :authentication => 'plain', 
          :enable_starttls_auto => true } 
end 

mail = Mail.deliver do 
    to '[email protected]' 
    from 'Your Name <[email protected]>' 
    subject 'This is the subject of your email' 
    text_part do 
    body 'Hello world in text' 
    end 
    html_part do 
    content_type 'text/html; charset=UTF-8' 
    body '<b>Hello world in HTML</b>' 
    end 
end 

樣品(來自docs拍攝)是更好的解決方案,因爲你還可以獲得高級報告& analyticss這是不可用的Gmail 。此外,sendgrid中的「from」地址沒有限制。


隨着Heroko,你不能直接從localhost發送電子郵件,因爲Heroku does not provide an outgoing mail service

所以你將不得不考慮一個外部 smtp服務器發送您的電子郵件。流行的是Gmail & Sendgrid

這只是使用像heroku這樣的雲計算平臺的一種權衡。

使用Gmail,嘗試做這樣的事情:

require 'net/smtp' 

    msg = "your message goes here" 
    smtp = Net::SMTP.new 'smtp.gmail.com', 587 
    smtp.enable_starttls 
    smtp.start(YourDomain, YourAccountName, YourPassword, :login) do 
     smtp.send_message(msg, FromAddress, ToAddress) 
    end 
+0

謝謝,明天我會試試。但是如果我還沒有一個設置,YourDomain參數會是什麼?例如,我通過Heroku提供的URL訪問網站(目前),所以我會詳細說明:http://appname.heroku.com? – Integralist

+0

有關更多信息,請參閱[API文檔](http://apidock.com/ruby/v1_9_3_125/Net/SMTP/start/class) – CuriousMind

+0

這不行,恐怕。我已更新我的問題以反映我正在使用的新代碼。也許你可以告訴我可能會出錯的地方。謝謝 – Integralist

0

如果你嘗試:

@email = Email.new('[email protected]', params[:email], 'test subject', params[:message]) 
@email.send_email 
0

只加載了SendGrid作爲一個附加的和它的作品開箱的。另外,您還可以獲得整套分析工具,用於故障排除反彈,交付等...