我想从我的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
存在。
答案 0 :(得分: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而不是rubygems。
这些是到目前为止我必须实施的黑客攻击,使用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