Joel Always one syllable, sometimes "@jayroh"

Using Google Apps Email as Your App’s SMTP Server

27 Apr 2009 #email #gmail #google #rails #ruby #smtp

Something I’ve held out on for a while now has been to switch over the settings for ActionMailer in my application(s) to point to my hosted Google apps account.   I figured it was probably time to do so as piping email notifications through my comcast email account is generally, probably, a bad idea (courtesy of the “No Duh” department).

Seems like it should be rather easy, no?  Just change action mailer to resemble:

ActionMailer::Base.smtp_settings = {
  :address => "smtp.gmail.com",
  :port => 587,
  :domain => "hosteddomain.com",
  :authentication => :plain,
  :user_name => "account@hosteddomain.com",
  :password => "omgsup3rsecret"
}

Meh. Looks easy enough, right? Except for the fact Google’s got some magic TLS authentication thing going on – you’ll run into an error in your mailers resembling Must issue a STARTTLS command first.. Enough to make you work a little harder to get the magic working.

For those of you/us that are running Ruby 1.8.7 and Rails 2.3.x the answer is rather simple – add :enable_starttls_auto => true to your smtp settings, which will result in :

ActionMailer::Base.smtp_settings = {
  :enable_starttls_auto => true,
  :address => "smtp.gmail.com",
  :port => 587,
  :domain => "hosteddomain.com",
  :authentication => :plain,
  :user_name => "account@hosteddomain.com",
  :password => "omgsup3rsecret"
}

And for the rest of you/us (that would be me) that are still sticking with Ruby 1.8.6, there is an answer in the form of the action_mailer_tls gem. Following the readme will get you to right where you want to be – shoveling all the mail you would like into the ether that is the interwebs.

Link Slugs with Javascript

26 Feb 2008 #javascript #rails #ruby #seo

Over at Thredded I am still using Rails 1.2.3 (I’m a little gun-shy to upgrade to 2.0) and, of course, felt that slugged links were necessary for both search engine optimization and making things like assessing site analytics a little easier. It doesn’t even need justification as it’s a matter of fact and necessity for any and all social platforms – blogging, forums, etc. With RoR 1.2.3 the best way to get your links slugging it out was to incorporate a plugin like acts_as_sluggable. It works like a charm, really, and I’ve never had any case where I needed extra functionality.

… Until now. I’ve started incorporating some auto-updating magic to Thredded and needed to grab a lot of data back from an AJAX call (sorry Steve – XHR) in the form of JSON. All well and good so far. But, when new links needed to be built on the client side, I didn’t have my handy built-in Rails ActiveRecord overrides to spit out my new slugged-up link! What to do?!

I dug through the plugin source and found the function that built the url’s slug -

def make_slug(string)
      string.to_s.downcase.gsub(/[^a-z0-9] /, '-').gsub(/- $/, '').gsub(/^- $/, '')
end

… And thought the quickest solution was just to rewrite it as a simple JS function.

function slug(id, title)
{
      title = title.toLowerCase().replace(/[^a-z0-9] /g,'-').replace(/- $/g,'').replace(/^- $/g,'');
      return(id '-' title);
}