Skip to content

Commit

Permalink
mailserver
Browse files Browse the repository at this point in the history
  • Loading branch information
tsengyushiang authored Oct 30, 2024
1 parent 67b520e commit a675d6c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Docker

- [commands](./commands.md)
- [mailserver](./mailserver.md)
- [nginx](./nginx.md)
- [ngrok](./ngrok.md)
- [sonarqube](./sonarqube.md)
Expand Down
79 changes: 79 additions & 0 deletions docker/mailserver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Mailserver

- [reference](https://i12bretro.github.io/tutorials/0779.html)

# Prerequisite

- Domain name and static IP (EC2)
- Docker compose
- [Host Port Forwarding](https://docker-mailserver.github.io/docker-mailserver/latest/config/security/understanding-the-ports/#overview-of-email-ports)

# Quick Setup

- Set `DOMAIN_NAME` to system env by command:

```bash
DOCKER_MAILSERVER_DOMAIN_NAME=ec2-111-111-111-11.ap.aws.com
```

- Create main working directories

```bash
mkdir ~/docker/mailserver/{data,state,logs,config} -p
```

- Run `docker-compose up -d` with `docker-compose.yml`

```yaml
docker run -d --name=mailserver --hostname="$HOSTNAME" --domainname="$DOCKER_MAILSERVER_DOMAIN_NAME" -p 25:25 -p 143:143 -p 587:587 -p 993:993 -e ENABLE_SPAMASSASSIN=1 -e SPAMASSASSIN_SPAM_TO_INBOX=1 -e ENABLE_CLAMAV=1 -e ENABLE_POSTGREY=1 -e ENABLE_FAIL2BAN=0 -e ENABLE_SASLAUTHD=0 -e ONE_DIR=1 -e TZ=America/New_York -v ~/docker/mailserver/data/:/var/mail/ -v ~/docker/mailserver/state/:/var/mail-state/ -v ~/docker/mailserver/logs/:/var/log/mail/ -v ~/docker/mailserver/config/:/tmp/docker-mailserver/ --restart=unless-stopped mailserver/docker-mailserver
```

- Add admin user

```bash
docker run --rm -e MAIL_USER=admin@$DOCKER_MAILSERVER_DOMAIN_NAME -e MAIL_PASS=admin -it mailserver/docker-mailserver /bin/sh -c 'echo "$MAIL_USER|$(doveadm pw -s SHA512-CRYPT -u $MAIL_USER -p $MAIL_PASS)"' >> ~/docker/mailserver/config/postfix-accounts.cf
```

> Check that the user has been added to the configuration file `~/docker/mailserver/config/postfix-accounts.cf`
# Test

```javascript
const nodemailer = require("nodemailer");

let transporter = nodemailer.createTransport({
host: "ec2-122-248-230-39.ap-southeast-1.compute.amazonaws.com",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: "admin@ec2-122-248-230-39.ap-southeast-1.compute.amazonaws.com",
pass: "admin",
},
tls: { rejectUnauthorized: false },
debug: true,
logger: true, // Enable logger
});

let mailOptions = {
from: "admin@ec2-122-248-230-39.ap-southeast-1.compute.amazonaws.com",
to: "admin@ec2-122-248-230-39.ap-southeast-1.compute.amazonaws.com",
subject: "Hello ✔",
text: "Hello world?",
html: "<b>Hello world?</b>",
};

transporter.verify((error) => {
if (error) {
return console.error("verify:", error);
}

transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.error(error);
}
console.log("Message sent: %s", info.messageId);
});
});
```

> Check that received mail is stored in `~/docker/mailserver/data/-/${user}/new`

0 comments on commit a675d6c

Please sign in to comment.