{"id":51,"date":"2011-09-15T10:22:42","date_gmt":"2011-09-15T14:22:42","guid":{"rendered":"http:\/\/unixmonkey.net\/?p=51"},"modified":"2019-05-14T15:08:45","modified_gmt":"2019-05-14T19:08:45","slug":"rails-3-mail-gem-error-creating-mail","status":"publish","type":"post","link":"https:\/\/unixmonkey.net\/?p=51","title":{"rendered":"Rails 3 Mail gem error creating mail"},"content":{"rendered":"<p>I&#8217;m in the process of upgrading a Rails app from 2 to 3, and this proved to be a stumbling block.<\/p>\n<p>Creating a message of any kind would result in a message like this:<\/p>\n<pre>NoMethodError: undefined method `new' for \"smtp\":String\nfrom gems\/mail-2.2.15\/lib\/mail\/message.rb:252:in `delivery_method'\nfrom gems\/actionmailer-3.0.0\/lib\/action_mailer\/delivery_methods.rb:74:in `wrap_delivery_behavior'\nfrom gems\/actionmailer-3.0.0\/lib\/action_mailer\/delivery_methods.rb:83:in `wrap_delivery_behavior!'\nfrom gems\/actionmailer-3.0.0\/lib\/action_mailer\/base.rb:641:in `mail'\nfrom app\/mailers\/developer_mailer.rb:22:in `email_error_message'<\/pre>\n<p>This behavior comes from a method named lookup_delivery_method in the Mail gem.<br \/>\nThe problem is that this method assumes the ActionMailer::Base.delivery_method to return a symbol like :smtp instead of a string like &#8220;smtp&#8221;. The old behavior worked fine in Rails 2.x apps.<\/p>\n<p>I have different mailer settings per environment and load the settings in pretty much the same way Redmine does, by looping over a yml config file and using send to set actionmailer settings. The solution is to make sure it is created as a symbol like so:<\/p>\n<pre lang=\"ruby\"># Loads ActionMailer settings from config\/email.yml\n# and turns deliveries on only if configuration block is found\nconfig_file = Rails.root.join('config','email.yml')\n\nif File.file?(config_file)\n  mailconfig = YAML::load_file(config_file)\n\n  if mailconfig.is_a?(Hash) &amp;&amp; mailconfig.has_key?(Rails.env)\n  # enable deliveries\n  ActionMailer::Base.perform_deliveries = true\n\n  mailconfig[Rails.env].each do |key, value|\n    if value.respond_to?(:symbolize_keys!)\n      value.symbolize_keys!\n    elsif value.respond_to?(:to_sym)\n      value = value.to_sym\n    end\n    ActionMailer::Base.send(\"#{key}=\", value)\n  end\nelse\n  # disable deliveries\n  ActionMailer::Base.perform_deliveries = false\nend\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m in the process of upgrading a Rails app from 2 to 3, and this proved to be a stumbling block. Creating a message of any kind would result in a message like this: NoMethodError: undefined method `new&#8217; for &#8220;smtp&#8221;:String from gems\/mail-2.2.15\/lib\/mail\/message.rb:252:in `delivery_method&#8217; from gems\/actionmailer-3.0.0\/lib\/action_mailer\/delivery_methods.rb:74:in `wrap_delivery_behavior&#8217; from gems\/actionmailer-3.0.0\/lib\/action_mailer\/delivery_methods.rb:83:in `wrap_delivery_behavior!&#8217; from gems\/actionmailer-3.0.0\/lib\/action_mailer\/base.rb:641:in `mail&#8217; from app\/mailers\/developer_mailer.rb:22:in `email_error_message&#8217; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[29],"_links":{"self":[{"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/posts\/51"}],"collection":[{"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=51"}],"version-history":[{"count":0,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions"}],"wp:attachment":[{"href":"https:\/\/unixmonkey.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}