19

If I will send 100 email to the registered user and I want to know if users open email or not

How can I do this using Ruby on Rails?

Sander de Jong
  • 351
  • 6
  • 18
AMIC MING
  • 6,306
  • 6
  • 46
  • 62
  • Doon's answer below is thorough and accurate. I find these kind of requirements to usually come from stakeholders who only see the "mail" part of email and insist on applying please-sign-here snail mail delivery guarantees to email. I think you're better off talking to you client and explaining the limitations to them. – Mhmmd Aug 19 '10 at 19:05
  • 2
    The problem is not in wanting to know if it was opened, the problem is with the "or not". Adding the traditional web bug to an HTML email can tell you if an email was opened. It may have been opened and you not know it, but it is pretty unlikely that the bug would say it was opened when it wasn't. While less than accurate, it is still a good measure. Especially when wanting to compare the success of one campaign to another. – Jackson Miller Feb 17 '12 at 02:35

3 Answers3

26

The only way to do this, is to use html email with a tracker image. You need to include a user specific image into the code.

class TrackingController < ApplicationController
  def image
    # do something with params[:id]
    send_file "/path/to/an/image"
  end
end

add the following route:

# Rails 2
map.tracking_image "tracking_image/:id.gif", :controller => 'tracking', :action => image

# Rails 3
match 'products/:id', :to => 'tracking#image', :as => "tracking_image"

# Rails 4 (match without verb is deprecated)
get 'producsts/:id' => 'tracking#image', as: 'tracking_image'
# or
match 'producsts/:id' => 'tracking#image', as: 'tracking_image', via: :get

in your email template something like this:

<%= image_tag tracking_image_url(@user.id) %>

But be aware, that this it's not guaranteed that the user reads the email and loads the image, some email clients don't load images, until the user wants to. And If he doesn't you can't do anything about this. Also if the user uses text mail only this won't work neither.

jigfox
  • 18,057
  • 3
  • 60
  • 73
  • 2
    for render images in the browser ---- send_file "/path/to/an/image.gif", :type => 'image/gif', :disposition => 'inline' ---- send_file "/path/to/an/image.jpg", :type => 'image/jpeg', :disposition => 'inline' – SyntaxGoonoo Sep 27 '11 at 22:43
  • technical point of view - this answer solved my problem. Yes - I am totally agree with @Doon – AMIC MING Feb 21 '12 at 23:53
9

Short answer, You can't. Slightly longer answer You can't reliably.

Using something like VERP you can automate the the bounce processing, to get a fairly good idea if the far end mail server accepted the email. But after that all bets are off. You can't really tell what the email server did with it, (route it to junk/spam folder, put in inbox, silently drop it on the floor/bit bucket, etc..). You could enable read-receipt headers in your email, but that is client specific (and people like me eat/deny them). You can look into using a web bug, for example customize each email with an HTML file, that pulls a remote image, that has a unique id associated with it, but again client specific, most will not load remote images. So unless the email bounces there is no 100% reliable way to tell what happens to the email after it leaves your server.

Doon
  • 19,719
  • 3
  • 40
  • 44
2

I am not very familiar with ruby but have written multiple mass mailer apps. You can use a webbug image to get an approximate open rate. Basically it is just a one pixel or transparent image with some tracking information:

<img src="http://mysite/trackingimage.gif?email=x&customer=y">

What I do is make a directory called trackingimage.gif with an index in it that reads and stores the url params and then relocates to the real image.

Nick Van Brunt
  • 15,244
  • 11
  • 66
  • 92