Files
balikci/backend/src/services/mail.service.js
salvacybersec dea1b874b5 feat: Send actual test email instead of just verifying connection
- testConnection now sends a real test email to the configured Gmail address
- User receives actual email confirmation that setup works
- Email contains success message and system info
2025-11-10 19:31:42 +03:00

108 lines
2.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const nodemailer = require('nodemailer');
const handlebars = require('handlebars');
const logger = require('../config/logger');
const { Settings } = require('../models');
class MailService {
constructor() {
this.transporter = null;
}
async initializeTransporter() {
try {
// Get Gmail settings from database
const gmailUser = await Settings.findOne({ where: { key: 'gmail_user' } });
const gmailPassword = await Settings.findOne({ where: { key: 'gmail_password' } });
const gmailFromName = await Settings.findOne({ where: { key: 'gmail_from_name' } });
// Fallback to env variables
const user = gmailUser?.value || process.env.GMAIL_USER;
const pass = gmailPassword?.value || process.env.GMAIL_APP_PASSWORD;
const fromName = gmailFromName?.value || process.env.GMAIL_FROM_NAME || 'Güvenlik Ekibi';
if (!user || !pass) {
throw new Error('Gmail credentials not configured');
}
this.transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user,
pass,
},
});
this.fromAddress = `"${fromName}" <${user}>`;
// Verify transporter
await this.transporter.verify();
logger.info('Mail service initialized successfully');
return true;
} catch (error) {
logger.error('Failed to initialize mail service:', error);
throw error;
}
}
async sendMail(to, subject, htmlBody) {
try {
if (!this.transporter) {
await this.initializeTransporter();
}
const mailOptions = {
from: this.fromAddress,
to,
subject,
html: htmlBody,
};
const info = await this.transporter.sendMail(mailOptions);
logger.info(`Mail sent to ${to}: ${info.messageId}`);
return {
success: true,
messageId: info.messageId,
};
} catch (error) {
logger.error(`Failed to send mail to ${to}:`, error);
throw error;
}
}
renderTemplate(templateHtml, data) {
try {
const template = handlebars.compile(templateHtml);
return template(data);
} catch (error) {
logger.error('Failed to render template:', error);
throw error;
}
}
async testConnection() {
try {
await this.initializeTransporter();
// Send actual test email
const testEmail = await this.sendMail(
this.transporter.options.auth.user, // Send to self
'Test Mail - Oltalama Paneli',
'<h2>✅ Test Başarılı!</h2><p>Gmail ayarlarınız doğru yapılandırılmış ve mail gönderimi çalışıyor.</p><p><strong>Sistem:</strong> Oltalama Test Yönetim Paneli</p>'
);
return {
success: true,
message: `Test maili başarıyla gönderildi! (${testEmail.messageId})`
};
} catch (error) {
return { success: false, error: error.message };
}
}
}
module.exports = new MailService();