Rails电子邮件与取消订阅链接 [英] Rails Email with Unsubscribe link

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

问题描述

我正在使用一个Rails 4.2应用程序,该应用程序每周都会有重复的事件进行注册。他们将在每个活动之前收到提醒电子邮件(每周)。我想在电子邮件中单击取消订阅链接。这似乎是一个常见的任务,但我没有找到一个很好的当前解决方案。我看到的一些方向是使用新的Rails 4.1的MessageVerifier,并且不需要保存令牌字符串以与数据库进行比较。要做到这一点的步骤是什么我有一个用户模型和事件模型。电子邮件将发送给已注册的定期活动的注册用户。

I am working on a Rails 4.2 app that has recurring weekly events that people register for. They will get a reminder email before each event (so weekly). I want a one click unsubscribe link on the email. This seems like a common task but I haven't found a good current solution. Some directions I have seen are to use MessageVerifier which was new to Rails 4.1 and doesn't require saving a token string to compare to in the database. What are the steps to accomplish this. I have a user model and an event model. Emails are sent to registered users who signed up for the recurring event.

推荐答案

这是解决方案,我想到了一个取消订阅链接的常规电子邮件发送给订阅者。它使用MessageVerifier。

Here is the solution I came up with for an unsubscribe link for regular emails sent to subscribers. It uses MessageVerifier.

在我的活动控制器中,我有一个send_notice操作发送电子邮件。我将为取消订阅链接创建一个变量,并将其传递给邮件程序。

In my Events controller I have a send_notice action that sends the email. I'll create a variable for the unsubscribe link and pass it to the mailer.

# app/controller/events_controller.rb
def send_notice
  ...
  @unsubscribe = Rails.application.message_verifier(:unsubscribe).generate(@user.id)
  EventMailer.send_notice(@event, @user, @unsubscribe).deliver_later
end

这需要用户标识,并生成一个签名和编码字符串变量@unsubscribe。消息验证者将用户ID转换成不可破译的字符串,将其与您的secret_key_base(位于文件config / secrets.yml中)进行混合,以及您给出消息的名称(在这种情况下,我称之为:取消订阅但是它可以是任何名称),就像它们称为盐,并通过称为SHA1的算法运行它。确保您的secret_key_base保持安全。使用环境变量存储生产中的值。
在最后一行,我们将@unsubscribe变量与@event和@user变量一起传递给邮件程序。

This takes the user id and generates a signed and encoded string variable @unsubscribe. Message verifier turns the user id into an indecipherable string of characters by mixing it with your secret_key_base (found in the file config/secrets.yml), and the name that you give the message (in this case I'm calling it :unsubscribe but it could be any name) as what they call a salt, and running it through an algorithm called SHA1. Make sure your secret_key_base stays secure. Use environmental variables to store the value in production. In the last line we pass the @unsubscribe variable to the mailer along with @event and @user variables.

# app/mailers/event_mailer.rb
def send_notice(event, user, unsubscribe)
  @event = event
  @user = user
  @unsubscribe = unsubscribe
  mail(to: user.email, subject: "Event Info")
end 



3。将取消订阅链接放在电子邮件中



3. Put the unsubscribe link in the email

# app/views/events_mailer/send_notice.html.erb
...
<%= link_to "Unsubscribe", settings_unsubscribe_url(id: @unsubscribe) %>.

请注意添加了(id:@unsubscribe)参数。这将附加编码的用户ID到URL开头的一个?在所谓的查询参数中。

Notice the (id: @unsubscribe) argument added. This will append the encoded user id to the end of the URL beginning with a ? in what is known as a query param.

添加将用户带到取消订阅页面的路由。然后再提交取消订阅的另一条路线。

Add a route that takes the user to an unsubscribe page. Then another route for when they submit the unsubscribe.

# config/routes.rb
get 'settings/unsubscribe'
patch 'settings/update'



5。向用户模型添加订阅字段



我选择向用户模型添加订阅boolean(true / false)字段,但可以通过多种不同的方式进行设置。 rails g migration AddSubscriptionToUsers subscription:boolean

我选择添加一个具有取消订阅和更新操作的设置控制器。生成控制器 rails g controller设置取消订阅
当用户点击电子邮件中的取消订阅链接时,它将转到该网址,并将编码后的用户标识添加到该问号后的URL作为查询参数。以下是该链接的示例: http:// localhost ?:3000 /设置/取消ID = BAhpEg%3D%3D - be9f8b9e64e13317bb0901d8725ce746b156b152
取消订阅操作将使用MessageVerifier对id参数进行解码和解码,以获取实际的用户标识,并使用它来查找用户并将其分配给@user变量。

I opted to add a settings controller with an unsubscribe and an update action. Generate the controller rails g controller Settings unsubscribe. When the user clicks on the unsubscribe link in the email it will go to the url and append the encoded User ID to the URL after the question mark as a query param. Here's an example of what the link would look like: http://localhost:3000/settings/unsubscribe?id=BAhpEg%3D%3D--be9f8b9e64e13317bb0901d8725ce746b156b152. The unsubscribe action will use MessageVerifier to unsign and decode the id param to get the actual user id and use that to find the user and assign it to the @user variable.

# app/controllers/settings_controller.rb
def unsubscribe
  user = Rails.application.message_verifier(:unsubscribe).verify(params[:id])
  @user = User.find(user)
end

def update
  @user = User.find(params[:id])
  if @user.update(user_params)
    flash[:notice] = 'Subscription Cancelled' 
    redirect_to root_url
  else
    flash[:alert] = 'There was a problem'
    render :unsubscribe
  end
end

private
  def user_params
    params.require(:user).permit(:subscription)
  end



7。视图



添加包含表单的取消订阅页面以取消订阅。

7. Views

Add the unsubscribe page that includes a form to cancel your subscription.

# app/views/settings/unsubscribe.html.erb
<h4>Unsubscribe from Mysite Emails</h4>
<p>By unsubscribing, you will no longer receive email...</p>
<%= form_for(@user, url: settings_update_path(id: @user.id)) do |f| %>
  <%= f.hidden_field(:subscription, value: false) %>
  <%= f.submit 'Unsubscribe' %>
  <%= link_to 'Cancel', root_url %>
<% end %>

有很多方法可以设置。这里我使用form_for帮助器路由到settings_update_path,添加了一个参数:@ user.id。当将表单作为查询参数发送时,这将附加用户ID到URL。更新操作将读取它以查找用户并将Subscription字段更新为false。

There are a number of ways you can set this up. Here I use the form_for helper routed to the settings_update_path with an added argument of id: @user.id. This will append the user id to the URL when sending the form as a query param. The Update action will read it to find the user and update the Subscription field to false.

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

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