outlook-mcp/email/send.js
Seton Carmichael a7886b5b2b Initial commit: Outlook MCP Server v1.0.0
MCP server providing Microsoft Outlook integration via Graph API:
- Email: list, search, read, send, thread reconstruction
- Calendar: list, create, decline, cancel, delete events
- Folders: list, create, move emails
- Inbox rules: list, create, reorder
- MSAL device code flow auth with persistent token cache
- Test mode with mock data
- Comprehensive README with setup, config, and tool reference

20 MCP tools across 5 modules. Node.js >= 14. MIT license.
2026-06-21 19:39:31 -04:00

120 lines
2.9 KiB
JavaScript

/**
* Send email functionality
*/
const config = require('../config');
const { callGraphAPI } = require('../utils/graph-api');
const { ensureAuthenticated } = require('../auth');
/**
* Send email handler
* @param {object} args - Tool arguments
* @returns {object} - MCP response
*/
async function handleSendEmail(args) {
const { to, cc, bcc, subject, body, importance = 'normal', saveToSentItems = true } = args;
// Validate required parameters
if (!to) {
return {
content: [{
type: "text",
text: "Recipient (to) is required."
}]
};
}
if (!subject) {
return {
content: [{
type: "text",
text: "Subject is required."
}]
};
}
if (!body) {
return {
content: [{
type: "text",
text: "Body content is required."
}]
};
}
try {
// Get access token
const accessToken = await ensureAuthenticated();
// Format recipients
const toRecipients = to.split(',').map(email => {
email = email.trim();
return {
emailAddress: {
address: email
}
};
});
const ccRecipients = cc ? cc.split(',').map(email => {
email = email.trim();
return {
emailAddress: {
address: email
}
};
}) : [];
const bccRecipients = bcc ? bcc.split(',').map(email => {
email = email.trim();
return {
emailAddress: {
address: email
}
};
}) : [];
// Prepare email object
const emailObject = {
message: {
subject,
body: {
contentType: body.includes('<html') ? 'html' : 'text',
content: body
},
toRecipients,
ccRecipients: ccRecipients.length > 0 ? ccRecipients : undefined,
bccRecipients: bccRecipients.length > 0 ? bccRecipients : undefined,
importance
},
saveToSentItems
};
// Make API call to send email
await callGraphAPI(accessToken, 'POST', 'me/sendMail', emailObject);
return {
content: [{
type: "text",
text: `Email sent successfully!\n\nSubject: ${subject}\nRecipients: ${toRecipients.length}${ccRecipients.length > 0 ? ` + ${ccRecipients.length} CC` : ''}${bccRecipients.length > 0 ? ` + ${bccRecipients.length} BCC` : ''}\nMessage Length: ${body.length} characters`
}]
};
} catch (error) {
if (error.message === 'Authentication required') {
return {
content: [{
type: "text",
text: "Authentication required. Please use the 'authenticate' tool first."
}]
};
}
return {
content: [{
type: "text",
text: `Error sending email: ${error.message}`
}]
};
}
}
module.exports = handleSendEmail;