2

我在本地的所有电子邮件都可以在我的 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 => 'hyperrjas@mydomain.com'
  config.action_mailer.smtp_settings = {
    :address   => "smtp.mandrillapp.com",
    :port      => 25,
    :user_name => Settings.email.username,
    :password  => Settings.email.password
  }

这是我的邮件控制器:

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' => 'hyperrjas@mydomain.com'})
    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' => 'hyperrjas@mydomain.com'})
    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' => 'hyperrjas@mydomain.com'})
    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 修复此错误?

谢谢!

4

3 回答 3

2

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

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' => 'hyperrjas@mydomain.com'})
  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 帮助中的文档

谢谢!

于 2013-09-27T08:38:18.377 回答
1

如果您在 Mandril 中打开点击跟踪,它不应该用自己的 URL 替换您的 URL。然后您的链接应该按预期工作。试一试,看看是否可以仅对确认电子邮件禁用它:

当您执行 API 调用发送时,请确保设置"track_clicks": false https://mandrillapp.com/api/docs/messages.html

或者,您可以创建自定义路由,例如http://localhost:3000/users/password/edit/token/yzYyyZuZArq7ksLZdgh3,其中password_token是路径的一部分,而不是参数的一部分(在 之后的部分?)。然后,您可以处理该路径并password_token从中获取路径,然后再传递给 Devise。

第一种方法可能更简单。您真的不需要对确认电子邮件进行点击跟踪,因为您会根据他们是否访问您的网站并确认来知道他们是否点击!

于 2013-09-26T20:48:46.110 回答
1

如果您愿意在全局范围内关闭它,您可以通过 Web UI 来完成。

您可以通过 Mandrillapp 或 Heroku 到达那里。

对于您的应用:

单击 Mandrill > 设置 > 发送默认值 > 跟踪点击(无点击跟踪)

于 2014-12-04T16:08:33.463 回答