12-10-11

HTML Not Rendering in Gmail (SendGrid)

SendGrid

I’ve been handling many of my company’s email campaigns for some time now and I can honestly say that it’s been pretty fun from a technical stand-point. There’s a lot that goes into email marketing (not including the marketing/sales implications). It has it’s own conventions, ideologies, and I believe to the utmost that there’s a larger future than presumed in store for the sometimes misunderstood technology. (I’m patiently waiting for a mobile device header-type.)

Recently I’ve been using my new skills while working on my own applications using SendGrid as an alternative SMTP and email analytic tool. And I’ve only really come across one major programmable error. If you use a desktop client instead of a browser for viewing emails, as I do oftentimes, then this issue might not be so apparent (till now).

I recently was witness to how Gmail handles email headers in regards to HTML v. text-only. Gmail’s SaaS actually only reads the latter MIME-type and throws away the rest. So if you have a text version being made available, and you want your emails to be seen strictly as HTML, you must render the HTML version in your code LAST. Here is a Ruby on Rails example:

class Notifier < ActionMailer::Base
  include SendGrid
  default :from => Rails.configuration.sendgrid.reply_to
  sendgrid_enable :opentrack, :clicktrack, :ganalytics
  sendgrid_category :testing
  
  def user_updated(user)
    @user = user
    mail( :to => @user.email,
          :subject => "Your information has been updated.") do |format|
      format.text { "#{user.first_name},\n\nThis is an auto-response to inform you that your information has changed." }
      format.html { render :layout => 'autoresponse' }
    end
  end
end

Super “thanks” go out to the SendGrid crew for their constantly superior customer service skills and for helping me out on my way to becoming an email marketing specialist.

11-14-11

Export CSV with Mongoid and FasterCSV

In my research I found a hundred and more different ways to manage the task of exporting data into a CSV file. Whenever I’m learning something new, on a new system or with a new language, I like to build the process myself from scratch. And being a minimalist programmer it just seems fitting. Enjoy

def export_inverts
  require 'fastercsv'
  inverts = Invert.all
  filename = params[:action] + ".csv"

  #this is required if you want this to work with IE
  if request.env['HTTP_USER_AGENT'] =~ /msie/i
    headers['Pragma'] = 'public'
    headers["Content-type"] = "text/plain"
    headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0'
    headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
    headers['Expires'] = "0"
  else
    headers["Content-Type"] ||= 'text/csv'
    headers["Content-Disposition"] = "attachment; filename=\"#{filename}\""
    headers["Content-Transfer-Encoding"] = "binary"
  end

  csv_string = FasterCSV.generate do |csv|
    csv << ["Genus","Species","Common Name","Pet Name","Gender"]
    inverts.each do |i|
      csv << [i.scientific_name,i.scientific_name,i.common_name,i.pet_name,i.gender]
    end
  end
  render :text => csv_string
end
view raw export_csv.rb This Gist brought to you by GitHub.

NOTE: Developers using Ruby 1.8 <= must install the fastercsv gem.