0

I'm developing a small blog with subscribing feature. It will notify top posts of past week to subscribers via a bcc mail with mandrill service. It works fine but I got stuck on unsubscribing feature. As far as I know, the mail that any subscriber receives is identical to bcc so may be I . So my question is how can I attach a link that is unique to each subscriber via bcc mail that will allow them to unsubscribe? Here a snippet of my code:

addresses = subscription_emails.all.pluck(:email)
mail(from: "no-reply@blog.com", bcc: addresses,subject: "Posts of past week")
Phuong Dao
  • 185
  • 1
  • 2
  • 12

1 Answers1

0

As far as I understand your question, the unsubscribe url will be in the contents of the email.

For getting that to work, I'd suggest you to generate a unique identifier to the mail template which you can then use to identify the user that is unsubscribing.

Check: http://guides.rubyonrails.org/action_mailer_basics.html point 2.1.3 - Create a Mailer View.

Lets say that your template would have something along the lines:

<p>
  Here is your email contents
</p>
<p><a href="#{@unsubscribe_url}">Unsubscribe</a></p>

This unsubscribe_url variable would generate you something like:

http://your-domain.com/newsletter/unsubscribe/unique-id-generated-for-the-user

Create a route in your config/routes.rb file that takes you to the controller that handles the unsubscription.

You can also take a look at here: http://railscasts.com/episodes/312-sending-html-email

EDIT

Unfortunately, from what I see in Mandrill's documentation I don't think you have a way to do a 1 to 1 mapping of the merge variables with the emails in the bcc list.

This way I see two possible solutions:

a) You don't really have a unique id for each user but rather you have a generic link that will take you to an unsubscribe page. For example:

http://your-domain.com/newsletter/unsubscribe

This would render a page with an input where you'd ask the person to write their email address. Using this solution, you'd have to take in account bad user behaviour such as:

  • inserted email does not belong to your user list
  • alice typed bob's email

b) you break the logic of email requests to mandrill. This way you'd keep the unique id login but instead of sending a list of emails to be in bcc, you'd send email request one by one.

rpbaltazar
  • 801
  • 7
  • 15
  • Yes, you understand my problem. But may I clarify, I send a mail via **blind carbon copy**, so rails only renders a mail and send bcc list to mail server for distributing. And that's the problem, I cant generate url that are unique to user because mail server basically duplicate the mail view and mass sending to users. Your instruction would be perfect if I send mail to **each** of my clients. – Phuong Dao Nov 19 '15 at 11:40
  • Based on your comment, I've updated my answer as it was too long for a comment. – rpbaltazar Nov 20 '15 at 02:57
  • On mandrill document i saw there is a way. Mandrill will generate unsubscribe url to mail, if user click it, the email address will be record in mandrill block list. I use the generic link instead – Phuong Dao Nov 21 '15 at 05:55
  • Well, but using that way you won't know in your app who has asked to unsubscribe. If in the future you decide to change your mailer service you'll have to fetch the list and exclude it from your database. – rpbaltazar Nov 23 '15 at 03:34
  • You are right, seems like create a generic unsubscribe url is a right choice – Phuong Dao Nov 23 '15 at 07:42