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

to

build: 
      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):

mailhub=10.77.77.101

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 here_my_email@gmail.com 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 myserver.de [Connection refused #111])

I tried adding 10.77.77.101 myserver.de to /etc/hosts but then I get the error:

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

Solution:

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

10.77.77.101 myserver.de

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

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

note, that ssmtpd is discontinued, use msmtp instead. (example: https://unix.stackexchange.com/a/527937/20661)