igor

igor

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 = mytestdomain.com
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/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" mymailaddress@gmail.com
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=<testuser@mytestdomain.com>
May 28 16:14:31 mytestdomain postfix/cleanup[30829]: 6E8445DC28: message-id=<20190528161431.6E8445DC28@mytestdomain.eu>
May 28 16:14:31 mytestdomain postfix/qmgr[6445]: 6E8445DC28: from=<testuser@mytestdomain.com>, size=399, nrcpt=1 (queue active)
May 28 16:14:31 mytestdomain postfix/smtp[30831]: connect to gmail-smtp-in.l.google.com[2a00:1450:400c:c00::1a]:25: Network is unreachable
May 28 16:14:31 mytestdomain postfix/smtp[30831]: 6E8445DC28: to=<testuser@gmail.com>, relay=gmail-smtp-in.l.google.com[64.233.184.24]: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: "mytestdomain.com",
  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
    new()
    |> to({"MyTestDomain administrator", "testuser@gmail.com"})
    |> from({"Notifications system", "notifications@mytestdomain.com"})
    |> 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}")
  end
end

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]
    ]
  end

What do I miss?

Most Liked

alexiss

alexiss

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 127.0.0.0/24

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

NobbZ

NobbZ

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.

outlog

outlog

you should definitely look into SPF, DKIM, and DMARC…

send a mail to mailtest@unlocktheinbox.com and you’ll get a report back (warning lot of upsell in that report but should get u going…)

igor

igor

I’ve already configured SPF, DKIM and DMARC, and tested sending an email to some testing site where I’ve got 10 points out of 10. But nevertheless gmail puts my test emails into the spam folder.

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement