6

I want to send mail from my Alpine-PHP-Fpm container using my host postfix installation.

RECAP

PHP-Fpm Container -> Sendmail -> PostFix on host -> Sending via SMTP

But I get

sendmail: can't connect to remote host (127.0.0.1): Connection refused

Here is the Postfix config:

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
append_dot_mydomain = no
readme_directory = no

smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = MYDOMAIN.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = localhost.com, localhost

# SMTP authentication settings
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:no-reply@MYDOMAIN.com:SECRETPSW
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = may
#header_size_limit = 4096000
relay_destination_concurrency_limit = 20

# Limit DOS Attacks
default_process_limit = 100
smtpd_client_connection_count_limit = 10
smtpd_client_connection_rate_limit = 30
queue_minfree = 20971520
header_size_limit = 51200
message_size_limit = 10485760
smtpd_recipient_limit = 100

# Default relayhost setting
relayhost = smtp.MYDOMAIN.com

mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 172.18.0.3 # Fpm container IP
mailbox_command = procmail -a "$EXTENSION"
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = 172.18.0.1 # Docker0 bridge IP
inet_protocols = all

And here is the php.ini mail section:

[mail function]
SMTP = localhost
smtp_port = 25
sendmail_path = "/usr/sbin/sendmail -t -i"
mail.add_x_header = On

Are there any ports that I need to open in my docker-compose file or in the host firewall settings?

peterh
  • 11,875
  • 18
  • 85
  • 108
javal88
  • 1,188
  • 3
  • 17
  • 29

3 Answers3

3

I'm not sure about using the host's config/postfix however I can suggest an alternative solution that may work just as well. Add a postfix container and copy the configs from the host to the container:

services:
  application:
    image: some-company/some-application
    environment:
      - APPLICATION_ENV
    ports:
      - 80:80

  smtp:
    image: namshi/smtp
    environment:
      - MAILNAME
      - SMARTHOST_USER
      - SMARTHOST_PASSWORD
    volumes:
      - /etc/postfix/main.cf:/etc/postfix/main.cf
    ports:
      - 25:25

Then you can use smtp as your mailer host / transport and the user/pass from the environment.

OK sure
  • 2,617
  • 16
  • 29
  • So in my php.ini config i've to change the SMTP voice from localhost to smtp? – javal88 Aug 21 '17 at 14:20
  • yep - the docker network should handle resolving the smtp host from that. – OK sure Aug 21 '17 at 14:24
  • Also, if you have `localhost` in your container, it will treat that as the container and not the host - I think you would need to use DNS to resolve to the host otherwise. This route keeps the whole application more portable. The other option is to install postfix on the container and copy the postfix config in but a separate container will give you greater control and portability IMO. – OK sure Aug 21 '17 at 14:26
  • Actually i switched from localhost to smtp and nothing changed, it seems that php tries to connect to 127.0.0.1 instead of the right address. – javal88 Aug 22 '17 at 07:41
  • Are you sure you changed this in the container and restarted NGINX etc? 127.0.0.1 would still be localhost I think. – OK sure Aug 22 '17 at 08:23
  • 2
    Problem found! It was a known issue with sendmail function and Alpine Linux, i switched from sendmail to ssmtp and now it works like a charm! Thank you! – javal88 Aug 22 '17 at 10:56
0

I am not a container expert, but if I understood correctly, you are trying to acccess 127.0.0.1 within the container (php.ini localhost config resolves to 127.0.0.1), and I assume that is not the hosts address, it points to the container internals.

Figure out the host IP and change in the php.ini:

[mail function]
SMTP = host_address
smtp_port = 25
Fiber
  • 96
  • 1
  • 4
  • I've tried your solution changing the config with the right address (the docker bridge) but i get connection refused. Tried telnet-ing it and get connection refused too, i think it's a firewall issue. Thanks – javal88 Aug 21 '17 at 14:29
  • 5
    The SMTP setting is used ONLY on windows hosts, according to the PHP docs http://php.net/manual/en/mail.configuration.php it's useless for linux machines. – Rainer Feike Oct 14 '17 at 08:11
0

The best solution I have found is to install msmtp and add the following to the php.ini file.

sendmail_path = /usr/bin/msmtp -t  --host host.docker.internal --from php@docker

Now your mail will be received by your mail server (smpt4dev or mailh)

Handsome Nerd
  • 17,114
  • 22
  • 95
  • 173