Docker container can't send mail using phpmailer

Dear docker forum,

I’m relatively new to docker so maybe I’m doing something stupid but what I’m trying to do is the following.
I have written some PHP code to send some emails from within my own written system.
The code works perfectly when running on my local webserver (apache2, php and mysql-server) but as soon as I put it in docker with the following Dockerfile it won’t send any email.

FROM ubuntu:18.04
LABEL Maintainer="Jesse Mulder"
EXPOSE 80/tcp

# Base setup
RUN apt-get update && apt-get upgrade -y

# Locales setup
RUN apt-get install locales locales-all -y && locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

RUN apt-get update && \
 export DEBIAN_FRONTEND=noninteractive && \
 apt-get install apache2 libapache2-mod-php php-mysql mysql-client libssl-dev -y


#Website toevoegen
### Add the website
ADD --chown=root:www-data sources/html /var/www/html
RUN chown -R root:www-data /var/www/html/
RUN chmod -R 774 /var/www/html/*

RUN apt-get autoclean -y && \
    apt-get autoremove -y && \
    rm -rf /tmp/*

RUN echo "extension=openssl" >> /etc/php/7.2/apache2/php.ini

WORKDIR /root
ADD --chown=root:root entrypoint.sh /opt/run/entrypoint.sh
RUN chmod 700 /opt/run/entrypoint.sh
ENTRYPOINT /opt/run/entrypoint.sh

The entrypoint.sh contains only a tail -f on the apache2 access log file. So the container stays running.
I’m using phpmailer to handle the emails for me, but when running in this docker it gives me the following error. (using gmail as smtp server)

2019-05-03 06:55:23 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP o47sm370336edc.37 - gsmtp
2019-05-03 06:55:23 CLIENT -> SERVER: EHLO pvb-mail
2019-05-03 06:55:23 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [xxx.xxx.xxx.xxx]
                                      250-SIZE 35882577
                                      250-8BITMIME
                                      250-STARTTLS
                                      250-ENHANCEDSTATUSCODES
                                      250-PIPELINING
                                      250-CHUNKING
                                      250 SMTPUTF8
2019-05-03 06:55:23 CLIENT -> SERVER: STARTTLS
2019-05-03 06:55:23 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2019-05-03 06:55:23 CLIENT -> SERVER: QUIT
2019-05-03 06:55:23
2019-05-03 06:55:23
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Does anyone know a solution to this?
Thanks in advance!

Kind regards,
Jesse Mulder

Edits

  • Added libssl-dev package
  • Added extension=openssl to php.ini for apache2

Hi

So by the look of it, it seems like it can connect to gmai, but as soon you fire the starttls it dies, so my thought, is that you’re missing the openssl lib for this to work

I added the libssl-dev package, but I still get the same error.
Also found a “extension=openssl” in the /etc/php/7.2/apache2/php.ini file uncommented that one but still nothing.

On that link, that phpmailer provided, it says this:

Check you have the openssl extension

To use any kind of encryption you need the openssl PHP extension enabled. If you don’t have it installed, or it’s misconfigured, you’re likely to have trouble at the STARTTLS phase of connections. Check this by looking at the output of phpinfo() or php -i (look for an ‘openssl’ section), or openssl listed in the output of php -m , or run this line of code:

<?php echo (extension_loaded('openssl')?'SSL loaded':'SSL not loaded')."\n"; ?>

Can you please verify this?

Checked the output of the php code and it says: SSL loaded
Tried testssl.sh from within the contianer but no fault’s detected.
Also did a phpinfo() run and found nothing but I attach a screenshot of the openssl bit to be sure.

Screenshot