How to set up sending emails in docker


I would like to send emails from my nextcloud that is installed in a docker container with the base version for docker-compose from the Readme:

How do I configure nextcloud now, that it can send out emails via my existing email server?

Nextcloud version (eg, 12.0.2): 13.5
Operating system and version (eg, Debian 9 Docker):
Apache or nginx version (eg, Apache 2.4.25): Apache 2.4
PHP version (eg, 7.1): PHP 7


What I did until now:

I changed docker-compose.yml the line

image: nextcloud


      context: .
      dockerfile: Dockerfile

I created a Dockerfile file in my docker-compose folder with the following content:

FROM nextcloud

RUN set -ex; \
  apt-get update \
  && apt-get install -y ssmtp  \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/?*

COPY ssmtp.conf /etc/ssmtp/ssmtp.conf

and the ssmtp.conf to be copied with the extra line (to point to my email-VM on the same host):


now inside the docker image, i can send out emails:

docker exec nextcloud_app_1 echo -e "Subject: Sent from docker\n\nHere the body" | ssmtp my@email

but still, in nextcloud, I get the error:

nextcloud Sharing testfolder failed, could not find maybe the server is currently unreachable or uses a self-signed certificate.

I also found the E-Mail Configuration in “Additional Settings” but there I tried all three options “PHP”, “SMTP” and “Sendmail” without success. Not even the smtp settings worked, although the credentials are all tested and work fine in thunderbird. I get this error:

A problem occurred while sending the email. Please revise your settings. (Error: Connection could not be established with host [Connection refused #111])

I tried adding to /etc/hosts but then I get the error:

stream_socket_enable_crypto(): Peer certificate' did not match expected CN=’ at /var/www/html/3rdparty/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php#94



add the host of the email-vm to /etc/hosts:

but don’t change the “Server address” in the email settings in Nextcloud, that one goes still

This will ommit the smtp-traffic going out through the internet.