mirror of
https://github.com/detleph/server.git
synced 2026-09-04 08:36:06 +02:00
Add mail functionality into the development branch (#12)
* Added basic code for sending emails + Added tests for emails + Modified README.md to reflect changes to .env file * [create-pull-request] push formatted files * Added dkim * Fixed mistake in docker-compose.yml Co-authored-by: Stefan <[email protected]>
This commit is contained in:
@@ -105,3 +105,6 @@ dist
|
|||||||
|
|
||||||
# Prisma
|
# Prisma
|
||||||
prisma/migrations
|
prisma/migrations
|
||||||
|
|
||||||
|
# Ignore config files for docker
|
||||||
|
host/
|
||||||
@@ -9,5 +9,9 @@ Before Runningthis on you local machine some things have to be setup
|
|||||||
|
|
||||||
```
|
```
|
||||||
DATABASE_URL: ADDRESS TO YOUR DATABASE
|
DATABASE_URL: ADDRESS TO YOUR DATABASE
|
||||||
|
DATABASE_PASSWORD: PASSOWRD OF THE DATABASE
|
||||||
PORT: PORT WHERE THE API SHOULD RUN
|
PORT: PORT WHERE THE API SHOULD RUN
|
||||||
|
DOMAIN: THE DOMAIN NAME OF THE SERVER
|
||||||
|
MAILPASSWORD: THE PASSWORD FOR THE MAIL ACCOUNT
|
||||||
|
DEV: SWITCH FOR DEV MODE AFFECTS EMAIL SERVER
|
||||||
```
|
```
|
||||||
|
|||||||
+22
-20
@@ -1,22 +1,24 @@
|
|||||||
version: "3"
|
version: "3"
|
||||||
services:
|
services:
|
||||||
mail:
|
mail:
|
||||||
image: boky/postfix
|
image: boky/postfix
|
||||||
restart: always
|
restart: always
|
||||||
environment:
|
environment:
|
||||||
- ALLOWED_SENDER_DOMAINS=mail.${DOMAIN}
|
- ALLOWED_SENDER_DOMAINS=mail.${DOMAIN}
|
||||||
ports:
|
ports:
|
||||||
- "587:587"
|
- "587:587"
|
||||||
container_name: "postfix"
|
container_name: "postfix"
|
||||||
postgres:
|
volumes:
|
||||||
image: postgres
|
- ./host/keys/:/etc/opendkim/keys
|
||||||
restart: always
|
postgres:
|
||||||
environment:
|
image: postgres
|
||||||
- POSTGRES_PASSWORD=${DATABASE_PASSWORD}
|
restart: always
|
||||||
ports:
|
environment:
|
||||||
- 5432:5432
|
- POSTGRES_PASSWORD=${DATABASE_PASSWORD}
|
||||||
container_name: "postgres"
|
ports:
|
||||||
#server:
|
- 5432:5432
|
||||||
# build: .
|
container_name: "postgres"
|
||||||
# restart: always
|
#server:
|
||||||
# container_name: "server"
|
# build: .
|
||||||
|
# restart: always
|
||||||
|
# container_name: "server"
|
||||||
|
|||||||
Generated
+2115
-39
File diff suppressed because it is too large
Load Diff
+10
-6
@@ -19,20 +19,24 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/detleph/server#readme",
|
"homepage": "https://github.com/detleph/server#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@prisma/client": "^3.2.1",
|
"@prisma/client": "^3.3.0",
|
||||||
"@types/chai-http": "^4.2.0",
|
"@types/node": "^16.10.3",
|
||||||
"chai-http": "^4.3.0",
|
"@types/nodemailer": "^6.4.4",
|
||||||
"dotenv": "^10.0.0",
|
"dotenv": "^10.0.0",
|
||||||
"express": "^4.17.1"
|
"express": "^4.17.1",
|
||||||
|
"nodemailer": "^6.7.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/chai": "^4.2.22",
|
"@types/chai": "^4.2.22",
|
||||||
|
"@types/chai-as-promised": "^7.1.4",
|
||||||
|
"@types/chai-http": "^4.2.0",
|
||||||
"@types/express": "^4.17.13",
|
"@types/express": "^4.17.13",
|
||||||
"@types/mocha": "^9.0.0",
|
"@types/mocha": "^9.0.0",
|
||||||
"@types/node": "^16.10.3",
|
|
||||||
"chai": "^4.3.4",
|
"chai": "^4.3.4",
|
||||||
|
"chai-as-promised": "^7.1.1",
|
||||||
|
"chai-http": "^4.3.0",
|
||||||
"mocha": "^9.1.3",
|
"mocha": "^9.1.3",
|
||||||
"prisma": "^3.2.1",
|
"prisma": "^3.3.0",
|
||||||
"ts-node": "^10.2.1",
|
"ts-node": "^10.2.1",
|
||||||
"typescript": "^4.4.3"
|
"typescript": "^4.4.3"
|
||||||
}
|
}
|
||||||
|
|||||||
Executable
+33
@@ -0,0 +1,33 @@
|
|||||||
|
#! /bin/bash
|
||||||
|
|
||||||
|
|
||||||
|
#load .env file
|
||||||
|
|
||||||
|
[[ -f .env ]] || { echo "`tput setaf 1`✗`tput sgr0` Could not find .env file are you in the correct directory (you should be in the server directory)"; exit 1;}
|
||||||
|
|
||||||
|
set -o allexport
|
||||||
|
[[ -f .env ]] && source .env
|
||||||
|
set +o allexport
|
||||||
|
|
||||||
|
#static variables
|
||||||
|
NC='tput sgr0' #No Color
|
||||||
|
GREEN='tput setaf 2'
|
||||||
|
RED='tput setaf 1'
|
||||||
|
|
||||||
|
|
||||||
|
#check dependencies
|
||||||
|
command -v opendkim-genkey >/dev/null 2>&1 || { echo >&2 "`$RED`✗`$NC` Opendkim not installed install with your favorite package manager (opendkim-tools | opendkim-utils)"; exit 1;}
|
||||||
|
|
||||||
|
#generate dkim keys
|
||||||
|
mkdir -p ./host/keys
|
||||||
|
echo "`$GREEN`✓`$NC` Created folder"
|
||||||
|
|
||||||
|
cd ./host/keys
|
||||||
|
opendkim-genkey -b 2048 -h rsa-sha256 -r -v --subdomains -s mail -d mail.$DOMAIN
|
||||||
|
sed -i 's/h=rsa-sha256/sha256/' mail.txt
|
||||||
|
mv mail.private mail.$DOMAIN.private
|
||||||
|
mv mail.txt mail.$DOMAIN.txt
|
||||||
|
|
||||||
|
echo "`$GREEN`✓`$NC` Generated dkim keys"
|
||||||
|
|
||||||
|
exit 0
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
process.env.NODE_ENV = "test";
|
||||||
|
|
||||||
|
import chai, { expect } from "chai";
|
||||||
|
import chaiaspromised from "chai-as-promised";
|
||||||
|
import mail from "../lib/mail";
|
||||||
|
|
||||||
|
chai.use(chaiaspromised);
|
||||||
|
const should = chai.should();
|
||||||
|
|
||||||
|
describe("mail", () => {
|
||||||
|
it("should succesfully send a email", async () => {
|
||||||
|
//NOTE THESE TESTS ONLY WORK WITH DEV BEING TRUE
|
||||||
|
const info = await mail.sendMail('"Chai Testing suite" <[email protected]>', "[email protected]", "Hello", "Test");
|
||||||
|
info.rejected.length.should.eq(0);
|
||||||
|
info.accepted.length.should.eq(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
import nodemailer from "nodemailer";
|
||||||
|
import SMTPTransport from "nodemailer/lib/smtp-transport";
|
||||||
|
|
||||||
|
let mailAccount = { user: process.env.MAILUSER + "@mail." + process.env.DOMAIN, pass: process.env.MAILPASSWORD };
|
||||||
|
|
||||||
|
let transporter = nodemailer.createTransport(
|
||||||
|
new SMTPTransport({
|
||||||
|
host: "localhost",
|
||||||
|
port: 587,
|
||||||
|
secure: false,
|
||||||
|
auth: {
|
||||||
|
user: mailAccount.user,
|
||||||
|
pass: mailAccount.pass,
|
||||||
|
},
|
||||||
|
tls: {
|
||||||
|
rejectUnauthorized: false,
|
||||||
|
secureProtocol: "TLSv1_method",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
if (process.env.DEV == "true") {
|
||||||
|
(async () => {
|
||||||
|
mailAccount = await nodemailer.createTestAccount();
|
||||||
|
if (process.env.NODE_ENV != "test") {
|
||||||
|
console.log(mailAccount);
|
||||||
|
}
|
||||||
|
transporter = nodemailer.createTransport({
|
||||||
|
host: "smtp.ethereal.email",
|
||||||
|
port: 587,
|
||||||
|
secure: false, // true for 465, false for other ports
|
||||||
|
auth: {
|
||||||
|
user: mailAccount.user, // generated ethereal user
|
||||||
|
pass: mailAccount.pass, // generated ethereal password
|
||||||
|
},
|
||||||
|
});
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
|
||||||
|
const sendMail = async (from: string, to: string, subject: string, text?: string, html?: string) => {
|
||||||
|
return await transporter.sendMail({
|
||||||
|
from: from,
|
||||||
|
to: to,
|
||||||
|
subject: subject,
|
||||||
|
text: text,
|
||||||
|
html: html,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export default { sendMail, mailAccount };
|
||||||
Reference in New Issue
Block a user