ruby-on-rails - mandrill 使用 devise 2.2.3 破解重置密码指令

标签 ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 devise mandrill

我在本地的所有电子邮件都可以与我的 Gmail 帐户配合使用。但是在生产环境中,我使用 mandrill发送电子邮件。

我的问题是当用户想要重置密码时。这是我在生产中的电子邮件配置:

config.action_mailer.default_url_options = { :host => 'mydomain.com' }
 config.action_mailer.delivery_method = :smtp
 config.action_mailer.perform_deliveries = true
 config.action_mailer.raise_delivery_errors = false
 config.action_mailer.default :charset => "utf-8"
 config.action_mailer.asset_host = "mydomain.com"

  ActionMailer::Base.default :from => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bed6c7cedbccccd4dfcdfed3c7dad1d3dfd7d090ddd1d3" rel="noreferrer noopener nofollow">[email protected]</a>'
  config.action_mailer.smtp_settings = {
    :address   => "smtp.mandrillapp.com",
    :port      => 25,
    :user_name => Settings.email.username,
    :password  => Settings.email.password
  }

这是我的邮件 Controller :

require 'digest/sha2'
class UserMailer < Devise::Mailer
default "Message-ID"=>"#{Digest::SHA2.hexdigest(Time.now.to_i.to_s)}@mydomain.com"
  def confirmation_instructions(record, opts={})
    set_locale(record)
    headers["template_path"] = "user_mailer"
    headers["template_name"] = "confirmation_instructions"
    headers({'X-No-Spam' => 'True', 'In-Reply-To' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a0c8d9d0c5d2d2cac1d3e0cdd9c4cfcdc1c9ce8ec3cfcd" rel="noreferrer noopener nofollow">[email protected]</a>'})
    super
  end

  def reset_password_instructions(record, opts={})
    set_locale(record)
    headers["template_path"] = "user_mailer"
    headers["template_name"] = "reset_password_instructions"
    headers({'X-No-Spam' => 'True', 'In-Reply-To' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b03121b0e1919010a182b06120f04060a020545080406" rel="noreferrer noopener nofollow">[email protected]</a>'})
    super
  end

  def unlock_instructions(record, opts={})
    set_locale(record)
    headers["template_path"] = "user_mailer"
    headers["template_name"] = "unlock_instructions"
    headers({'X-No-Spam' => 'True', 'In-Reply-To' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83ebfaf3e6f1f1e9e2f0c3eefae7eceee2eaedade0ecee" rel="noreferrer noopener nofollow">[email protected]</a>'})
    super
  end
  private
  def set_locale(user)
    I18n.locale = user.locale || I18n.default_locale
  end
end

这是生产中出现问题的链接:

<%= link_to "Change password", edit_password_url(@resource, :reset_password_token => @resource.reset_password_token) %>

Mandrill 生成一个链接,如下所示:

http://mandrillapp.com/track/click.php?u=30029014&id=890aac6b235b4802883f75b484d5ac8f&url=http%3A%2F%2Fmydomain.com%2Fusers%2Fpassword%2Fedit&url_id=18754ce20fc88b338f0aa3993686e33be8ab84a1

但是在开发中工作正常:

http://localhost:3000/users/password/edit?reset_password_token=yzYyyZuZArq7ksLZdgh3

当使用点击链接时,我可以看到错误:

You can't access this page without coming from a password reset email. If you do come from a password reset email, please make sure you used the full URL provided

如何使用 mandrill 修复此错误?

谢谢!

最佳答案

我已经使用下一个代码解决了问题:

def reset_password_instructions(record, opts={})
  set_locale(record)
  headers["template_path"] = "user_mailer"
  headers["template_name"] = "reset_password_instructions"
  headers({'X-No-Spam' => 'True', 'In-Reply-To' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="026a7b72677070686371426f7b666d6f636b6c2c616d6f" rel="noreferrer noopener nofollow">[email protected]</a>'})
  headers['X-MC-Track'] = "False, False"
  super
end

您必须将 headers['X-MC-Track'] = "False, False" 添加到您的操作 reset_password_instructions

您可以在 http://help.mandrill.com/entries/21688056-Using-SMTP-Headers-to-customize-your-messages#enable-open-and-click-tracking 的 mandrill 帮助中查看该文档

谢谢!

关于ruby-on-rails - mandrill 使用 devise 2.2.3 破解重置密码指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19038123/

相关文章:

ruby - 使用 Rails 3 从数据库中获取数据数组

ruby-on-rails - Geokit 问题 - "could not find generator"

ruby-on-rails - Ruby 从数组中随机选择有时会重复返回相同的值

ruby-on-rails - Puma 可以从变量读取 TLS 证书吗?

ruby-on-rails - 我将如何解析此 url 以获取 id?

ruby-on-rails-3 - 存储在数据库中的 Rails session

ruby-on-rails - 设计限制每个用户一次只能进行一个 session

ruby-on-rails - 如何在重定向时显示 Rails flash 通知?

ruby-on-rails - 部署到 capistrano 时如何调试?

facebook - 导轨 : Could not authenticate you from Facebook because "Invalid credentials"