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:
@@ -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