@@ -2,46 +2,57 @@ import { DeliveryInformation } from '@dm3-org/dm3-lib-messaging';
2
2
import { logError } from '@dm3-org/dm3-lib-shared' ;
3
3
import nodemailer from 'nodemailer' ;
4
4
import SMTPTransport from 'nodemailer/lib/smtp-transport' ;
5
+ import { NotificationType } from '../types' ;
6
+ import { fetchEmailSubjectAndTemplate } from '../utils' ;
5
7
6
- // Define types for email server and user configuration
7
- export type EmailNotificationServerConfig = SMTPTransport . Options & {
8
- senderAddress : string ;
8
+ // email server configuration
9
+ export type EmailNotificationServerConfig = {
10
+ host : string ;
11
+ port : number ;
12
+ username : string ;
13
+ password : string ;
14
+ emailID : string ;
9
15
} ;
10
16
17
+ // email notification configuration
11
18
export type EmailNotificationUserConfig = {
12
19
recipientAddress : string ;
20
+ notificationType : NotificationType ;
13
21
} ;
14
22
15
- // Constants for email subject and HTML template
16
- export const MAIL_SUBJECT = 'New DM3 Message' ;
17
- export const MAIL_HTML = (
18
- deliveryInformation : DeliveryInformation ,
19
- ) => `<html lang="en">
20
- <body>
21
- <p>You have received a new DM3 message from ${ deliveryInformation . from } .
22
- <br/>
23
- Open <a href="app.dm3.network">DM3</a> to read it</p>
24
- <script src="index.js"></script>
25
- </body>
26
- </html>` ;
27
-
28
- // Define the Email function
23
+ // method to send email
29
24
export function Email ( config : EmailNotificationServerConfig ) {
30
25
const send = async (
31
26
mailConfig : EmailNotificationUserConfig ,
32
27
deliveryInformation : DeliveryInformation ,
33
28
) => {
34
- nodemailer . createTestAccount ( ) ;
35
- const transport = nodemailer . createTransport ( new SMTPTransport ( config ) ) ;
36
-
37
29
try {
38
- // Send the email using nodemailer
30
+ // create transport with email credentials
31
+ const transport : nodemailer . Transporter < SMTPTransport . SentMessageInfo > =
32
+ nodemailer . createTransport ( {
33
+ host : config . host ,
34
+ port : config . port ,
35
+ auth : {
36
+ user : config . username ,
37
+ pass : config . password ,
38
+ } ,
39
+ } ) ;
40
+
41
+ // fetch the specific subject & template of email
42
+ const { subject, template } = fetchEmailSubjectAndTemplate (
43
+ mailConfig . notificationType ,
44
+ deliveryInformation ,
45
+ ) ;
46
+
47
+ // send the email using nodemailer
39
48
await transport . sendMail ( {
40
- from : config . senderAddress ,
49
+ from : config . emailID ,
41
50
to : mailConfig . recipientAddress ,
42
- subject : MAIL_SUBJECT ,
43
- html : MAIL_HTML ( deliveryInformation ) ,
51
+ subject : subject ,
52
+ html : template ,
44
53
} ) ;
54
+
55
+ // close the connection
45
56
transport . close ( ) ;
46
57
} catch ( err ) {
47
58
logError ( 'Send mail failed ' + err ) ;
0 commit comments