Rails 3.2 ActionMailer handle unsubscribe link in emails

时光总嘲笑我的痴心妄想 提交于 2019-12-08 05:43:45

问题


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.

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>

回答1:


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)

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

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.



来源:https://stackoverflow.com/questions/9592144/rails-3-2-actionmailer-handle-unsubscribe-link-in-emails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!