Skip to content
This repository was archived by the owner on Jan 25, 2019. It is now read-only.

Commit 9abb2f5

Browse files
committed
Use your own SMTP server for sending notifications.
1 parent 44b1440 commit 9abb2f5

File tree

4 files changed

+56
-144
lines changed

4 files changed

+56
-144
lines changed

docs/email-setup.md

+21-137
Original file line numberDiff line numberDiff line change
@@ -35,148 +35,32 @@ work, but usually it is worth doing it.
3535

3636
## Building a simple mailserver
3737

38-
We are going to use this docker based mailserver:
39-
https://github.com/tomav/docker-mailserver We don't need to check for
40-
spam or viruses, we just need a simple mailserver to send emails from
41-
our applications (in this case from Moodle).
38+
To install a simple SMTP server, follow the instructions here:
39+
https://github.com/docker-scripts/postfix/blob/master/INSTALL.md
40+
(or here: http://dashohoxha.fs.al/simple-smtp-server/).
4241

42+
You can set the SMTP settings with one of these alternatives:
4343

44-
- First create a directory for the mailserver and get the setup script:
44+
- Set **SMTP_SERVER** on `settings.sh`, like this:
4545
```
46-
mkdir -p /var/ds/mail.example.org
47-
cd /var/ds/mail.example.org/
48-
49-
curl -o setup.sh \
50-
https://raw.githubusercontent.com/tomav/docker-mailserver/master/setup.sh
51-
chmod a+x ./setup.sh
52-
```
53-
54-
- Create the file `docker-compose.yml` with a content like this:
46+
### You can build an SMTP server as described here:
47+
### https://github.com/docker-scripts/postfix/blob/master/INSTALL.md
48+
SMTP_SERVER=smtp.example.org
49+
SMTP_DOMAIN=example.org
5550
```
56-
version: '2'
5751

58-
services:
59-
mail:
60-
image: tvial/docker-mailserver:latest
61-
hostname: mail
62-
domainname: example.org
63-
container_name: mail
64-
ports:
65-
- "25:25"
66-
- "587:587"
67-
- "465:465"
68-
volumes:
69-
- ./data/:/var/mail/
70-
- ./state/:/var/mail-state/
71-
- ./config/:/tmp/docker-mailserver/
72-
- /var/ds/wsproxy/letsencrypt/:/etc/letsencrypt/
73-
environment:
74-
- PERMIT_DOCKER=network
75-
- SSL_TYPE=letsencrypt
76-
- ONE_DIR=1
77-
- DMS_DEBUG=1
78-
- SPOOF_PROTECTION=0
79-
- REPORT_RECIPIENT=1
80-
- ENABLE_SPAMASSASSIN=0
81-
- ENABLE_CLAMAV=0
82-
- ENABLE_FAIL2BAN=1
83-
- ENABLE_POSTGREY=0
84-
cap_add:
85-
- NET_ADMIN
86-
- SYS_PTRACE
52+
- From the command line:
8753
```
88-
89-
For more details about the environment variables that can be used,
90-
and their meaning and possible values, check also these:
91-
- https://github.com/tomav/docker-mailserver#environment-variables
92-
- https://github.com/tomav/docker-mailserver/blob/master/.env.dist
93-
94-
Make sure to set the propper `domainname` that you will use for the
95-
emails. We forward only SMTP ports (not POP3 and IMAP) because we
96-
are not interested in accessing the mailserver directly (from a
97-
client). We also use these settings:
98-
- `PERMIT_DOCKER=network` because we want to send emails from other
99-
docker containers.
100-
- `SSL_TYPE=letsencrypt` because we will manage SSL certificates
101-
with letsencrypt.
102-
103-
- We need to open these ports on the firewall: `25`, `587`, `465`
104-
```
105-
ufw allow 25
106-
ufw allow 587
107-
ufw allow 465
108-
```
109-
On your server you may have to do it differently.
110-
111-
- Pull the docker image:
54+
moosh config-set smtphosts smtp.example.org
55+
moosh config-set smtpsecure TLS
56+
moosh config-set smtpauthtype PLAIN
57+
moosh config-set smtpuser ''
58+
moosh config-set smtppass ''
59+
moosh config-set smtpmaxbulk 100
11260
```
113-
docker pull tvial/docker-mailserver:latest
114-
```
115-
116-
- Now generate the DKIM keys with `./setup.sh config dkim` and copy
117-
the content of the file `config/opendkim/keys/domain.tld/mail.txt`
118-
on the domain zone configuration at the DNS server. I use
119-
[bind9](https://github.com/docker-scripts/bind9) for managing my
120-
domains, so I just paste it on `example.org.db`:
121-
```
122-
mail._domainkey IN TXT ( "v=DKIM1; h=sha256; k=rsa; "
123-
"p=MIIBIjANBgkqhkiG9w0BAQEFACAQ8AMIIBCgKCAQEAaH5KuPYPSF3Ppkt466BDMAFGOA4mgqn4oPjZ5BbFlYA9l5jU3bgzRj3l6/Q1n5a9lQs5fNZ7A/HtY0aMvs3nGE4oi+LTejt1jblMhV/OfJyRCunQBIGp0s8G9kIUBzyKJpDayk2+KJSJt/lxL9Iiy0DE5hIv62ZPP6AaTdHBAsJosLFeAzuLFHQ6USyQRojefqFQtgYqWQ2JiZQ3"
124-
"iqq3bD/BVlwKRp5gH6TEYEmx8EBJUuDxrJhkWRUk2VDl1fqhVBy8A9O7Ah+85nMrlOHIFsTaYo9o6+cDJ6t1i6G1gu+bZD0d3/3bqGLPBQV9LyEL1Rona5V7TJBGg099NQkTz1IwIDAQAB" ) ; ----- DKIM key mail for example.org
125-
126-
```
127-
128-
- Add these configurations as well on the same file on the DNS server:
129-
```
130-
mail IN A 10.11.12.13
131-
132-
; mailservers for example.org
133-
3600 IN MX 1 mail.example.org.
134-
135-
; Add SPF record
136-
IN TXT "v=spf1 mx ~all"
137-
```
138-
Then don't forget to change the serial number and to restart the service.
139-
140-
- Get an SSL certificate from letsencrypt. I use
141-
[wsproxy](https://github.com/docker-scripts/wsproxy) for managing
142-
SSL letsencrypt certificates of my domains:
143-
```
144-
cd /var/ds/wsproxy
145-
ds domains-add mail mail.example.org
146-
ds get-ssl-cert myemail@gmail.com mail.example.org --test
147-
ds get-ssl-cert myemail@gmail.com mail.example.org
148-
```
149-
Now the certificates will be available on
150-
`/var/ds/wsproxy/letsencrypt/live/mail.example.org`.
151-
152-
- Start the mailserver and check for any errors:
153-
```
154-
apt install docker-compose
155-
docker-compose up mail
156-
```
157-
158-
- Create email accounts and aliases:
159-
```
160-
./setup.sh email add admin@example.org passwd123
161-
./setup.sh email add info@example.org passwd123
162-
./setup.sh alias add admin@example.org myemail@gmail.com
163-
./setup.sh alias add info@example.org myemail@gmail.com
164-
./setup.sh email list
165-
./setup.sh alias list
166-
```
167-
168-
Aliases make sure that any email that comes to these accounts is
169-
forwarded to my real email address, so that I don't need to use
170-
POP3/IMAP in order to get these messages. Also no anti-spam and
171-
anti-virus software is needed, making the mailserver lighter.
172-
173-
- Send some test emails to these addreses and make other tests. Then
174-
stop the container with `Ctrl+c` and start it again as a daemon:
175-
`docker-compose up mail -d`.
17661

177-
- Now save on Moodle configuration the SMTP settings and test by
178-
trying to send some messages to other users:
179-
- **SMTP hosts**: `mail.example.org:465`
180-
- **SMTP security**: `SSL`
181-
- **SMTP username**: `info@example.org`
182-
- **SMTP password**: `passwd123`
62+
- From the admin interface (as described on the previous section):
63+
- **SMTP hosts**: `smtp.example.org`
64+
- **SMTP security**: `TLS`
65+
- **SMTP Auth Type**: `PLAIN`
66+
Leave empty username and password.

scripts/cfg/05_moodle_install.sh

+12-1
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,25 @@ git pull
2222
git checkout $MOODLE_BRANCH
2323

2424
### set some configuration defaults
25-
cat <<_EOF > local/defaults.php
25+
if [[ -n $SMTP_SERVER ]]; then
26+
cat <<_EOF > local/defaults.php
27+
<?php
28+
\$defaults['moodle']['smtphosts'] = '$SMTP_SERVER';
29+
\$defaults['moodle']['smtpsecure'] = 'TLS';
30+
\$defaults['moodle']['smtpauthtype'] = 'PLAIN';
31+
\$defaults['moodle']['smtpuser'] = '';
32+
\$defaults['moodle']['smtppass'] = '';
33+
_EOF
34+
elif [[ -n $GMAIL_ADDRESS ]]; then
35+
cat <<_EOF > local/defaults.php
2636
<?php
2737
\$defaults['moodle']['smtphosts'] = 'smtp.gmail.com:465';
2838
\$defaults['moodle']['smtpsecure'] = 'ssl';
2939
\$defaults['moodle']['smtpauthtype'] = 'LOGIN';
3040
\$defaults['moodle']['smtpuser'] = '$GMAIL_ADDRESS';
3141
\$defaults['moodle']['smtppass'] = '$GMAIL_PASSWD';
3242
_EOF
43+
fi
3344

3445
### fix ownership
3546
chown -R www-data: /var/www

scripts/cfg/06_moodle_config.sh

+13-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@ $moosh config-set theme more # set theme
1313
$moosh config-set registerauth email
1414

1515
### set smtp settings
16-
$moosh config-set smtphosts 'smtp.gmail.com:465'
17-
$moosh config-set smtpsecure ssl
18-
$moosh config-set smtpauthtype LOGIN
19-
$moosh config-set smtpuser $GMAIL_ADDRESS
20-
$moosh config-set smtppass "$GMAIL_PASSWD"
16+
if [[ -n $SMTP_SERVER ]]; then
17+
$moosh config-set smtphosts $SMTP_SERVER
18+
$moosh config-set smtpsecure TLS
19+
$moosh config-set smtpauthtype PLAIN
20+
$moosh config-set smtpuser ''
21+
$moosh config-set smtppass ''
22+
elif [[ -n $GMAIL_ADDRESS ]]; then
23+
$moosh config-set smtphosts 'smtp.gmail.com:465'
24+
$moosh config-set smtpsecure SSL
25+
$moosh config-set smtpauthtype LOGIN
26+
$moosh config-set smtpuser $GMAIL_ADDRESS
27+
$moosh config-set smtppass "$GMAIL_PASSWD"
28+
fi
2129

2230
# Maximum number of messages sent per SMTP session
2331
# Grouping messages may speed up the sending of emails.

settings.sh

+10-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ DBNAME=moodle
1313
DBUSER=moodle
1414
DBPASS=moodle
1515

16-
### Gmail account for notifications. This will be used by ssmtp.
16+
### You can build an SMTP server as described here:
17+
### https://github.com/docker-scripts/postfix/blob/master/INSTALL.md
18+
### Comment out if you don't have a SMTP server and want to use
19+
### a gmail account, as described below.
20+
SMTP_SERVER=smtp.example.org
21+
SMTP_DOMAIN=example.org
22+
23+
### Gmail account for notifications. Use this as a simple alternative to building
24+
### your own SMTP server. However this has some limitations, as described here:
25+
### https://github.com/docker-scripts/moodle/blob/master/docs/email-setup.md
1726
### You need to create an application specific password for your account:
1827
### https://www.lifewire.com/get-a-password-to-access-gmail-by-pop-imap-2-1171882
1928
GMAIL_ADDRESS=

0 commit comments

Comments
 (0)