neezer
neezer

Reputation: 20560

Send emails from Rails 3 in development mode, offline?

I'm developing an app with email capabilities and I'd like to send emails to myself, locally (as in, I could completely disconnect my development machine from the internet and still have these test emails send and receive, just on my computer).

I imagine the Rails app sending to Mac OS X's built-in Linux CLI mail program, but I've no idea how to set this up.

I want to do this so I can send an unlimited number of test emails to myself without worrying about locking myself out of a GMail account or burning up free credit on Sendgrid, et al., or waiting for the message to make a round-trip to some server, etc.

Anyone help me with this?

Upvotes: 3

Views: 3181

Answers (3)

Alan Peabody
Alan Peabody

Reputation: 3557

While I don't run OS X myself I do work with OS Xers and we all use sendmail in development. All you need to do is configure it just for your development environment.

In config/environments/development.rb:

AppName::Application.configure do

  # …

  config.action_mailer.delivery_method = :sendmail
  config.action_mailer.sendmail_settings = {
    :location => '/usr/sbin/sendmail',
    :arguments => '-i -t'
  }

end

Then in your mailer you can add a private method to determine who to email to if you are worried about accidentally emailing users/random email addresses:

class UserMailer < ActionMailer
  default :from => '[email protected]'

  def welcome(user)
    @user = user
    mail(
      :subject => "Hello World",
      :to => recipient(@user.email)
    )
  end

private

  def recipient(email_address)
    return '[email protected]' if Rails.env.development?
    email_address
  end

end

Upvotes: 2

jwilsco
jwilsco

Reputation: 408

Use MailCatcher. It's a gem that runs on a local server (localhost:1080) and displays outgoing emails from a Rails app in a browser-rendered mock email client.

$ gem install mailcatcher
$ mailcatcher
  1. Go to http://localhost:1080/
  2. Send mail through smtp://localhost:1025

Upvotes: 4

neezer
neezer

Reputation: 20560

This is it! MockSMTP (for OS X, at least)


UPDATE: This is arguably even better: MailCatcher. As it is Ruby/Web powered, it's platform-agnostic, and doesn't require paying for a license for desktop software. Also, if you use it with Google Chrome, it employs WebSockets for live-updating when a new message arrives! Cool!

Upvotes: 11

Related Questions