import { Injectable, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import * as nodemailer from 'nodemailer'; @Injectable() export class EmailService { private transporter: nodemailer.Transporter; private readonly logger = new Logger(EmailService.name); private readonly from: string; constructor(private configService: ConfigService) { console.log('Initializing EmailService...'); // Direct console log for debugging this.logger.log('Initializing EmailService...'); // Load config const host = this.configService.get('SMTP_HOST'); const port = this.configService.get('SMTP_PORT'); const user = this.configService.get('SMTP_USER'); const pass = this.configService.get('SMTP_PASS'); this.from = this.configService.get('EMAIL_FROM'); console.log('Email Config:', { host, port, user, from: this.from }); // Direct console log this.logger.log('Email Config:', { host, port, user, from: this.from }); // Create transporter with Gmail settings this.transporter = nodemailer.createTransport({ // host: 'smtp.gmail.com', // port: 587, // Use STARTTLS port // secure: false, // Use STARTTLS host: host, port: port, auth: { user, pass }, debug: true, // Enable debug logs logger: true // Enable transport level logging }); // Verify connection this.verifyConnection(); } private async verifyConnection() { try { console.log('Verifying SMTP connection...'); // Direct console log this.logger.log('Verifying SMTP connection...'); const verification = await this.transporter.verify(); console.log('SMTP connection verified successfully!', verification); // Direct console log this.logger.log('SMTP connection verified successfully!', verification); } catch (error) { console.error('SMTP connection failed:', error); // Direct console log this.logger.error('SMTP connection failed:', { error: error.message, code: error.code, command: error.command, response: error.response, responseCode: error.responseCode, stack: error.stack, }); throw error; } } async sendWelcomeEmail(userEmail: string, username: string): Promise { console.log(`Sending welcome email to ${userEmail}...`); // Direct console log this.logger.log(`Sending welcome email to ${userEmail}...`); const mailOptions = { from: `"IMK Platform" <${this.from}>`, to: userEmail, subject: 'Welcome to IMK Platform!', html: `

Welcome to IMK Platform!

Dear ${username},

Thank you for joining IMK Platform. We're excited to have you on board!

You can now start using our platform to manage and share your documents securely.

If you have any questions or need assistance, please don't hesitate to contact our support team.

Best regards,
The IMK Team

` }; try { console.log('Attempting to send email with options:', mailOptions); // Direct console log this.logger.log('Attempting to send email with options:', { to: mailOptions.to, from: mailOptions.from, subject: mailOptions.subject }); const info = await this.transporter.sendMail(mailOptions); console.log('Email sent successfully:', info); // Direct console log this.logger.log('Email sent successfully:', { messageId: info.messageId, response: info.response, accepted: info.accepted, rejected: info.rejected, envelope: info.envelope, }); } catch (error) { console.error('Failed to send email:', error); // Direct console log this.logger.error('Failed to send email:', { error: error.message, code: error.code, command: error.command, response: error.response, responseCode: error.responseCode, stack: error.stack, }); throw error; } } async sendDocumentNotification( userEmail: string, username: string, documentName: string, action: 'uploaded' | 'shared', ): Promise { console.log('=== Starting document notification email process ==='); this.logger.log('=== Starting document notification email process ==='); console.log(`Preparing notification for ${userEmail}, document: ${documentName}, action: ${action}`); this.logger.log(`Preparing notification for ${userEmail}, document: ${documentName}, action: ${action}`); const actionText = action === 'uploaded' ? 'uploaded' : 'shared with you'; const mailOptions = { from: `"IMK Platform" <${this.from}>`, to: userEmail, subject: `New Document ${actionText} - IMK Platform`, html: `

New Document Notification

Dear ${username},

A new document "${documentName}" has been ${actionText} on the IMK Platform.

You can access this document by logging into your account.

If you have any questions or concerns, please contact our support team.

Best regards,
The IMK Team

`, }; try { console.log('Sending document notification email with options:', { to: mailOptions.to, from: mailOptions.from, subject: mailOptions.subject, documentName, action }); this.logger.log('Sending document notification email with options:', { to: mailOptions.to, from: mailOptions.from, subject: mailOptions.subject, documentName, action }); const info = await this.transporter.sendMail(mailOptions); console.log('Document notification email sent successfully:', { messageId: info.messageId, response: info.response, accepted: info.accepted, rejected: info.rejected, envelope: info.envelope, }); this.logger.log('Document notification email sent successfully:', { messageId: info.messageId, response: info.response, accepted: info.accepted, rejected: info.rejected, envelope: info.envelope, }); } catch (error) { console.error('Failed to send document notification email:', error); this.logger.error('Failed to send document notification email:', { error: error.message, code: error.code, command: error.command, response: error.response, responseCode: error.responseCode, stack: error.stack, }); throw error; } } }