1

我想从我的 sinatra 应用程序中发送一封电子邮件。

这是代码:

    require 'pony'
    class Cms < Application

      get "/mail" do
        Pony.mail :to => 'to@gmail.com',
          :from => "from@gmail.com",
          :subject => "Thanks for signing my guestbook!",
          :via => :sendmail,
          :via_options => {
              :address     => 'smtp.gmail.com',
              :port     => '587',
              :user_name     => 'user@gmail.com',
              :pass     => 'pass',
              :enable_starttls_auto => false
          },
          :body => erb(:"cms/mail")
        redirect '/'
      end

    end`

Thin 启动应用程序时没有错误,但是当我请求 myapp.local/mail 时出现错误:

    LoadError - no such file to load -- mail/network/delivery_methods/smtp:
        /var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/configuration.rb:31:in lookup_delivery_method'
        /var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/configuration.rb:25:in delivery_method'
        /var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/mail.rb:111:in delivery_method'
        /var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/message.rb:116:in initialize'
        /var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/mail.rb:50:in new'
        /var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/mail.rb:50:in new'
        /var/lib/gems/1.8/gems/pony-1.4/lib/pony.rb:174:in build_mail'
        /var/lib/gems/1.8/gems/pony-1.4/lib/pony.rb:138:in mail'
        ./app/controllers/cms.rb:8:in GET /mail'

文件/var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/network/delivery_methods/smtp.rb存在。

4

1 回答 1

1

当我使用 Mail gem 的内联配置时,我遇到了同样的错误:

mail.delivery_method :sendmail
mail.deliver!

删除第一行,并将配置移动到加载邮件 gem 后立即修复它。

无论在您的应用程序中的哪个位置,您都require 'mail'可以立即对其进行配置:

require 'mail'
Mail.defaults do
  delivery_method :sendmail
end

更新:这工作了一段时间......但后来由于某种原因我开始看到这个错误: rbenv/versions/1.8.7-p374/lib/ruby/gems/1.8/gems/mail-2.5.4/lib/mail/fields/common/common_address.rb:9:in `parse': no such file to load -- mail/elements/address_list (LoadError)

Update2:故障似乎是随机发生的。Ruby 1.8.7-p374 中自动加载的工作方式导致它无法找到确实存在的文件。另外,我使用的是 slimgems 而不是 ruby​​gems。

到目前为止,这些是我必须实施的技巧,以便将 Mail 与多部分电子邮件和 sendmail 传递方法一起使用:

require 'mail'
require 'mail/network/delivery_methods/sendmail'
require 'mail/elements/address_list'
require 'mail/fields/common/common_address'
require 'mail/elements/content_type_element'
require 'mail/elements/address'
require 'mail/elements/content_transfer_encoding_element'

Mail.defaults do
  delivery_method :sendmail
end
于 2014-01-03T18:27:28.177 回答