Heroku

How It Works

Sending Email from Your App

Last Updated: 07 February 2012

email ruby

Table of Contents

Heroku does not provide an outgoing mail service but does allow the use of an external SMTP server. For production use, we recommend the Sendgrid add-on which lets you scale from very light requirements to thousands of emails per day with no change in configuration.

Sending Email from Rails

If you’re using the Sendgrid add-on, you do not need* to configure your SMTP connection. You can skip straight to Step #3.

Here’s a quick guide to setting up a Rails app to send email with an external SMTP provider.

1. Set the delivery method

In config/environment.rb set ActionMailer’s delivery method to SMTP:

config.action_mailer.delivery_method = :smtp

2. Configure your SMTP connection

ActionMailer::Base.smtp_settings = {
  :address  => "smtp.someserver.net",
  :port  => 25,
  :user_name  => "someone@someserver.net",
  :password  => "mypass",
  :authentication  => :login
}

You’ll want to make sure Rails raises errors on mail delivery, so turn that on as well:

config.action_mailer.raise_delivery_errors = true

If you would like to use Gmail as your SMTP server, there are a few additional configuration steps required, as outlined in this blog post.

3. Build a Mailer class

Start by creating a mailer class:

$ ./script/generate mailer <name>

Where <name> is the class name (e.g., Notifier). Rails mailers are somewhat similar to controllers in that both have one method for each action and an accompanying template under views/<class_name>. For mailers, each method is the definition of an email message: who is the recipient, what’s the subject, etc. The email body is contained in the view.

Here’s an example:

class UserMailer < ActionMailer::Base
  def signup_notification(user)
    recipients "#{user.name} <#{user.email}>"
    from       "My Forum "
    subject    "Please activate your new account"
    sent_on    Time.now
    body       { :user => user, :url => activate_url(user.activation_code, :host => user.site.host }
  end
end

And the view in app/views/user_mailer/signup_notification.rhtml looks like this:

Your account has been created.

Username: 
Password: 
Visit this url to activate your account:

If you’re using Rails 3.0 or higher, you can call the mailer method directly, without the “deliver_” prefix.

Now you are ready to send emails. You can manually send one from the console to make sure it’s working:

$ heroku console
>>> user = User.new(:name => 'Me', :email => 'me@gmail.com', :login => 'me', :password => '1234')
>>> UserMailer.deliver_signup_notification(user)

Check the output of heroku logs to confirm that the email was sent.

Once you’ve verified it as working, you’ll probably want to hook it into your model:

after_create :deliver_signup_notification

def deliver_signup_notification
  UserMailer.deliver_signup_notification(self)
end

Sending Email with Gmail

For limited email needs, Gmail can be a good choice. You can send up to 500 messages a day. The from address will ALWAYS be set to the account email address. This blog post outlines the steps necessary to send email through Gmail. Follow the instructions in the blog post, then start at step #1 above to configure Rails to use Gmail.