【问题标题】:Rails 4, Devise & Mandrill emailsRails 4、Devise 和 Mandrill 电子邮件
【发布时间】:2016-04-21 15:21:29
【问题描述】:

我正在尝试在 Rails 4 中制作应用程序。

在过去的 3 年里,我一直在努力找出设计/omniauth(我仍在努力让它发挥作用)。

抛开主要问题,当我尝试找到度过这个难关的意愿时,我尝试使用 Mandrill 设置电子邮件。

我找到了这个教程,我正在努力学习:https://nvisium.com/blog/2014/10/08/mandrill-devise-and-mailchimp-templates/

我有一个名为 mandrill_devise_mailer.rb 的邮件程序

class MandrillDeviseMailer < Devise::Mailer

  def confirmation_instructions(record, token, opts={})
    # code to be added here later
  end

  def reset_password_instructions(record, token, opts={})
    options = {
      :subject => "Reset your password",
      :email => record.email,
      :global_merge_vars => [
        {
          name: "password_reset_link",
          # content: "http://www.example.com/users/password/edit?reset_password_token=#{token}"
          content: "http://www.cr.com/users/password/edit?reset_password_token=#{token}"

        },

        {
          name: "PASSWORD_RESET_REQUEST_FROM",
          content: record.full_name 
        }
      ],
      :template => "Forgot Password"
    }
    mandrill_send options  
  end

  def unlock_instructions(record, token, opts={})
    # code to be added here later
  end

  def mandrill_send(opts={})
    message = { 
      :subject=> "#{opts[:subject]}", 
      :from_name=> "Reset Instructions",
      # :from_email=>"example@somecorp.com",
      :from_email=>["PROD_WELCOME"],
      :to=>
            [{"name"=>"#{opts[:full_name]}",
                "email"=>"#{opts[:email]}",
                "type"=>"to"}],
      :global_merge_vars => opts[:global_merge_vars]
      }
    sending = MANDRILL.messages.send_template opts[:template], [], message
    rescue Mandrill::Error => e
      Rails.logger.debug("#{e.class}: #{e.message}")
      raise
  end
end

以上内容与他们在教程中所做的不同之处在于:

在我的邮件黑猩猩山魈模板中,我有:

<a href="*|password_reset_link|*">Change my password </a>

当我收到重置说明的电子邮件时,我会看到一个带下划线的链接,指向更改密码表单,上面写着“更改我的密码”。我希望“将我的密码更改为隐藏链接文本的标签”。

谁能看到我做错了什么?

【问题讨论】:

  • 您的:from_email 设置为["PROD_WELCOME"],这在我看来不像是电子邮件地址。
  • 谢谢 - 设置 ENV 使电子邮件发送,但它打印 || 之间的单词而不是拉入变量
  • 也许您正在使用把手作为您的默认合并语言?转到设置下的 Mandrill sending options 并检查您是否启用了正确的合并语言。
  • 嗨 Dylan,设置已设置为 Mailchimp。我把它们改成了车把,但预览仍然显示错误
  • 很确定您想要车把。您在上面使用的 mergetag 语法使用 Mailchimp 格式。

标签: ruby-on-rails email devise actionmailer mandrill


【解决方案1】:

这是我创建自定义 DeviseMailer 的方式

class MyDeviseMailer < Devise::Mailer   
  default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views

  def reset_password_instructions(record, token, opts={})
    opts['from_email'] = "donotreply@mywebsite.com"
    opts['from_name'] = "Password Reset"
    #Rails.logger.mail.info "reset_password_instructions #{record.to_json} \n #{token.to_json} \n #{opts.to_json}"
    super
  end

end

https://github.com/plataformatec/devise/wiki/How-To:-Use-custom-mailerAdd dynamic value in devise email subject

【讨论】:

  • 嗨,Dmitry,看起来您并没有尝试使用 mailchimp/mandrill。还是谢谢。
  • Mandrill API 需要那些 opts['from_email'] 和 opts['from_name'] 参数。我为 alanverga.com/blog/2014/01/03/custom-rails-mailer-and-mandrillrobots.thoughtbot.com/… 添加了自定义交付类
  • 但我的文件也有这些选项。我的问题是链接的格式不像我表达的那样。相反,链接出现在可见的标签内,旁边的链接标签为文本。
猜你喜欢
  • 2012-04-21
  • 1970-01-01
  • 2015-08-25
  • 1970-01-01
  • 2015-11-08
  • 1970-01-01
  • 2015-10-10
  • 2016-01-22
  • 1970-01-01
相关资源
最近更新 更多