ActionMailler and Gmail (no plugins)

Last week I had some problems while trying to configure the ActionMailler with Gmail. I already have some applications in production mode recurring to this system, but this one, using ruby 1.8.7 and rails 2.3.3 gave me some trouble. Gmail requires TLS, older versions of ruby and rails do not support that, the work around was a pluging to enhance it(for example ActionMailerTLS). If you are using Ruby 1.8.7 and Rails 2.2.1, or later versions, you don’t need any plugin, you only need to follow my instructions.

Add the following lines in your config/enviroments/production.rb

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = YAML.load File.open("#{RAILS_ROOT}/config/mailer.yml")

And then create the yaml file config/mailer.yml :

---
  :enable_starttls_auto: true # This is how you tell ActionMailler to use tls
  :address: smtp.gmail.com
  :port: 587
  :domain: gmail.com
  :user_name: my_username #You should type your username, you should not include the "@gmail.com".
  :password: my_password #You should type your password
  :authentication: :plain

That’s it!

Now take a look in this notifier class example:

class Notifier < ActionMailer::Base
  def activation_instructions(user)
    @recipients  = "#{user.email}"
    #@from        = "Miguel <my_username@gmail.com>" # Doesn't work anymore 
    @from        = "my_username@gmail.com"
    @subject     = "[Card-Sorting] "
    @sent_on     = Time.now
    @subject    += "Important"
    @body[:user] = user
  end
end

There is a lack of backward compatible. Due to some changes in the latest versions you cannot use that syntax anymore, if you are upgrading from old versions be careful, you should only simply specify your email to get it correctly parsed.

Gato Fedorento – Esmiúça os Sufrágios

Apenas uma referencia para quem me pediu os links:
1º Episódio – part1, part2, part3 (José Sócrates)
2º Episódio – part1, part2, part3 (Manuela Ferreira Leite)
3º Episódio – part1, part2, part3 (Paulo Portas)
4º Episódio – part1, part2, part3 (Francisco Louçã)
5º Episódio – part1, part2, part3 (Paulo Rangel)
6º Episódio – part1, part2, part3 (Jerónimo de Sousa )
7º Episódio – part1, part2, part3 (Joana Amaral DIas )
8º Episódio – part1, part2 (Ministro Teixeira dos Santos )
9º Episódio – part1, part2, part3 (Maria José Nogueira Pinto )
10º Episódio – part1, part2 (Garcia Pereira )
11º Episódio – part1, part2, part3 (António Costa )
12º Episódio – part1, part2, part3 (Pedro Santana Lopes)
13º Episódio – part1, part2, part3 (Rui Rio)
14º Episódio – part1, part2, part3 (Luis Filipe Menezes)
15º Episódio – part1, part2, part3 (Mário Soares)
16º Episódio – part1, part2, part3 (Pedro Passos Coelho)
17º Episódio – part1, part2, part3 (Elisa Ferreira)
18º Episódio – part1, part2, part3 (Marques Mendes)
19º Episódio – part1, part2, part3 (António Marinho Pinto)
20º Episódio – part1, part2, part3 (Nuno Melo)
21º Episódio – part1, part2, part3 (Jorge Coelho)
22º Episódio – part1, part2, part3 (Francisco Moita Flores)
23º Episódio – part1, part2, part3 (Mário Lino)
24º Episódio – part1, part2, part3 (General Ramalho Eanes)
25º Episódio – part1, part2, part3 (Bernardino Soares)
26º Episódio – part1, part2, part3 (Marcelo Rebelo de Sousa)
27º Episódio – part1, part2, part3 (Francisco Assis, José Pedro Aguiar Branco)
28º Episódio – part1, part2, part3, part4 (José Alberto de Carvalho, Julio Magalhães, Rodrigo Guedes de Carvalho)

My friends twitter updates on Google Reader

I was trying to get this working, but Twitter RSS feeds now require authentication, and Google Reader does not support authentication.
A service called http://freemyfeed.com can strip away the authentication from the feed, and gives you a new RSS feed that you can then supply to Google Reader or IGoogle.

Change default user shell

Example of how to use chsh command to change current user shell to bash:

chsh -s /bin/bash

Git push to remote

Just a note, this is the way to push the current branch to a branch on a remote git repository using ssh trough non default port:

git push ssh://remote_user@domain.com:remote_port/~/remote_repository remote_branch

Git diff pacth

How to create a patch with git diff:

git diff HEAD~2 > changes.diff


How to use it:

patch -Np2 -i changes.diff

thanks to Andy ;)

Hash to Object

Sometimes I have the need to create objects that responds to some methods with a specific values.
Something that I can use like
HashObject.new :method1 => value_for_method1, :method2 => value_for_method2
There is already a way to do it, with OpenStruct, but I created a HashObject. Just for academic proposals. I didn’t knew the OpenStruct at the time.

class HashObject
  def initialize(hash)
    hash.each do |k,v|
      self.instance_variable_set("@#{k}", v)
      self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})
      self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)})
    end
  end

  def to_hash
    hash_to_return = {}
    self.instance_variables.each do |var|
      hash_to_return[var.gsub("@","")] = self.instance_variable_get(var)
    end
    return hash_to_return
  end
end

Object.tot

I know Rails 2.3 is out and has a new “try” method, But I was looking into an old app and I saw some code I have written. It’s extending the object class with a method called tot (that or that lolol) so that you can use it anywhere like in the example:

tot do params[:search][:name] end

or like that

tot(:also => ["","nome feio"], return => "nome invalido") do params[:search][:name] end

this last example case will return “nome invalido” in case of params[:search][:name] being empty string, “nome feio”, or if a NoMethodError happens”, other wise it will return params[:search][:name] value

And that is my code extending object class.

class Object
  def tot(options = {})
    begin
      (options[:also] and options[:also].include?(yield)) ? options[:return] : yield
      rescue NoMethodError
      options[:return]
    end
  end
end

I found it funny, anyway Ruby on Rails got it’s own try method built in some months later.

Git clone from remote repositorium with ssh and non default port

just a reminder, suppose your ssh port is 3022: (sometimes I swap the way to specify the port in those two commands)

git clone ssh://user@domain.com:3022/~/Projects/my_project

ssh user@domain.com -p 3022


(when connecting through ssh the non default port goes as param, the default port is 22)

Javascript auto include rails plugin

I need to check this link latter:

http://blog.media72.net/2008/05/13/javascript-auto-include-rails-plugin/

I haven’t tried it yet but seems a good way to maintain your .js files organized.

Next Page »


Last Regedor’s twitts

  • Preparing a presentation for my Cryptography work "Leetus" a secure peer-to-peer sharing system! 3 days ago
  • is watching naruto! 3 days ago

Last Regedor's bookmarks