- 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
108 lines
2.9 KiB
JavaScript
108 lines
2.9 KiB
JavaScript
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();
|
||
|