Rails 3.2 ActionMailer处理电子邮件中的退订链接 [英] Rails 3.2 ActionMailer handle unsubscribe link in emails

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

问题描述

我有一个客户列表,我使用ActionMailer邮件程序发送电子邮件。我正在尝试在每封电子邮件的底部编写一个链接,以便客户可以退订邮件。到目前为止,我的客户数据库中有一个opted_out字段,我只是不确定如何正确设置路由并使其更新正确的数据库字段。我要进行设计,以便用户可以简单地单击链接和中提琴,取消订阅。

I have a list of customers that I send emails using an ActionMailer mailer. I am trying to code a link at the bottom of each email so a customer can unsubscribe from a mailing. So far I have an opted_out field in my customers database, I am simply unsure how to properly set up the route and have it update the correct database field. I want to design this so the user can simply click the link and viola, unsubscribed.

在blast_mailer.rb中

in blast_mailer.rb

  def mail_blast(customer, blast)
    @customer = customer
    @blast =blast
#what conditional statement goes here?
    mail(:to => @customer.email, :subject => @blast.subject)
    @blast.update_attributes(:last_sent => DateTime.now)
  end



in mail_blast.html.erb

 <div style="font-family:Helvetica; font-size:12pt; font-style:italic; width:500px; height:auto;">
        <img src="http://www.ratatouillecatering.com/<%=asset_path("emailheader.png")%>" alt="Menu" />
        <br />
        <br />
        Dear <%= @customer.fname %> <br />

        <%= raw(@blast.content) =%> <br />
#what goes here? a link_to what?
        <br />
    </div>


推荐答案

在users表中我会有一个布尔字段,称为已订阅。这样,您可以选择所有已订阅该电子邮件的用户。

I would have a boolean field in the users table called something like subscribed. This way, you can just select all Users that are subscribed to the email.

User.where(:subscribed => true)

然后,您可以在翻转布尔值的控制器中设置退订操作。 / p>

You can then set up an unsubscribe action in a controller that flips the boolean.

def unsubscribe
  User.find(params[:id]).update_attributes(:subscribed => false)
end

所有您需要做的就是在电子邮件模板中传递此操作的链接,并随其传递用户ID。可以设置路由,使URL看起来像 www.example.com/users/<id>/取消订阅

All you have to do is pass in a link to this action in the email template, and have the user's ID be passed in with it. The route can be setup so that the URL looks like www.example.com/users/<id>/unsubscribe.

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

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