在ActionMailer中发送电子邮件时出现TypeError

时间:2015-07-09 13:25:32

标签: ruby-on-rails ruby email actionmailer

我正在尝试在我的Rails应用程序中设置邮件程序,该邮件程序向系统用户发送电子邮件。当我尝试发送电子邮件时,它正在处理,并且已完成的电子邮件将被放入日志中,但在流程结束时我收到错误:

TypeError (no implicit conversion of Symbol into Integer):
  app/controllers/users_controller.rb:58:in `email'

我没有收到电子邮件,因此在创建或发送电子邮件时似乎发生了错误。

以下是我的配置和调用电子邮件的代码:

Production.rb

# Mailer Configuration
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address => 'corp-alt-14',  # Local mail server on the network
  :port => 25,
  :openssl_verify_mode => :none
}

users_controller.rb

def email
  user = User.find(params[:user_id])
  UserMailer.welcome_email(user).deliver_now  # Line 58
  render nothing: true   #Ajax request should not update the page
end

user_mailer.rb

class UserMailer < ApplicationMailer
  def welcome_email(user)
    @user = user
    mail(to: @user.email, subject: 'Welcome Test Email')
  end
end

application_mailer.rb

class ApplicationMailer < ActionMailer::Base
  default from: "DoNotReply@example.com"
  layout 'mailer'
end

production.log

I, [2015-07-08T15:55:08.569007 #8496]  INFO -- : Started GET "/users/email?user_id=2" for ::1 at 2015-07-08 15:55:08 -0400
I, [2015-07-08T15:55:08.571007 #8496]  INFO -- : Processing by UsersController#email as JS
I, [2015-07-08T15:55:08.571007 #8496]  INFO -- :   Parameters: {"user_id"=>"2"}
D, [2015-07-08T15:55:08.572007 #8496] DEBUG -- :   [1m[36mUser Load (0.0ms)[0m  [1mEXEC sp_executesql N'SELECT  [users].* FROM [users] WHERE [users].[id] = @0  ORDER BY [users].[id] ASC OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY', N'@0 int', @0 = 2[0m  [["id", "2"]]
I, [2015-07-08T15:55:08.575007 #8496]  INFO -- :   Rendered user_mailer/welcome_email.html.erb within layouts/mailer (1.0ms)
I, [2015-07-08T15:55:08.575007 #8496]  INFO -- :   Rendered user_mailer/welcome_email.text.erb within layouts/mailer (0.0ms)
D, [2015-07-08T15:55:08.577007 #8496] DEBUG -- : 
UserMailer#welcome_email: processed outbound mail in 4.0ms
I, [2015-07-08T15:55:08.614007 #8496]  INFO -- : 
Sent mail to evan_frisch@example.com (36.0ms)
D, [2015-07-08T15:55:08.614007 #8496] DEBUG -- : Date: Wed, 08 Jul 2015 15:55:08 -0400
From: DoNotReply@example.com
To: evan_frisch@example.com
Message-ID: <559d801c8d1d7_21301f76380640db@FRISCHE.mail>
Subject: Welcome Test Email
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_559d801c8ca07_21301f7638063943";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_559d801c8ca07_21301f7638063943
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

This is a test email.

Hello Evan

----==_mimepart_559d801c8ca07_21301f7638063943
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<html>
  <body>
    <h1>This is a test email</h1>

<p>Hello Evan</p>
  </body>
</html>

----==_mimepart_559d801c8ca07_21301f7638063943--

I, [2015-07-08T15:55:08.615007 #8496]  INFO -- : Completed 500 Internal Server Error in 44ms
F, [2015-07-08T15:55:08.623007 #8496] FATAL -- : 
TypeError (no implicit conversion of Symbol into Integer):
  app/controllers/users_controller.rb:58:in `email

我正在使用Ruby 2.0.0和Rails 4.2.0。

据我所知,我的代码中没有符号被转换为整数。我试图调试到邮件程序代码,以查看错误被引发的位置,但我找不到它。我不知道我的代码是否有问题,或者我的配置是否错误。我已经确认了邮件服务器的名称和端口,以便数据至少正确。任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:2)

事实证明,当您不使用SSL并且没有SMTP时,只设置:openssl_verify_mode => :none是不够的。添加:enable_starttls_auto => false我的错误消失了,电子邮件就开始发送了。它通常默认为true,这显然导致错误而不使用安全用户。

要注意我的最终SMTP设置是:

config.action_mailer.smtp_settings = {
      :address => 'corp-alt-14',
      :port => 25,
      :openssl_verify_mode => :none,
      :enable_starttls_auto => false
  }