Generating a sitemap.xml file on Heroku with Ruby on Rails

Here is a description of how to dynamically generate an xml sitemap every time it is accessed. If you have a larger site, you may want to generate the sitemap using a cron job, and publish it to a storage service like Amazon S3.

Step 1:
Remove /public/sitemap.xml if it exists.

Step 2:
Add the following to /config/routes.rb
get “sitemap.xml” => “sitemap#index”, as: “sitemap”, defaults: { format: “xml” }
Step 3:
Create /app/controllers/sitemap_controller.rb

 class SitemapController < ApplicationController
   def index
     @static_pages = [root_url, about_url, contact_url, help_url]
     @users = User.all
     @posts = Post.all
     respond_to do |format|

Step 4:
Create /app/views/sitemap/index.xml.builder with the following content. Note that this sitemap.xml includes not only the links to the pages in the site, but also the links to the photographs that are embedded in the links, and which are stored on Amazon S3.

  'xmlns'.to_sym => "",
  'xmlns:image'.to_sym => ""
) do
  @static_pages.each do |page|
    xml.url do
      xml.loc "#{page}"
  @users.each do |user|
    xml.url do
      xml.loc "#{user_url(user)}"
  @posts.each do |post|
    xml.url do
      xml.loc "#{post_url(post)}"
      xml.lastmod post.updated_at.strftime("%F")
        xml.image :image do
          xml.image :loc, "#{request.protocol}#{request.host_with_port}#{}"

Step 5:
Commit and upload to heroku.


Sending Email from Rails Using smtp on Heroku

A recent project we worked on used Ruby on Rails running on Heroku. The actual domain, including all email services, DNS services are hosted elsewhere. In order to get this working, I needed to figure out how to configure Heroku to send email through a remote SMTP server.

First of all configure the production server by adding the following lines to the project /config/environments/production.rb file. If you are going to be testing the SMTP mail functions on your development server you need to add the configuration information to /config/environments/development.rb as well.

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address: "",
  port: 25,
  authentication: "plain",
  user_name: "",
  password: ENV['SMTP_PASSWORD'],
  enable_starttls_auto: false
config.action_mailer.raise_delivery_errors = true

In your configuration replace address: ‘’ with your own SMTP server hostname, and replace the user_name: “” with the user name for a valid email account on your SMTP server.

Depending on your situation, you may also have different values for port:, authentication: and enable_starttls_auto:. This is what works for a basic SMTP host running plain authentication and no TLS encryption. This configuration is not designed to work with gmail.

Notice that the password to access your SMTP server is not specified here. It is generally a bad idea to store security credentials in a project file if that file is stored in a version control system like git. Anyone with access to that version control system will also get access to the production security information, which could be a big deal for some applications. So in this case the SMTP server password is stored in an environment variable called SMTP_PASSWORD. By setting this variable on the development and production machines, the mail configuration will be complete.

On MAC or Linux development systems, the environment variable can be set one of 2 ways.

Before running the development server (rails server), enter the following command on the command line, substituting the actual password for the word password in quotes.

export SMTP_PASSWORD='password'

Alternatively you can add the same export command to your ~/.bash_profile file.

You can verify that the password has been set by typing:


If you are running foreman as your development rails server, you can have foreman load the environment from a file named .env at the root of your project:


If you are running version control, you should make sure that the .env file is not stored in your repository. If you are using git make sure the following line is in the .gitignore file.


Once you have the mailer running on your development system using SMTP, you need to run one more command to make it work on heroku.

$ heroku config:add SMTP_PASSWORD='password'

This creates an environment variable on heroku that works the same as environment variables on your development system. At this point you can upload the new version of your project and you will have the ability to send mail using your existing SMTP server.