gmail-mcp-server
Version:
Gmail MCP Server with on-demand authentication for SIYA/Claude Desktop. Complete Gmail integration with multi-user support and OAuth2 security.
80 lines (79 loc) • 2.95 kB
JavaScript
import { appPasswordAuth } from './app-password-auth.js';
import { logger } from './api.js';
/**
* SMTP-based email operations using App Passwords
* This replaces Gmail API operations for sending emails
*/
export class SMTPOperations {
/**
* Send an email using SMTP
*/
async sendEmail(emailMessage) {
try {
if (!await appPasswordAuth.isAuthenticated()) {
throw new Error('SMTP authentication required. Please configure app password first.');
}
const transporter = appPasswordAuth.getSMTPTransporter();
// Prepare email options
const mailOptions = {
from: appPasswordAuth.getUserEmail(),
to: emailMessage.to.join(', '),
subject: emailMessage.subject,
text: emailMessage.text,
html: emailMessage.html,
replyTo: emailMessage.replyTo
};
// Add CC if provided
if (emailMessage.cc && emailMessage.cc.length > 0) {
mailOptions.cc = emailMessage.cc.join(', ');
}
// Add BCC if provided
if (emailMessage.bcc && emailMessage.bcc.length > 0) {
mailOptions.bcc = emailMessage.bcc.join(', ');
}
// Add attachments if provided
if (emailMessage.attachments && emailMessage.attachments.length > 0) {
mailOptions.attachments = emailMessage.attachments.map(att => ({
filename: att.filename,
content: att.content,
contentType: att.contentType,
encoding: att.encoding || 'base64'
}));
}
// Send the email
const info = await transporter.sendMail(mailOptions);
logger.log('Email sent successfully via SMTP');
return {
id: info.messageId || 'smtp-' + Date.now(),
threadId: undefined // SMTP doesn't provide thread IDs like Gmail API
};
}
catch (error) {
logger.error('Error sending email via SMTP:', error);
throw new Error(`Failed to send email: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
/**
* Test SMTP connection
*/
async testConnection() {
try {
const transporter = appPasswordAuth.getSMTPTransporter();
await transporter.verify();
return true;
}
catch (error) {
logger.error('SMTP connection test failed:', error);
return false;
}
}
/**
* Reset SMTP client (for authentication changes)
*/
resetClient() {
// App password auth handles its own transporter reset
logger.log('SMTP client reset');
}
}
// Export singleton instance
export const smtpOperations = new SMTPOperations();