Thursday 5 January 2017














Docker: How to send emails from WordPress?

Following numerous incidents resulting in the blog taken offline for several hours, the blog is now running on Docker . For this we can thank, among others, Dominique for his articles on Docker it
After my transition, several problems arose, one of them being  unable to send emails to WordPress .
This is what allows me to receive messages from the  as well as various notifications. In the immediate future I could have done without, replacing the form by a link to my PGP key and my mail. Except that I have several ideas in mind including a possible newsletter.
Not wishing to inflict on me a technical debt that I should have had to repay, I needed a solution.
Not wishing to either use an external SMTP type Gmail, following the logic , so I started looking for a way to implement a sending email directly from the server.
All  without using a WordPress plugin , why make it simple when you can make complicated?
And as usual, if I speak to you about my problem, it is because I have at least a trick or a hack to solve it. Let's look at it all ready!
Note that here I use Docker on a server where sendmail is functional.

 Adding sendmail to the WordPress Dockerfile

Sendmail is an SMTP (Simple Mail Transfer Protocol),  Mailutils is it a utility includes a set of utilities, programs and libraries for processing emails.
We start by modifying the Dockerfile of WordPress to add them to our installation.
How do we do ? I never did that myself!
No worries, it takes a first time to everything! Here's how:
  • Download WordPress sources
cd /tmp
git clone https://github.com/docker-library/wordpress.git .
  • Open the Dockerfile:
cd fpm
nano Dockerfile
  • Add sendmail and mailutils to the first block
# install the PHP extensions we need RUN apt-get update && apt-get install -y libpng12-dev libjpeg-dev sendmail sendmail-bin mailutils && rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr \
&& docker-php-ext-install gd mysqli opcache
If you use nano like me then make CTRL + O then Enter to save then CTRL + X to close the editor.
  • To finish rebuild the image:
docker build -t wordpress-sendmail .

What have we done?

We just downloaded the file for building the image docker WordPress which we added our packages . Thus we create an exact copy of the official image with our packages in addition!

 Php Configuration

We will now create a php.ini to inform him some parameters, move to the folder on your dock-compose.yml or where other files that you link to your containers located (nginx in my case) and create a file php.ini .
So it will be ./etc/php.ini
Inside the latter insert the following lines:
sendmail_path = /usr/sbin/sendmail -t -i
SMTP = localhost
smtp_port = 25
All we have to do is tie it to our container!

 HOSTS file modification script

For sendmail to work, it must be assigned an FQN. For this we will create an email.sh file containing the following script:
#!/bin/bash
line=$(head -n 1 /etc/hosts)
line2=$(echo $line | awk '{print $2}')
echo "$line $line2.localdomain" >> /etc/hosts
service sendmail restart

 Starting the WordPress container

Note that you should also add our script to your container, so you can either add it to the Dockerfile, or put it in a shared folder with the host, which I chose to do.
Why not run it from our Dockerfile? Because the script uses the ID of your container, this one changing at each "layer" we are obliged to execute it once our container launched.
Using docker-composing my WordPress block looks like the end of it (note the appearance of the line pointing to our php.ini file):
wordpress:
image: wordpress-qsypoq
restart: always
links:
- mysql:mysql
volumes:
- ./wp:/var/www/html
- ./etc/php.ini:/usr/local/etc/php/php.ini
environment:
WORDPRESS_DB_NAME: ma_bdd
WORDPRESS_TABLE_PREFIX: mon_prefix
If you are unsure of the location of the php.ini file in your container, run it first, then:
docker exec -i ID_DE_VOTRE_CONTAINER php --ini
So you will retrieve the locations of your various folders and php configuration files.
Once our containers have been started and our script included, retrieve the ID of your container:
docker ps
Then run your script:
docker exec -i ID_DE_VOTRE_CONTAINER bash chemin/vers/votre/fichier/email.sh
Note that if in the future you rebuild your container you will have to run again the script so that WordPress can continue to send emails!

 Edit the sending of mail via WordPress

Last step, we will modify the file /wp-includes/class-phpmailer.php!
The lines to be modified are as follows:
public $From = 'root@votrevps';
public $Sender = 'root@votrevps';
public $Mailer = 'smtp';
All you have to do is to fill in the email address of your local user in the first 2 lines and you are good! Smiley

No comments:

Post a Comment

User Interface(UI) for Docker, Portainer

Portainer gives you access to a central overview of your Docker host or Swarm cluster. From the dashboard, you can easily access any manag...