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..)

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New

Other popular topics Top

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 29377 241
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
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
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement