@@ -12,16 +12,9 @@ export class MailNotification extends NotificationContract {
1212 private fromAddress ?: string
1313 private subjectLine ?: string
1414 private recipients ?: MailRecipient
15- static defaultHtmlTemplate = [
16- '<!doctype html>' ,
17- '<html>' ,
18- '<body style="font-family:Arial,sans-serif;line-height:1.5;color:#111827">' ,
19- '<h2>{subject}</h2>' ,
20- '<div>{message}</div>' ,
21- '<p style="color:#6b7280;font-size:12px">© {year} {app_name}</p>' ,
22- '</body>' ,
23- '</html>' ,
24- ] . join ( '' )
15+ private ViewName : string = '~arkstack/notifications.mail'
16+ private htmlTemplate ?: string
17+ private textTemplate ?: string
2518
2619 constructor ( options : MailDriverOptions = { } ) {
2720 super ( )
@@ -57,18 +50,66 @@ export class MailNotification extends NotificationContract {
5750 return this
5851 }
5952
53+ /**
54+ * The email subject
55+ *
56+ * @param subject
57+ * @returns
58+ */
6059 subject ( subject : string ) : this {
6160 this . subjectLine = subject
6261
6362 return this
6463 }
6564
65+ /**
66+ * Set email the notification recipeint
67+ *
68+ * @param recipient string or array of email addresses
69+ * @returns
70+ */
6671 recipient ( recipient : MailRecipient ) : this {
6772 this . recipients = recipient
6873
6974 return this
7075 }
7176
77+ /**
78+ * Set email the notification view name
79+ *
80+ * @param view view name
81+ * @returns
82+ */
83+ view ( view : string ) : this {
84+ this . ViewName = view
85+
86+ return this
87+ }
88+
89+ /**
90+ * Set email the notification html template
91+ *
92+ * @param content view name
93+ * @returns
94+ */
95+ html ( content : string ) : this {
96+ this . htmlTemplate = content
97+
98+ return this
99+ }
100+
101+ /**
102+ * Set email the notification text template
103+ *
104+ * @param content view name
105+ * @returns
106+ */
107+ text ( content : string ) : this {
108+ this . textTemplate = content
109+
110+ return this
111+ }
112+
72113 /**
73114 * Prepare a notification to be sent.
74115 *
@@ -115,16 +156,25 @@ export class MailNotification extends NotificationContract {
115156 throw new Error ( 'No recipient provided for mail notification' )
116157 }
117158
159+ const templateData = {
160+ ...mergedData ,
161+ message : resolvedMessage ,
162+ subject : resolvedSubject ,
163+ }
164+
165+ const textMessage = resolvedMessage . replace ( / < \/ ? [ ^ > ] + ( > | $ ) / g, '' )
166+
118167 return await this . driver . sendMail ( {
119168 to : to as never ,
120169 subject : resolvedSubject ,
121170 from : this . fromAddress ,
122- text : resolvedMessage . replace ( / < \/ ? [ ^ > ] + ( > | $ ) / g, '' ) ,
123- html : interpolate ( await globalThis . view ( '~arkstack/notifications.mail' , {
124- ...mergedData ,
125- message : resolvedMessage ,
126- subject : resolvedSubject ,
127- } ) ) ,
171+ text : interpolate (
172+ this . textTemplate ?? textMessage ,
173+ { ...templateData , message : textMessage }
174+ ) ,
175+ html : this . htmlTemplate
176+ ? interpolate ( this . htmlTemplate , templateData )
177+ : await globalThis . view ( this . ViewName , templateData ) ,
128178 } )
129179 }
130180
0 commit comments