如何为 Actionmailer 的电子邮件生成取消订阅链接? [英] How to generate Unsubscription link for emails of Actionmailer?

查看:44
本文介绍了如何为 Actionmailer 的电子邮件生成取消订阅链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 ID、姓名和电子邮件字段的表 CLIENTS,我正在使用 ActionMailer 和 3rd 方 SMTP 向他们发送电子邮件.

I have a table CLIENTS with id, name and email fields and I am sending them emails using ActionMailer with 3rd party SMTP.

现在我希望客户端也有订阅选项,所以我添加了默认值为 true 的订阅"列.

Now I want the clients to have subscription option too so I added "subscription" column with default value as true.

现在如何生成一个可以放入视图邮件模板的链接,以便当用户点击它时,订阅值更改为 false 以便将来客户不会收到任何电子邮件?请注意,这些客户端不是我的 rails 应用程序用户,所以我不能使用这里建议的内容 Rails 3.2 ActionMailer 处理电子邮件中的取消订阅链接

Now how to generate a link which can be put in views mailer template so when the user clicks on it, the subscription value changes to false so in future the client dont' get any email ? Do note that these clients are not my rails app users so I can't uses what is been suggested here Rails 3.2 ActionMailer handle unsubscribe link in emails

我也找到了这个链接如何生成取消订阅电子邮件的链接,这看起来很有帮助,但我认为可能在 3年,我们可能有更好的解决方案

I found this link how to generate link for unsubscribing from email too which looked helpful but I thought may be in 3 years, we might have got a better solution

这是我的完整代码 -

#client.rb

attr_accessible :name, :company, :email

belongs_to :user
has_many :email_ids
has_many :emails, :through => :email_ids

before_create :add_unsubscribe_hash

private

def add_unsubscribe_hash
  self.unsubscribe_hash = SecureRandom.hex
end  

这是 Clients_controller.rb 文件

Here is Clients_controller.rb file

# clients_controller.rb

  def new
    @client = Client.new

    respond_to do |format|
      format.html
      format.json { render json: @client }
      format.js
    end
  end

  def create
    @client = current_user.clients.new(params[:client])
    respond_to do |format|
      if @client.save
        @clients = current_user.clientss.all
        format.html { redirect_to @client }
        format.json { render json: @client }
        format.js
      else
        @clients = current_user.clients.all
        format.html { render action: "new" }
        format.json { render json: @client.errors, status: :error }
        format.js
      end
    end
  end

def unsubscribe
  @client = Client.find_by_unsubscribe_hash(params[:unsubscribe_hash])
  @client.update_attribute(:subscription, false)
end

代码对现有记录工作正常,取消订阅工作正常,我只是在创建新客户时遇到问题.

我在取消订阅方法中使用了@client,因为我在 client_mailer.rb 模板中使用了这个对象(使用 @client 或只使用客户端,两者都在工作!)

I have used @client in unsubscribe method as I am using this object in client_mailer.rb template (using @client or just using client, both are working!)

编辑 2 -_form.html.erb

EDIT 2 - _form.html.erb

<%= simple_form_for(@client, :html => {class: 'form-horizontal'}) do |f| %>

    <%= f.input :name, :label => "Full Name" %>
    <%= f.input :company %>
    <%= f.input :email %>
    <%= f.button :submit, class: 'btn btn-success' %>
<% end %>

我已在 http://jsfiddle.net/icyborg7/dadGS/复制了完整的轨道堆栈

推荐答案

尝试将每个客户端与唯一但模糊的标识符相关联,该标识符可用于通过电子邮件中包含的取消订阅链接查找(和取消订阅)用户.

Try associating each client with a unique, but obscure, identifier which can be used to look up (and unsubscribe) the user via the unsubscribe link contained within the email.

首先在您的客户表中添加另一列名为 unsubscribe_hash:

Start by adding another column to your clients table called unsubscribe_hash:

# from command line
rails g migration AddUnsubscribeHashToClients unsubscribe_hash:string

然后,为每个客户端关联一个随机哈希:

Then, associate a random hash with each client:

# app/models/client.rb
before_create :add_unsubscribe_hash

private

def add_unsubscribe_hash
    self.unsubscribe_hash = SecureRandom.hex
end

创建一个控制器动作,将 subscription 布尔值切换为 true:

Create a controller action that will toggle the subscription boolean to true:

# app/controllers/clients_controller.rb
def unsubscribe
    client = Client.find_by_unsubscribe_hash(params[:unsubscribe_hash])
    client.update_attribute(:subscription, false)
end

将其连接到路线:

# config/routes.rb
match 'clients/unsubscribe/:unsubscribe_hash' => 'clients#unsubscribe', :as => 'unsubscribe'

然后,当客户端对象传递给 ActionMailer 时,您将可以访问 unsubscribe_hash 属性,您可以通过以下方式将其传递给链接:

Then, when a client object is passed to ActionMailer, you'll have access to the unsubscribe_hash attribute, which you can pass to a link in the following manner:

# ActionMailer view
<%= link_to 'Unsubscribe Me!', unsubscribe_url(@user.unsubscribe_hash) %>

当点击链接时,将触发unsubscribe动作.客户端将通过传入的 unsubscribe_hash 进行查找,subscription 属性将变为 false.

When the link is clicked, the unsubscribe action will be triggered. The client will be looked up via the passed in unsubscribe_hash and the subscription attribute will be turned to false.

更新:

为现有客户端的 unsubscribe_hash 属性添加值:

To add a value for the unsubscribe_hash attribute for existing clients:

# from Rails console
Client.all.each { |client| client.update_attribute(:unsubscribe_hash, SecureRandom.hex) }

这篇关于如何为 Actionmailer 的电子邮件生成取消订阅链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