emailService.jsā¢4.46 kB
const nodemailer = require("nodemailer");
// Email configuration
const emailConfig = {
smtp: {
host: process.env.SMTP_HOST || "smtp.gmail.com",
port: parseInt(process.env.SMTP_PORT || "587"),
secure: process.env.SMTP_SECURE === "true",
auth: {
user: process.env.SMTP_USER || "your-email@gmail.com",
pass: process.env.SMTP_PASS || "your-app-password",
},
},
defaultFrom: {
name: "MCP Server",
email: process.env.SMTP_USER || "your-email@gmail.com",
},
templates: {
default: {
subject: "Notification from MCP Server",
html: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h2 style="color: #333;">MCP Server Notification</h2>
<div style="background-color: #f5f5f5; padding: 20px; border-radius: 5px; margin: 20px 0;">
{{body}}
</div>
<p style="color: #666; font-size: 12px;">
This email was sent from the MCP Server application.
</p>
</div>
`,
},
},
};
class EmailService {
constructor() {
this.transporter = nodemailer.createTransporter(emailConfig.smtp);
}
/**
* Send email notification
*/
async sendEmail(request) {
try {
this.validateEmailRequest(request);
const mailOptions = {
from:
request.from ||
`${emailConfig.defaultFrom.name} <${emailConfig.defaultFrom.email}>`,
to: request.recipient,
subject: request.subject,
html: this.formatEmailBody(request.body),
text: request.body,
};
const info = await this.transporter.sendMail(mailOptions);
return {
success: true,
messageId: info.messageId,
};
} catch (error) {
console.error("Email sending failed:", error);
return {
success: false,
error:
error instanceof Error ? error.message : "Unknown error occurred",
};
}
}
/**
* Send email with template
*/
async sendTemplatedEmail(recipient, subject, body, template = "default") {
const templateData = emailConfig.templates[template];
if (!templateData) {
throw new Error(`Template '${template}' not found`);
}
const formattedBody = templateData.html.replace("{{body}}", body);
const finalSubject = subject || templateData.subject;
return this.sendEmail({
recipient,
subject: finalSubject,
body: formattedBody,
});
}
/**
* Test email configuration
*/
async testConnection() {
try {
await this.transporter.verify();
return true;
} catch (error) {
console.error("Email connection test failed:", error);
return false;
}
}
/**
* Validate email request
*/
validateEmailRequest(request) {
if (!request.recipient) {
throw new Error("Recipient email is required");
}
if (!request.subject) {
throw new Error("Email subject is required");
}
if (!request.body) {
throw new Error("Email body is required");
}
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(request.recipient)) {
throw new Error("Invalid recipient email format");
}
}
/**
* Format email body with basic HTML structure
*/
formatEmailBody(body) {
if (body.includes("<") && body.includes(">")) {
return body;
}
return `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px;">
<div style="background-color: #f8f9fa; padding: 20px; border-radius: 8px; border-left: 4px solid #007bff;">
${body.replace(/\n/g, "<br>")}
</div>
<div style="margin-top: 20px; padding-top: 20px; border-top: 1px solid #dee2e6; color: #6c757d; font-size: 12px;">
<p>This email was sent from the MCP Server application.</p>
<p>Timestamp: ${new Date().toISOString()}</p>
</div>
</div>
`;
}
/**
* Get email configuration status
*/
getConfigStatus() {
const issues = [];
if (!emailConfig.smtp.host) {
issues.push("SMTP host not configured");
}
if (!emailConfig.smtp.auth.user) {
issues.push("SMTP user not configured");
}
if (!emailConfig.smtp.auth.pass) {
issues.push("SMTP password not configured");
}
return {
configured: issues.length === 0,
issues,
};
}
}
module.exports = { EmailService };