@@ -187,6 +187,111 @@ async function sendMailProvider(req, plan, monthchange) {
187
187
}
188
188
}
189
189
}
190
+ async function sendcustomsmtp ( extRes , req ) {
191
+ const smtpsecure = extRes . SmtpConfig . port !== '465' ? false : true ;
192
+ const transporterSMTP = createTransport ( {
193
+ host : extRes . SmtpConfig . host ,
194
+ port : extRes . SmtpConfig . port ,
195
+ secure : smtpsecure ,
196
+ auth : { user : extRes . SmtpConfig . username , pass : extRes . SmtpConfig . password } ,
197
+ } ) ;
198
+ if ( req . params . url ) {
199
+ let Pdf = fs . createWriteStream ( 'test.pdf' ) ;
200
+ const writeToLocalDisk = ( ) => {
201
+ return new Promise ( ( resolve , reject ) => {
202
+ if ( useLocal !== 'true' ) {
203
+ https . get ( req . params . url , async function ( response ) {
204
+ response . pipe ( Pdf ) ;
205
+ response . on ( 'end' , ( ) => resolve ( 'success' ) ) ;
206
+ } ) ;
207
+ } else {
208
+ const path = new URL ( req . params . url ) ?. pathname ;
209
+ const localurl = 'http://localhost:8080' + path ;
210
+ http . get ( localurl , async function ( response ) {
211
+ response . pipe ( Pdf ) ;
212
+ response . on ( 'end' , ( ) => resolve ( 'success' ) ) ;
213
+ } ) ;
214
+ }
215
+ } ) ;
216
+ } ;
217
+ // `writeToLocalDisk` is used to create pdf file from doc url
218
+ const ress = await writeToLocalDisk ( ) ;
219
+ if ( ress ) {
220
+ function readTolocal ( ) {
221
+ return new Promise ( ( resolve , reject ) => {
222
+ setTimeout ( ( ) => {
223
+ let PdfBuffer = fs . readFileSync ( Pdf . path ) ;
224
+ resolve ( PdfBuffer ) ;
225
+ } , 100 ) ;
226
+ } ) ;
227
+ }
228
+ // `PdfBuffer` used to create buffer from pdf file
229
+ let PdfBuffer = await readTolocal ( ) ;
230
+ const pdfName = req . params . pdfName ? `${ req . params . pdfName } .pdf` : 'exported.pdf' ;
231
+ const file = { filename : pdfName , content : PdfBuffer } ;
232
+ let attachment ;
233
+ const certificatePath = './exports/certificate.pdf' ;
234
+ if ( fs . existsSync ( certificatePath ) ) {
235
+ try {
236
+ // `certificateBuffer` used to create buffer from pdf file
237
+ const certificateBuffer = fs . readFileSync ( certificatePath ) ;
238
+ const certificate = { filename : 'certificate.pdf' , content : certificateBuffer } ;
239
+ attachment = [ file , certificate ] ;
240
+ } catch ( err ) {
241
+ attachment = [ file ] ;
242
+ console . log ( 'Err in read certificate sendmailv3' , err ) ;
243
+ }
244
+ } else {
245
+ attachment = [ file ] ;
246
+ }
247
+ const from = req . params . from || '' ;
248
+ const mailsender = extRes . SmtpConfig . username ;
249
+
250
+ const messageParams = {
251
+ from : from + ' <' + mailsender + '>' ,
252
+ to : req . params . recipient ,
253
+ subject : req . params . subject ,
254
+ text : req . params . text || 'mail' ,
255
+ html : req . params . html || '' ,
256
+ attachments : attachment ,
257
+ } ;
258
+ const res = await transporterSMTP . sendMail ( messageParams ) ;
259
+ console . log ( 'custom smtp transporter res: ' , res ?. response ) ;
260
+ if ( ! res . err ) {
261
+ if ( req . params ?. extUserId ) {
262
+ await updateMailCount ( req . params . extUserId ) ; //, plan, monthchange
263
+ }
264
+ if ( fs . existsSync ( certificatePath ) ) {
265
+ try {
266
+ fs . unlinkSync ( certificatePath ) ;
267
+ } catch ( err ) {
268
+ console . log ( 'Err in unlink certificate sendmailv3' ) ;
269
+ }
270
+ }
271
+ return { status : 'success' , code : 200 } ;
272
+ }
273
+ }
274
+ } else {
275
+ const from = req . params . from || '' ;
276
+ const mailsender = extRes . SmtpConfig . username ;
277
+ const messageParams = {
278
+ from : from + ' <' + mailsender + '>' ,
279
+ to : req . params . recipient ,
280
+ subject : req . params . subject ,
281
+ text : req . params . text || 'mail' ,
282
+ html : req . params . html || '' ,
283
+ } ;
284
+
285
+ const res = await transporterSMTP . sendMail ( messageParams ) ;
286
+ console . log ( 'custom smtp transporter res: ' , res ?. response ) ;
287
+ if ( ! res . err ) {
288
+ if ( req . params ?. extUserId ) {
289
+ await updateMailCount ( req . params . extUserId ) ; //, plan, monthchange
290
+ }
291
+ return { status : 'success' , code : 200 } ;
292
+ }
293
+ }
294
+ }
190
295
async function sendmailv3 ( req ) {
191
296
const mailProvider = req . params . mailProvider || 'default' ;
192
297
if ( mailProvider ) {
@@ -206,14 +311,26 @@ async function sendmailv3(req) {
206
311
const extRes = await extUserQuery . get ( extUserId , { useMasterKey : true } ) ;
207
312
if ( extRes ) {
208
313
const _extRes = JSON . parse ( JSON . stringify ( extRes ) ) ;
209
- if ( _extRes . google_refresh_token && mailProvider === 'google' ) {
314
+ if (
315
+ _extRes . active_mail_adapter === 'google' &&
316
+ _extRes . google_refresh_token &&
317
+ mailProvider === 'google'
318
+ ) {
210
319
const res = await sendMailGmailProvider ( _extRes , template ) ;
211
320
if ( res . code === 200 ) {
212
321
await updateMailCount ( req . params . extUserId ) ;
213
322
return { status : 'success' } ;
214
323
} else {
215
324
return { status : 'error' } ;
216
325
}
326
+ } else if ( _extRes . active_mail_adapter === 'smtp' && mailProvider === 'smtp' ) {
327
+ const res = await sendcustomsmtp ( _extRes , req ) ;
328
+ if ( res . code === 200 ) {
329
+ await updateMailCount ( req . params . extUserId ) ;
330
+ return { status : 'success' } ;
331
+ } else {
332
+ return { status : 'error' } ;
333
+ }
217
334
} else {
218
335
if ( Plan && Plan === 'freeplan' ) {
219
336
let MonthlyFreeEmails = _extRes ?. MonthlyFreeEmails || 0 ;
0 commit comments