即使没有错误,Actionmailer也不会发送电子邮件

时间:2013-12-07 00:26:02

标签: ruby-on-rails email ruby-on-rails-4 actionmailer

我的系统上设置了几个不同的邮件程序。一个工作正常,另一个没有错误,但不发送电子邮件。

工作的邮件看起来像这样:

class UserMailer < ActionMailer::Base
  default from: "info@footballpoolmania.com"

  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.user_mailer.password_reset.subject
  #
  def password_reset(user)
    @user = user
    mail to: user.email, subject: "Password Reset"
  end

  def confirm_registration(user)
    @user = user
    mail to: user.email, subject: "Confirm Registration"
  end
end

当发送password_reset电子邮件时,我在日志中得到以下内容:

Started POST "/password_resets" for 127.0.0.1 at 2013-12-06 17:24:32 -0600
Processing by PasswordResetsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"jwGUtsyNRbCfBRFJm3uqcVZA4luHc05Gf0OBm4jyFpI=", "email"=>"robertr2112@yahoo.com", "commit"=>"Reset Password"}
  [1m[35mUser Load (0.8ms)[0m  SELECT "users".* FROM "users" WHERE "users"."email" = 'robertr2112@yahoo.com' LIMIT 1
  [1m[36m (0.2ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (2.7ms)[0m  UPDATE "users" SET "password_reset_token" = $1, "updated_at" = $2 WHERE "users"."id" = 2  [["password_reset_token", "c851e80faeb3612c654c512857cfb29afc6a4665"], ["updated_at", Fri, 06 Dec 2013 23:24:32 UTC +00:00]]
  [1m[36m (5.8ms)[0m  [1mCOMMIT[0m
  [1m[35m (1.2ms)[0m  BEGIN
  [1m[36mSQL (1.5ms)[0m  [1mUPDATE "users" SET "password_reset_sent_at" = $1, "updated_at" = $2 WHERE "users"."id" = 2[0m  [["password_reset_sent_at", Fri, 06 Dec 2013 23:24:32 UTC +00:00], ["updated_at", Fri, 06 Dec 2013 23:24:32 UTC +00:00]]
  [1m[35m (1.6ms)[0m  COMMIT
  Rendered user_mailer/password_reset.text.erb (2.3ms)

Sent mail to robertr2112@yahoo.com (316.8ms)
Date: Fri, 06 Dec 2013 17:24:32 -0600
From: info@footballpoolmania.com
To: robertr2112@yahoo.com
Message-ID: <52a25cb0c52be_c94585623640840@debian.mail>
Subject: Password Reset
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit

To reset your password, click the URL below.

http://localhost:3000/password_resets/c851e80faeb3612c654c512857cfb29afc6a4665/edit

If you did not request your password to be reset, just ignore this email and 
your password will continue to stay the same.

Redirected to http://localhost:3000/
Completed 302 Found in 380ms (ActiveRecord: 13.7ms)

此外,该消息的视图定义为password_reset.text.erb,位于app / views / user_mailer中,如下所示:

To reset your password, click the URL below.

<%= edit_password_reset_url(@user.password_reset_token) %>

If you did not request your password to be reset, just ignore this email and 
your password will continue to stay the same.

不起作用的邮件看起来像这样:

class PoolMailer < ActionMailer::Base
  default from: "info@footballpoolmania.com"

  def send_pool_message(pool, subject, msg, allMembers)
    @pool = pool
    @msg = msg
    email_list = Array.new
    @pool.users.each do |user|
      if allMembers
        entries = Entry.where(user_id: user.id)
      else
        entries = Entry.where(pool_id: pool.id, user_id: user.id, survivorStatusIn:true)
      end
      if entries.any?
        email_list << "#{user.name} <#{user.email}>"
      end
    end
    subject_text = pool.name + ": " + subject
    if mail to: email_list, subject: subject_text
      puts "Successfully sent email"
      return false
    else
      puts "Couldn't send email"
      return true
    end
  end
end

日志如下所示:

Started POST "/pools/2/pool_messages" for 127.0.0.1 at 2013-12-06 17:50:54 -0600
Processing by PoolMessagesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"jwGUtsyNRbCfBRFJm3uqcVZA4luHc05Gf0OBm4jyFpI=", "subject"=>"This is a test", "msg"=>"A test of the group pool message.", "allMembers"=>"false", "commit"=>"Send email", "pool_id"=>"2"}
  [1m[36mPool Load (8.9ms)[0m  [1mSELECT "pools".* FROM "pools" WHERE "pools"."id" = $1 LIMIT 1[0m  [["id", "2"]]
  [1m[35mUser Load (0.7ms)[0m  SELECT "users".* FROM "users" INNER JOIN "pool_memberships" ON "users"."id" = "pool_memberships"."user_id" WHERE "pool_memberships"."pool_id" = $1  [["pool_id", 2]]
  [1m[36m (0.4ms)[0m  [1mSELECT COUNT(*) FROM "entries" WHERE "entries"."user_id" = 2[0m
  [1m[35m (0.3ms)[0m  SELECT COUNT(*) FROM "entries" WHERE "entries"."user_id" = 3
  [1m[36m (0.3ms)[0m  [1mSELECT COUNT(*) FROM "entries" WHERE "entries"."user_id" = 4[0m
  [1m[35m (0.4ms)[0m  SELECT COUNT(*) FROM "entries" WHERE "entries"."user_id" = 5
  Rendered pool_mailer/send_pool_message.text.erb (0.7ms)
Redirected to http://localhost:3000/pools/2
Completed 302 Found in 1527ms (ActiveRecord: 11.0ms)

此电子邮件的视图名为send_pool_message.text.erb,位于app / views / pool_mailer中,如下所示:

<%= @msg %>

This is an automated email sent by the pool administer.  Do not reply to this
email.

第一封邮件正确发送邮件并将其记录下来。第二个邮件程序既不发送电子邮件也不记录电子邮件。我不确定在哪里寻找纠正问题,因为我在任何一个上都得到绝对零错误消息。

任何帮助我指出正确方向来解决这个问题都会非常感激。

1 个答案:

答案 0 :(得分:1)

您需要将.deliver调用到您的邮件程序功能

PoolMailer.send_pool_message(...)。递送

另外,它只会在不提供电子邮件的情况下生成电子邮件