Haml and Google SMTP for email

I first thought that I would have to use ERB to render non-HTML-like templates. Not true. Haml let you do a :plain filter which is pretty much the same as using ERB templates.

Here’s a simple example haml template for email:

1
2
3
4
5
6
:plain
  Welcome #{@user.username} to Fund-A-Stache! 
 
  click the following link to activate your account:
 
  #{activation_link(@user)}

I’m also using Google SMTP for email. That way I can send email from my home (which blocks SMTP traffic). This may change, but just so I can remember why I’m doing this, here’s the Pony call:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Pony.mail(
  :to=>@user.email, 
  :from=>'user@example.org', 
  :subject=>'Welcome', 
  :body=>(haml :registration_email, :layout=>false),
  :content_type=>'text/html',
  :via=>:smtp,
  :smtp=>{
    :host=>'smtp.gmail.com',
    :port=>'587',
    :tls=>true,
    :user=>'user@example.org',
    :password=>'secret',
    :auth=>:plain, # :plain, :login, :cram_md5, no auth by default
    :domain=>"example.org" # the HELO domain provided by the client to the server
  }
)
Posted: March 13th, 2010 | Author: jay | Filed under: Code | Tags: , , , , , , | No Comments »

Trying out Pony for email

Email is going to be used to activate an account, and I’m leaning towards using the Pony gem to simplify the creations and sending.

Here’s how simple Pony is:

1
Pony.mail(:to => 'test@example.com', :from=>'test@example.com', :subject=>'spam', :body=>(erb :registration_email))

and here’s how you can test it with the pony-test gem:

1
2
3
4
5
6
7
8
9
10
  def test_valid_new_user_signup_should_create_email
    count=all_email.count
    create_user
    assert_equal count+1, all_email.count
  end
 
  def test_signup_email_should_contain_activation_link
    create_user
    assert_match /http\:\/\//, current_email.body
  end

Don’t forget to nclude the Pony test helpers in your test class…

1
include Pony::TestHelpers
Posted: March 12th, 2010 | Author: jay | Filed under: Code | Tags: , , , , | No Comments »