Search code examples
emailruby-on-rails-4bccunsubscribe

Rail 4 mailer sending mass email with bcc and unsubscribe feature


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: "[email protected]", bcc: addresses,subject: "Posts of past week")

Solution

  • 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.