【问题标题】:Actionmailer not sending email even though no error即使没有错误,Actionmailer 也不发送电子邮件
【发布时间】:2013-12-07 08:56:43
【问题描述】:

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

有效的邮件程序如下所示:

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

在发送密码重置电子邮件时,我在日志中收到以下内容:

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.

第一个邮寄者正确地发送电子邮件并记录它。第二个邮件程序既不发送电子邮件也不记录电子邮件。我不确定在哪里可以解决问题,因为我收到的任何错误消息绝对为零。

任何帮助我指出正确的方向来解决这个问题将不胜感激。

【问题讨论】:

  • 发布调用邮件程序函数的控制器操作。

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


【解决方案1】:

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

PoolMailer.send_pool_message(...).deliver

否则它只会生成电子邮件而不发送它

【讨论】:

  • 这可能不是答案。 OP 说邮件程序的第一次迭代成功发送 - 只有后续迭代没有通过。
  • 据我了解,他说的是第一种邮件程序(UserMailer)而不是第一次迭代
  • @Marc-AlexandreBérubé 感谢您的帮助!我忙于查看所有邮寄物品,我完全错过了明显的问题!
  • 不!也经常发生在我身上:)
猜你喜欢
  • 2021-01-25
  • 1970-01-01
  • 2021-06-19
  • 1970-01-01
  • 1970-01-01
  • 2014-01-14
  • 2023-03-19
  • 1970-01-01
  • 2014-12-31
相关资源
最近更新 更多