How to generate Unsubscription link for emails of Actionmailer?

后端 未结 1 1523
借酒劲吻你
借酒劲吻你 2021-01-16 05:20

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

Now I want the clients to have subscriptio

相关标签:
1条回答
  • 2021-01-16 06:11

    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.

    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
    

    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
    

    Hook it up to a route:

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

    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) %>
    

    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.

    UPDATE:

    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) }
    
    0 讨论(0)
提交回复
热议问题