2

我是 Ruby 新手,如果这个问题很简单,我深表歉意。非常感谢您的帮助。

我们有一个已部署到 Heroku 上的 Sinatra 应用程序。我们正在尝试添加一个发送简单电子邮件的页面。我已将 SendGrid 插件添加到 Heroku 应用程序中。现在,我只是尝试添加使用 SendGrid SMTP 服务器信息创建和发送消息的 Ruby 代码。

我遇到的问题是,即使我在 Heroku 应用程序上安装了 Mail gem(通过添加到 Gemfile 中),当 Heroku 应用程序启动时出现错误,抱怨未安装“treetop/runtime”:

←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:75:in `require': no such file to load -- treetop/runtime (LoadError)
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m  from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:75:in `rescue in block in <module:Mail>'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m  from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:69:in `block in <module:Mail>'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m  from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:68:in `each'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m  from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:68:in `<module:Mail>'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m  from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:2:in `<top (required)>'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m  from /usr/ruby1.9.2/lib/ruby/gems/1.9.1/gems/bundler-1.0.7/lib/bundler/runtime.rb:64:in `require'

同样,当我尝试安装 Pony gem 时,它抱怨它无法加载 Mail。奇怪的是,这一切都适用于我的本地系统,所以我认为这是 Heroku 的问题。我只是无法让 Heroku 从 Pony 或 Mail 中完全加载成功启动所需的一切。(注意:我什至无法启动 Heroku,所以我什至无法测试电子邮件代码的实际发送。)

任何具体的帮助/见解将不胜感激。有人用 Heroku 遇到过这种情况吗?是否有其他宝石可以用于这个简单的目的?

谢谢!

PS 捆绑包成功安装了 mail (2.4.4) 和 pony (1.4)。

4

2 回答 2

8

It would be helpful to see code snippets for your primary routing ruby script. But for my sinatra app, I configured Mail configs as.

Mail.defaults do
  delivery_method :smtp, {
    :address => 'smtp.sendgrid.net',
    :port => 587,
    :domain => 'heroku.com',
    :user_name => ENV['SENDGRID_USERNAME'],
    :password => ENV['SENDGRID_PASSWORD'],
    :authentication => 'plain',
    :enable_starttls_auto => true
  }
end

And in my sending the contact email

mail = Mail.deliver do
    to <youremail>
    from <someone>
    subject 'Feedback for my Sintra app'
    text_part do
        body <your feedback>
    end
end

Hope this helps.

于 2012-06-07T13:17:21.420 回答
1

如果您想要 Pony 的一些设置而不是 Mail(它们非常相似):

configure :production do # I have a different hash for development settings.
  email_options, {      
    :from => "noreply@midas.heroku.com",
    :via => :smtp,
    :via_options => {
      :address => 'smtp.sendgrid.net',
      :port => '587',
      :domain => 'heroku.com',
      :user_name => ENV['SENDGRID_USERNAME'],
      :password => ENV['SENDGRID_PASSWORD'],
      :authentication => :plain,
      :enable_starttls_auto => true
    },
  }
end

Pony.options = settings.email_options

确保您已通过配置设置设置 sendgrid 用户名和密码,例如

heroku config:add SENDGRID_USERNAME=GIVEN_USERNAME SENDGRID_PASSWORD=GIVEN_PASSWORD

发送非常简单,只需调用Pony.mail并传递一个带有 'to' 和 'body' 等的哈希值。(我相信你知道这一点,但我只是想彻底!)

我也使用 v1.4,但没有遇到同样的问题,但如果它说当您切换到 Pony 时它无法加载 Mail,我会检查流氓require声明。

于 2012-07-10T01:19:08.827 回答