2022-11-11 19:03:08 +01:00
import { Injectable , InternalServerErrorException } from "@nestjs/common" ;
2022-11-11 15:12:16 +01:00
import { User } from "@prisma/client" ;
import * as nodemailer from "nodemailer" ;
2022-11-28 15:04:32 +01:00
import { ConfigService } from "src/config/config.service" ;
2022-11-11 15:12:16 +01:00
@Injectable ( )
export class EmailService {
constructor ( private config : ConfigService ) { }
async sendMail ( recipientEmail : string , shareId : string , creator : User ) {
2022-11-28 15:04:32 +01:00
// create reusable transporter object using the default SMTP transport
const transporter = nodemailer . createTransport ( {
host : this.config.get ( "SMTP_HOST" ) ,
port : parseInt ( this . config . get ( "SMTP_PORT" ) ) ,
secure : parseInt ( this . config . get ( "SMTP_PORT" ) ) == 465 ,
auth : {
2022-12-08 20:00:04 +01:00
user : this.config.get ( "SMTP_USERNAME" ) ,
2022-11-28 15:04:32 +01:00
pass : this.config.get ( "SMTP_PASSWORD" ) ,
} ,
} ) ;
2022-12-05 18:09:18 +01:00
if ( ! this . config . get ( "ENABLE_EMAIL_RECIPIENTS" ) )
2022-11-11 19:03:08 +01:00
throw new InternalServerErrorException ( "Email service disabled" ) ;
2022-12-05 16:53:52 +01:00
const shareUrl = ` ${ this . config . get ( "APP_URL" ) } /share/ ${ shareId } ` ;
2022-11-11 15:12:16 +01:00
2022-11-28 15:04:32 +01:00
await transporter . sendMail ( {
2022-11-11 15:12:16 +01:00
from : ` "Pingvin Share" < ${ this . config . get ( "SMTP_EMAIL" ) } > ` ,
to : recipientEmail ,
subject : "Files shared with you" ,
2022-12-08 20:04:56 +01:00
text : ` Hey! \ n ${ creator . username } shared some files with you. View or dowload the files with this link: ${ shareUrl } . \ nShared securely with Pingvin Share 🐧 ` ,
2022-11-11 15:12:16 +01:00
} ) ;
}
}