How to enable port 25 on my Ubuntu server?

December 1, 2015 11.9k views
Email Ruby on Rails Ubuntu

So I am running Spree e-commerce in Rails. When I click on 'place order' after I have added items to my cart and proceed to checkout i got this error. (same question as this person)

Errno::ECONNREFUSED in Spree::CheckoutController#update
Connection refused - connect(2) for "localhost" port 25

I believe it has something to do with the email but I am not 100 percent sure. As for spree is defaulted to port 25 as referred to here

I checked my port using

telnet mydroplet'sIPaddress 25

and it said (as expected)

telnet: Unable to connect to remote host: Connection refused

I tried a solution to unblock my port 25 by adding the line below as suggested in another question here.

iptables -I INPUT -p tcp -m tcp --dport 25 -j ACCEPT

and recheck using the same method and still no good.

I even went to setup my firewall using ufw and made sure my port 25 is available by following this tutorial

sudo ufw allow 25/tcp

still no good...


How do I enable my port 25?

I am running Ubuntu Ruby on Rails on 14.04 (Postgres, Nginx, Unicorn).

My IP address is if anyone wants to try my port 25.

2 Answers

You'll probably want to install either sendmail or postfix. Probably postfix.

  • I found the solution by installing Postfix and reconfiguring Rails.

    Here is what I did ..

    step 1 -> follow this tutorial on how to install and configure postfix

    step 2 -> when editing the postfix config in /etc/postfix/ make hostname = localhost

    here is my working setting (I have to set it to 'localhost' because the default for Spree's mail configuration is 'localhost' you can change Spree also but just have to make sure they match one another.)

    myhostname = localhost
    alias_maps = hash:/etc/aliases
    alias_database = hash:/etc/aliases
    mydestination = APPNAME, localhost.localdomain, , localhost
    relayhost =
    mynetworks = [::ffff:]/104 [::1]/128
    mailbox_size_limit = 0
    recipient_delimiter = +
    inet_interfaces = localhost
    inet_protocols = all

    step 3 -> Reconfigured Rails application.rb in config folder of Rails by adding this line (i found the solution here)

    config.action_mailer.smtp_settings = { enable_starttls_auto: false  }

    Here is what my application.rb looks like..

    require File.expand_path('../boot', __FILE__)
    require 'rails/all'
    # Require the gems listed in Gemfile, including any gems
    # you've limited to :test, :development, or :production.
    module APPNAME
      class Application < Rails::Application
        config.to_prepare do
          # Load application's model / class decorators
          Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
            Rails.configuration.cache_classes ? require(c) : load(c)
          # Load application's view overrides
          Dir.glob(File.join(File.dirname(__FILE__), "../app/overrides/*.rb")) do |c|
            Rails.configuration.cache_classes ? require(c) : load(c)
        config.action_mailer.smtp_settings = { enable_starttls_auto: false  }
        # Settings in config/environments/* take precedence over those specified here.
        # Application configuration should go into files in config/initializers
        # -- all .rb files in that directory are automatically loaded.
        # Set default to the specified zone and make Active Record auto-convert to this zone.
        # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
        # config.time_zone = 'Central Time (US & Canada)'
        # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
        # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
        # config.i18n.default_locale = :de
        # Do not swallow errors in after_commit/after_rollback callbacks.
        config.active_record.raise_in_transactional_callbacks = true

    Thanks @kode54

    Postfix is an MTA (Mail Transfer Agent), an application used to send and receive email. In this tutorial, we will install and configure Postfix so that it can be used to send emails by local applications only.


Like many providers, DigitalOcean blocks Port 25 by default, so you would need to configure another port by which to send e-mail (2525 is a common alternative). You would need to configure your app to send mail through this (new )port and ensure that either your mail server or upstream mail server is capable of serving on this port as well (and, of course, open it on the firewall).

Have another answer? Share your knowledge.