Pavel Larionov
Pavel Larionov

Reputation: 33

Error when send mail with gem pony on Ruby

On initialization .rb i take this error

/home/fucc/.rvm/gems/ruby-2.5.1/gems/pony-1.11/lib/pony.rb:250:in `block in build_mail': undefined method `body' for Pony:Module (NoMethodError)

Cod when i send mail

def place
# Actually order
Pony.mail(:to => StoreApplication::Admin.email, 
            :from => "My store <[email protected]>",
            :via => :smtp,
            :via_options => {
            adress:             'smtp.gmail.com',
            port:               '587',
            user_name:          '[email protected]',
            password:           '###########',
            authentication:     :plain,
            domain:             "mail.google.com" },
            subject: "New order", body: "Check your admin page")
end

Ruby 2.5.1, Pony 1.12. I do: - update all gem's; - install again gem Pony; - down and up pony version

Upvotes: 3

Views: 579

Answers (1)

anothermh
anothermh

Reputation: 10526

The answer is in the error message that you posted:

/home/fucc/.rvm/gems/ruby-2.5.1/gems/pony-1.11/lib/pony.rb:250:in `block in build_mail': undefined method `body' for Pony:Module (NoMethodError)

That means that in the gem's source code, in the file lib/pony.rb on line 250 there is an attempt to call the method Pony.body, but the module Pony does not have a method body so an exception is being raised.

In cases like this, it helps to look at the source in the git repository for the gem. The first thing to notice is that the latest version of that gem is 1.12 and you are using 1.11. So what changed between 1.11 and 1.12, and could it fix this issue?

Looking at the history for lib/pony.rb I see there was an commit in November 2017 named 'Fix NoMethodError when pony is used with mail 2.7.0'. This change modifies lib/pony.rb line 250 and changes the method call for body. This is the exact line of code and method call that raised the error you described.

The solution is to update your copy of pony, for example with gem update pony or if you are using bundler then bundle update. And if the gem is defined in a Gemfile then make sure there is no version restriction on it.

I noticed you said you're using 1.12 and that you've tried rolling the version forward and backward, but that's not accurate. You're using 1.11 and you can tell because the path in your error message is gems/pony-1.11. So however it is your application is configured, it is configured to use 1.11. If you still can't get your app to use 1.12 then please post a comment explaining how you are managing gems for your app.

Finally, I recommend that you reformat your code to make it a little more readable:

def place
  Pony.mail(
    to: StoreApplication::Admin.email,
    from: 'My store <[email protected]>',
    via: :smtp,
    via_options: {
      address: 'smtp.gmail.com',
      port: '587',
      user_name: '[email protected]',
      password: '###########',
      authentication: :plain,
      domain: 'mail.google.com'
    },
    subject: 'New order',
    body: 'Check your admin page'
  )
end

Changes made include:

  • Change double quotes to single quotes (single quotes except when using string interpolation)
  • Fix indentation (two spaces)
  • Fix hash keys (don't use :key => value, use key: value)
  • Fix spelling on adress to address
  • Remove extra spaces
  • Properly align blocks and closures

The Ruby Style Guide can help with making sense of the recommended changes.

Upvotes: 4

Related Questions