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

TypeError when sending email in 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。

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

事实证明,当您不使用 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
  }