How to make sending mail from phoenix application through local postfix install work

Hello everyone, I can’t make my phoenix application send emails using postfix installed on the same VPS.
The result of command
postconf -n
looks like this:

alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
append_dot_mydomain = no
biff = no
compatibility_level = 2
inet_interfaces = loopback-only
inet_protocols = all
mailbox_size_limit = 0
milter_default_action = accept
milter_protocol = 6
mydestination =
myhostname =
mynetworks = [::ffff:]/104 [::1]/128
myorigin = /etc/mailname
non_smtpd_milters = $smtpd_milters
readme_directory = no
recipient_delimiter = +
relayhost =
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
smtpd_milters = local:/opendkim/opendkim.sock
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
smtpd_tls_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file = /etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtpd_use_tls = yes
virtual_alias_maps = hash:/etc/postfix/virtual

When I give the command to send email in the console like this:
echo "This is the body of the email" | mail -s "This is the subject"
I get the email in my mailbox, everything works as expected, I also see the postfix work by looking into it’s log:
sudo tail -f /var/log/mail.log

May 28 16:14:31 mytestdomain postfix/pickup[28788]: 6E8445DC28: uid=1000 from=<>
May 28 16:14:31 mytestdomain postfix/cleanup[30829]: 6E8445DC28: message-id=<>
May 28 16:14:31 mytestdomain postfix/qmgr[6445]: 6E8445DC28: from=<>, size=399, nrcpt=1 (queue active)
May 28 16:14:31 mytestdomain postfix/smtp[30831]: connect to[2a00:1450:400c:c00::1a]:25: Network is unreachable
May 28 16:14:31 mytestdomain postfix/smtp[30831]: 6E8445DC28: to=<>,[]:25, delay=0.43, delays=0.02/0.01/0.17/0.24, dsn=2.0.0, status=sent (250 2.0.0 OK  1559060071 n7si2302256wmc.53 - gsmtp)
May 28 16:14:31 mytestdomain postfix/qmgr[6445]: 6E8445DC28: removed

But emails from my phoenix application are not sent, there is nothing in the postfix log and nothing in my mailbox. My config looks like this:

# Configures the mailer
config :jp, JpWeb.Mailer,
  adapter: Swoosh.Adapters.SMTP,
  relay: "",
  ssl: false,
  tls: :always,
  port: 1025,
  retries: 2,
  no_mx_lookup: false

I tried with ssl: true first but then I found the topic on this forum where it was recommended to set it to false, so I did it but it didn’t help. Also I tried to set port to 25 but with no result.
The email is sent like this:
JpWeb.SpecialistRequestEmail.specialist_request_email(id, url, email, phone, name) |> JpWeb.Mailer.deliver
and the function specialist_request_email looks like this:

defmodule JpWeb.SpecialistRequestEmail do
  import Swoosh.Email

  def specialist_request_email(specialist_id, url, email, phone, name) do
    |> to({"MyTestDomain administrator", ""})
    |> from({"Notifications system", ""})
    |> subject("New specialist request")
    |> html_body("<h1>Specialist request details</h1><div>Name: #{name}</div><div>Phone: #{phone}</div><div>E-mail: #{email}</div><div><a href=\"#{url}\">Specialist ##{specialist_id}#</a></div>")
    |> text_body("Specialist request details\nName: #{name}, Phone: #{phone}, E-mail: #{email}, Specialist Id: #{specialist_id}, Link: #{url}")

In my mix.exs file I added dependencies:

      {:swoosh, "~> 0.23"},
      {:gen_smtp, "~> 0.13"},

And added them to extra_applications (should I?):

  def application do
      mod: {Jp.Application, []},
      extra_applications: [:swoosh, :gen_smtp]

What do I miss?

what following

JpWeb.SpecialistRequestEmail.specialist_request_email(id, url, email, phone, name)
|> JpWeb.Mailer.deliver
|> IO.inspect


{:error, {:retries_exceeded, {:network_failure, ‘’, {:error, :econnrefused}}}}

The problem is not with elixir or swoosh. You need to configure your infrastructure correctly.

