Skip to content

Commit

Permalink
changed the email_template.html to follow the {{}} convention
Browse files Browse the repository at this point in the history
added an email_template.txt as an eqivelent for mail resivers that block html
added an test for nodemailer to check if the smpt clinet is working
changed the send-mails that it loads the templates and inputs and replaces it with the text and name
added an tempory test befor the socket handelers for the smtp clent
compose-messages.ts Added send TODO has to get the Name of the email recever
  • Loading branch information
antidodo committed Jul 21, 2022
1 parent f075efa commit b021043
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/components/socket/domains/compose-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function formatEmailHTMLText(text, receiver_name) {
}
async function send(smtpClient: nodemailer.Transport, station: Station, emailHTMLText: string, emailSubject: string) {
if (station.email != null) {
await sendEmail(smtpClient, station.email, emailHTMLText, emailSubject);
// TODO get name
await sendEmail(smtpClient, station.email, emailHTMLText,station.email, emailSubject);
}
}
6 changes: 3 additions & 3 deletions src/components/socket/domains/email_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>{receiver_name}</title>
<title>{{receiver_name}}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body style="margin: 0; padding: 0;">
Expand All @@ -25,12 +25,12 @@
<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse;">
<tr>
<td style="padding: 20px 20px 20px 20px;" >
Hallo {receiver_name} admin,
Hallo {{receiver_name}},
</td>
</tr>
<tr>
<td style="padding: 20px 20px 20px 20px;">
{text}
{{text}}
</td>
</tr>
<tr>
Expand Down
7 changes: 7 additions & 0 deletions src/components/socket/domains/email_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

Hallo {{receiver_name}},

{{text}}

Regards,
PHT
16 changes: 13 additions & 3 deletions src/components/socket/domains/send-mail.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { nodemailer } from 'nodemailer';
import * as env from '../../../env';
import fs from "fs";

export async function sendEmail(client: nodemailer.Transport, destinationAddress: string, emailBody: string, emailSubject: string) {
export async function sendEmail(client: nodemailer.Transport, destinationAddress: string, Text: string, receiver_name: string, emailSubject: string) {
// create the email HTML body from the template
let emailBodyHTML = fs.readFileSync("./src/components/socket/domains/email_template.html", "utf8");
emailBodyHTML = emailBodyHTML.replace(/{{receiver_name}}/g, receiver_name );
emailBodyHTML = emailBodyHTML.replace(/{{text}}/g, Text);
// create the email plain text body from the template
let emailBodyPlain = fs.readFileSync("./src/components/socket/domains/email_template.txt", "utf8");
emailBodyPlain = emailBodyPlain.replace(/{{receiver_name}}/g, receiver_name );
emailBodyPlain = emailBodyPlain.replace(/{{text}}/g, Text);
// send the email
await client.sendMail({
from: env.default.smtpMailFrom, // sender address
to: destinationAddress, // list of receivers
subject: emailSubject, // Subject line
text: emailBody, // plain text body
html: emailBody, // html body
text: emailBodyPlain, // plain text body
html: emailBodyHTML, // html body
});
}
28 changes: 25 additions & 3 deletions src/components/socket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import * as env from '../../env'
import { findTokenForRobot } from '../../config/utils';
import { createTestSocketComponentHandler } from './domains/test';
import { ProposalStationSocketComponentHandler } from './domains/proposal-station';
import {SMTPClient} from "smtp-client";
import {TrainStationSocketComponentHandler} from "./domains/train-listeners";
import {sendEmail} from "./domains/send-mail";





export function buildSocketComponentHandler() {
function start() {

Promise.resolve()
.then(findTokenForRobot)
.then((token) => {
Expand All @@ -37,13 +39,33 @@ export function buildSocketComponentHandler() {
pass: env.default.smtpPassword, // generated ethereal password
},
});
smtpClient.verify(function (error, success) {
if (error) {
console.log(error);
} else {
console.log("email Server is ready to take our messages");
}
if (success) {
console.log(success);
} else {
console.log("Server is not ready to take our messages, look at the smtp configurations and try again");
}
});
// test for smtp client remove this lines later
let Text = "this is a test text";
let emailSubject = "this is a test subject";
let receiver_name = "Jhon Doe";
sendEmail(smtpClient, "david.hieber@uni-tuebingen.de", Text, receiver_name, emailSubject).then(r =>
console.log(r));
// end of test



console.log('socket connected');
//createProposalSocketComponentHandler(socket);
createTestSocketComponentHandler(socket);
ProposalStationSocketComponentHandler(socket, smtpClient);
TrainStationSocketComponentHandler(socket, SMTPClient);
TrainStationSocketComponentHandler(socket, smtpClient);

// todo: add additional domain handlers

socket.connect();
Expand Down

0 comments on commit b021043

Please sign in to comment.