Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoMethodError: undefined method ascii_only? - Error with mail 2.5.4 and rails 4.0

Please find below error message occured when I tried to deliver mails with mail 2.5.4 / rails 4.0.0 / ruby 1.9.3-p125 configuration.

SubscriptionMailer.send_email(Subscription.last).deliver
  Subscription Load (0.6ms)  SELECT `subscriptions`.* FROM `subscriptions` ORDER BY `subscriptions`.`id` DESC LIMIT 1
  User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
  Rendered subscription_mailer/send_email.text.erb (0.6ms)
  Rendered subscription_mailer/send_email.html.haml (5.7ms)
NoMethodError: undefined method `ascii_only?' for nil:NilClass
  from /opt/blog/vendor/ruby/1.9.1/gems/mail-2.5.4/lib/mail/encodings.rb:68:in `param_encode'
  from /opt/blog/vendor/ruby/1.9.1/gems/mail-2.5.4/lib/mail/fields/content_type_field.rb:95:in `block in stringify'
  from /opt/blog/vendor/ruby/1.9.1/gems/mail-2.5.4/lib/mail/fields/content_type_field.rb:95:in `each'
  from /opt/blog/vendor/ruby/1.9.1/gems/mail-2.5.4/lib/mail/fields/content_type_field.rb:95:in `map'
  from /opt/blog/vendor/ruby/1.9.1/gems/mail-2.5.4/lib/mail/fields/content_type_field.rb:95:in `stringify'
  from /opt/blog/vendor/ruby/1.9.1/gems/mail-2.5.4/lib/mail/fields/content_type_field.rb:88:in `value'
  from /opt/blog/vendor/ruby/1.9.1/gems/mail-2.5.4/lib/mail/fields/content_type_field.rb:28:in `parse'
  from /opt/blog/vendor/ruby/1.9.1/gems/mail-2.5.4/lib/mail/fields/content_type_field.rb:24:in `initialize'
  from /opt/blog/vendor/ruby/1.9.1/gems/mail-2.5.4/lib/mail/field.rb:203:in `new'
  from /opt/blog/vendor/ruby/1.9.1/gems/mail-2.5.4/lib/mail/field.rb:203:in `new_field'
  from /opt/blog/vendor/ruby/1.9.1/gems/mail-2.5.4/lib/mail/field.rb:192:in `create_field'
  from /opt/blog/vendor/ruby/1.9.1/gems/mail-2.5.4/lib/mail/field.rb:149:in `update'
  from /opt/blog/vendor/ruby/1.9.1/gems/mail-2.5.4/lib/mail/header.rb:170:in `[]='
  from /opt/blog/vendor/ruby/1.9.1/gems/mail-2.5.4/lib/mail/message.rb:588:in `content_type='
  from /opt/blog/vendor/ruby/1.9.1/gems/actionmailer-4.0.0/lib/action_mailer/base.rb:695:in `mail'
  from /opt/blog/app/mailers/subscription_mailer.rb:19:in `send_email'
... 8 levels...
  from /opt/blog/vendor/ruby/1.9.1/gems/actionmailer-4.0.0/lib/action_mailer/base.rb:497:in `initialize'
  from /opt/blog/vendor/ruby/1.9.1/gems/actionmailer-4.0.0/lib/action_mailer/base.rb:480:in `new'
  from /opt/blog/vendor/ruby/1.9.1/gems/actionmailer-4.0.0/lib/action_mailer/base.rb:480:in `method_missing'
  from (irb):4
  from /opt/blog/vendor/ruby/1.9.1/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
  from /opt/blog/vendor/ruby/1.9.1/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
  from /opt/blog/vendor/ruby/1.9.1/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
  from /opt/blog/vendor/ruby/1.9.1/gems/railties-4.0.0/lib/rails/app_rails_loader.rb:43:in `require'
  from /opt/blog/vendor/ruby/1.9.1/gems/railties-4.0.0/lib/rails/app_rails_loader.rb:43:in `block in exec_app_rails'
  from /opt/blog/vendor/ruby/1.9.1/gems/railties-4.0.0/lib/rails/app_rails_loader.rb:32:in `loop'
  from /opt/blog/vendor/ruby/1.9.1/gems/railties-4.0.0/lib/rails/app_rails_loader.rb:32:in `exec_app_rails'
  from /opt/blog/vendor/ruby/1.9.1/gems/railties-4.0.0/lib/rails/cli.rb:6:in `<top (required)>'
  from /opt/blog/vendor/ruby/1.9.1/gems/railties-4.0.0/bin/rails:9:in `require'
  from /opt/blog/vendor/ruby/1.9.1/gems/railties-4.0.0/bin/rails:9:in `<top (required)>'
  from bin/rails:16:in `load'
  from bin/rails:16:in `<main>'1.9.1 :005 > 
like image 553
Gokula Murthy Avatar asked Sep 23 '14 13:09

Gokula Murthy


3 Answers

I had a similar problem that occurred when sending attachments.

I needed to change attachments[:report] to attachments['report']

old:

def email_report(email, report) attachments[:report] = File.read(report) mail(to: email, subject: 'report') end

new:

def email_report(email, report) attachments['report'] = File.read(report) mail(to: email, subject: 'report') end

I suspect something similar is happening to you.

like image 159
jasonaibrahim Avatar answered Oct 30 '22 00:10

jasonaibrahim


I ran into this issue because I had set a User-instance in the 'to'-field instead of the user.email value.

like image 44
Hugo Logmans Avatar answered Oct 30 '22 01:10

Hugo Logmans


I ran into this error message when sending activestorage attached files as mail attachments. the problem was in the filename:

files.each do |file|
  # this next line does not work, throws NoMethodError: undefined method ascii_only?
  filename = file.filename  
  attachments[filename] = {
    mime_type: file.blob.content_type,
    content: file.blob.download
  }
end

when I convert the filename to a string explicitly the error went away:

files.each do |file|
  # convert to string and it does work!
  filename = file.filename.to_s
  attachments[filename] = {
    mime_type: file.blob.content_type,
    content: file.blob.download
  }
end
like image 38
bjelli Avatar answered Oct 29 '22 23:10

bjelli