rubysinatrapony

sending file with pony by sinatra app - missing file


I want to send a email from my sinatra application.

Here is the code:

    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 is starting application with no errors, but When i request myapp.local/mail i've got an error:

    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'

File /var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/network/delivery_methods/smtp.rb exists.


Solution

  • I was getting this same error when I was using the inline configuration of the Mail gem:

    mail.delivery_method :sendmail
    mail.deliver!
    

    Removing that first line, and moving the configuration to immediately following the loading of the mail gem fixed it.

    Wherever in your app you require 'mail' just configure it immediately:

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

    Update: This worked for awhile... But then for some reason I began seeing this error: 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: The failures happen randomly it seems. Something about the way the autoload works in Ruby 1.8.7-p374 is causing it to not be able to find files that do in fact exist. Also, I am using slimgems not rubygems.

    These are the hacks I've had to implement so far to use Mail with multi-part email and sendmail delivery method:

    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