【问题标题】:rails, action mailer not sending though logs say it israils,action mailer 不发送虽然日志说它是
【发布时间】:2012-04-25 05:16:08
【问题描述】:

我正在使用设计,并在有人使用 actionmailer 注册后尝试发送欢迎电子邮件。

我用...覆盖了设计上的注册控制器

  def create
    build_resource

    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?


        UserMailer.welcome_email(resource).deliver


        sign_in(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      respond_with resource
    end
  end

在那一行

UserMailer.welcome_email(resource).deliver

我打电话给我的 user_mailer.rb

  default :from => "blink@gmail.com"

  def welcome_email(user)
    @user = user
    @url = "http://example.com/login"
    mail(:to => user.email, :submit => "Welcome YEAH!")     
  end

我在 app/views/user_mailer/welcome_email.text.erb 下有一个视图

在我的初始化程序文件夹中,我有一个 setup_mail.rb

ActionMailer::Base.smtp_settings = {
    :address                => "smtp.gmail.com",
    :port                   => 587,
    :domain                 => "gmail.com",
    :user_name              => "blink@gmail.com",
    :password               => "example",
    :authentication     => "plain",
    :enable_starttls_auto   => true
}

在我的 development.rb 我有...

config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true

我非常困惑为什么这不起作用。我写了一个较旧的项目,我让动作邮件程序开始工作。现在的代码与我的旧项目几乎相同。例外是我现在使用设计。

在我的终端窗口中,当我运行“rails server”时,在该窗口中它还会显示...

Sent mail to blink@gmail.com (140ms)
Date: Thu, 12 Apr 2012 12:32:48 -0700
From: blink@gmail.com
To: blink@gmail.com
Message-ID: <4f872de096f4e_1805f3fdba4834cd493153@spiderman.local.mail>
Subject: Welcome email
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_4f872de0883d4_1805f3fdba4834cd49281a";
charset=UTF-8
Content-Transfer-Encoding: 7bit
submit: Welcome YEAH!

用户注册后。我不认为它真的发送虽然。我的 gmail 从来没有得到它,它不在我的垃圾邮件中。可能与设计的邮件/视图有关吗?但我在控制器中明确使用 UserMailer,这就是我覆盖它的原因

我被困了好几天!帮助将不胜感激。非常感谢

【问题讨论】:

    标签: ruby-on-rails devise actionmailer


    【解决方案1】:

    首先,你必须确保你告诉设计要寻找你的控制器(因为你已经对它进行了子类化,我假设)。您可以像这样编辑config/routes.rb 来做到这一点:

    # config/routes.rb
    devise_for :users, :controllers => {:registrations => "registrations"}
    

    如果您已这样做,请确保您的控制器是正在使用的控制器。此外,关于sendmail,我还没有尝试过使用它,但假设sendmail 使用您的主机操作系统的内部邮件代理,并且您处于动态IP 地址下(ISP 在连接时将其提供给其用户) , 我相信像 GMail 这样的邮件提供商会阻止您的邮件,因为他们不允许动态 IP 作为邮件提供商。他们这样做主要是出于安全目的。或许你可以通过编辑config/environments/development.rb来检查:

    # config/environments/development.rb
    config.action_mailer.raise_delivery_errors = true
    

    看看引擎盖下是否发生了什么事。如果这没有太大帮助(并且由于您实际上尝试做的是使用有效的邮件帐户发送电子邮件 - 给定您的 ActionMailer 设置),您可以使用 SMTP 传递而不是 sendmail,以及您拥有的配置,编辑这一行:

    # config/environment/development.rb
    config.action_mailer.delivery_method = :smtp
    

    在您的初始化程序中,它实际上将通过指定的帐户将电子邮件发送给指定的收件人。

    作为最后的(离题)建议,我建议您使用Rails Observers,在这种情况下,您不需要覆盖 Devise 的注册控制器。这将使您的生活更加简单、干燥并遵循约定优于配置的理念。然后你可以像这样创建一个观察者:

    # app/observers/user_observer.rb
    class UserObserver < ActiveRecord::Observer
      def after_create(user)
        UserMailer.welcome_email(user).deliver
      end
    end
    

    并编辑config/application.rb:

    config.active_record.observers = :user_observer
    

    【讨论】:

    • 难以置信。老实说,我以为我尝试将其更改为 smtp。我一定是在别的地方做的。它也可以通过不配置来工作,因为我猜它默认为 smtp(至少基于我的设置方式)。非常感谢它。非常感谢!
    【解决方案2】:

    你试过了吗

    config.action_mailer.delivery_method = :smtp
    

    看看 mailcatcher 用于测试开发中的电子邮件,我发现它比实际发送邮件要容易得多。

    【讨论】:

      猜你喜欢
      • 2011-05-15
      • 2011-10-07
      • 2013-05-21
      • 2015-08-23
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 2012-01-14
      • 2012-02-06
      相关资源
      最近更新 更多