Follow Along as I Stumble on the Path to Learning RoR

Posts tagged “email

Testing Email with RSpec

I ran into two types of errors when testing if emails triggered in the code with RSpec.

The first issue was that RSpec was not correctly determining if an email was triggered.  I knew the email was getting triggered correctly, but my test was failing.  The reason was that I was putting the should matcher too late.

Mailer.should_receive(:email_name)

I was putting this after calling the method I was testing (i.e. post :create) like I do with all of my other matchers.  However, with Mailer.should_receive, it should go before the call.

The second issue I had was that I was getting the error message:

Undefined method ‘deliver’ for nil:NilClass

For this, I had to stub out the response, like so:

Mailer.should_receive(:email_name).and_return(double(“Mailer”, :deliver => true))

Advertisements

How to Get Free Email Hosting for Your Domain

Google Apps no longer offers free email hosting for small businesses.  No worries – Microsoft offers free hosting through it’s new and improved Outlook and it’s extremely easy to set-up.

(1) go to https://domains.live.com and sign in (sign-up for a free live.com account if you don’t have one)

(2) add your domain

(3) open another browser window and log into your account with your domain name registrar (the website where you registered your domain (ie register.com or Go Daddy))

(4) in your domain name registrar account, go to where your domain name is managed and look for the place where you change nameservers.  once there, add the records listed in https://domains.live.com

(5) after you confirmed the settings are in place on https://domains.live.com, add a new mailbox (ie info for info@yourdomain.com)

(6) test the changes by sending an email to your new account (ie info@yourdomain.com) and then logging in via live.com


Using Amazon SES to Send Emails from Rails

I wanted to switch from Sendgrid to Amazon SES to send emails because of the difference in pricing.  Adding Amazon SES to your Rails app is just as simple as adding Sendgrid would be.  I was able to set it up based on one of the answers to this Stack Overflow questions.  There’s a gem for this, but I prefer not using a gem when the solution is this simple.

  1. Sign-up for your Amazon SES account
  2. In your management console for Amazon SES, click the button for “Verify a New Sender”
  3. Specify the email address you want to send your emails from
  4. Add an email to test receiving emails at (you can only send to verified email addresses until you get production access)
  5. Verify your email by clicking the confirmation link from the automated email Amazon sends you
  6. Go back to Amazon SES, and click on the left side for SMTP settings
  7. Click the button for “Create My SMTP credentials”
  8. Record the user name and password created
  9. Add this to your development and production.rb files:
config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      :address => "email-smtp.us-east-1.amazonaws.com",
      :user_name => "..." # Your SMTP user here.
      :password => "...", # Your SMTP password here.
      :authentication => :login,
      :enable_starttls_auto => true
  }

You should be all set - request production access once you've tested that your app is sending the emails ok.

Welcome Email After Devise Confirmation

After getting :confirmable working for the Rails Devise gem, I wanted to create a welcome email that would tell the user that they successfully confirmed their account and welcoming them to the site.

Here are the steps I took:

  1. Create the email views and placed them in my mailer directory
  2. I use User.rb as my model, so I created an observer to watch changes to the models.  In that app/models/user_observer.rb, I put this code:

def after_save(user)
# Send Welcome Mail after confirmation
if user.confirmed_at_changed?
Mailer.welcome_mail(user).deliver!
end
end

This watches for the change in confirmed_at, then sends my email (welcome_mail).

3.  In application.rb, added this:

config.active_record.observers = :user_observer


Mail on Desk.com

Desk.com provides free access to their software for one agent, so I set this up.  It took me a little more time because I use Heroku (which doesn’t host mail), with Google Apps mail.  Normally you would use your DNS settings for mapping the email in Desk.com.  However, since my email is hosted on Google Apps, I needed to route it through them.

Ironically, there’s no documentation on this on desk.com, which is software for the help desk.  You’d think they would be better about offering help since they provide help desk software – not the case.

It took me awhile, but what worked was, in the email (channel) settings:

  • enter the email with my domain (ie info@mydomain.com)
  • select IMAPS
  • enter the host as imap.gmail.com

When I tried other settings, such as pop.gmail.com or smtp.gmail.com I got errors indicating that the certification did not match (ie domain name vs gmail.com).  When I wrote desk.com’s support they said it was an adjustment on the mail side.  This was not the case I found, after playing with it for an hour, it was just a setting change within desk.com.