Firstly, make sure you have a correct DNS resolution: nslookup, secondary check that you have opened port 1025 on that IP address - telnet 1025



Non-authoritative answer:

telnet 1025
telnet: Unable to connect to remote host: Connection refused
telnet 25
telnet: Unable to connect to remote host: Connection refused

I have ufw installed and it’s status:

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)

All this postfix configuration is new to me, can you tell me what port is it listening 1025 or 25 (or other)? There is no such line in the /etc/postfix/ usually is your local computer, connected via the loopback interface. Are you sure you have started and configured an SMTP deamon?

Does something listen on port 1025? netstat -lnpt should shed some light.

netstat -lnpt
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0    *               LISTEN      -
tcp        0      0  *               LISTEN      1372/epmd
tcp        0      0 *               LISTEN      -
tcp        0      0    *               LISTEN      -
tcp        0      0 *               LISTEN      32189/
tcp        0      0*               LISTEN      -
tcp        0      0  *               LISTEN      -
tcp        0      0   *               LISTEN      -
tcp6       0      0 :::80                   :::*                    LISTEN      -
tcp6       0      0 :::4369                 :::*                    LISTEN      1372/epmd
tcp6       0      0 :::22                   :::*                    LISTEN      -
tcp6       0      0 ::1:5432                :::*                    LISTEN      -
tcp6       0      0 ::1:25                  :::*                    LISTEN      -
tcp6       0      0 :::4000                 :::*                    LISTEN      32189/

try to change your config to:

config :jp, JpWeb.Mailer,
  adapter: Swoosh.Adapters.SMTP,
  relay: "",
  ssl: false,
  tls: :always,
  port: 25, # <-----
  retries: 2,
  no_mx_lookup: false

Also you need to allow loopback in ufw - I don’t know how to do it in ufw. Something like this.

UPD Or just disable it for testing for a while

It’s bound ::1, not to, it won’t accept connections to the latter, target IP has to be corrected as well then.

Ah, yes - looks like DNS gives incorrect - it should resolve to

@igor give as output of cat /etc/hosts

Or if you configure zone on this server - correct the IP to

 sudo cat /etc/hosts    localhost

# The following lines are desirable for IPv6 capable hosts
::1          localhost ip6-localhost ip6-loopback
ff02::1      ip6-allnodes
ff02::2      ip6-allrouters

it seems there is at least one error…

change this to, change port to 25 in config and temporary disable ufw. You should have telnet 25 to be connected after that and then try to run elixir again

So I’ve changed /etc/hosts to correct IP
Added a rule for the ufw to allow 25 port: sudo ufw allow 25/tcp
and in my application config I’ve set the port: 25

And the email was sent, I’ve got it in my mailbox (though in spam).
First of all big thank you, guys for your help!
I still have a couple of questions:
In the postfix’s log there is quietness, no mention of the sent email, why is that?
And the second question is it dangerous to open 25 port? According to lines in my postfix config:

mynetworks = [::ffff:]/104 [::1]/128
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination

Is it safe config?

You are always welcome!

regarding port 25 - looks good, but I recommend to ask this question on postfix forum ))

UPD You can leave this port without any restrictions if you disable any connection from outside to port 25 in ufw. Allow just tcp from-to

UPD 2 Of course, you also should allow outgoing connections to any address to port 25 if you want to send a real emails

1 Like

Well, the first question is not relevant, it’s just the ssh session was expired so the log was not updated.

Thanks, Alexey!
I’ve done:

sudo ufw delete allow 25/tcp
sudo ufw allow from to any port 25

Tried to send email and everything worked.

Just random question - why use Postfix at all?

@hauleth, as I said, I lack of experience in this, I’ve read that Postfix is the most popular smtp server for linux, is there a better software for linux you are pointing to?
I know about third party services but they are not free and what are the benefits of using them? Which one is better and why?

The most important reason is, that the IP of your host from which you send your emails is not “trusted”, and it will take a lot of time to build up necessary reputation to not get flagged as SPAM everywhere. This is especially true if you send a lot of mails at once, eg. Newsletters.

1 Like