send_email
Send emails with attachments and multiple recipients using the Email MCP Server. Specify recipients, subject, message content (text or HTML), and optional CC, BCC, or attachments to manage email communications effectively.
Instructions
发送邮件
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| attachments | No | 邮件附件列表(可选) | |
| bcc | No | 密送邮箱地址列表(可选) | |
| cc | No | 抄送邮箱地址列表(可选) | |
| html | No | HTML格式邮件内容(可选) | |
| subject | Yes | 邮件主题 | |
| text | Yes | 纯文本邮件内容 | |
| to | Yes | 收件人邮箱地址列表 |
Implementation Reference
- index.js:542-586 (handler)The main handler function for the 'send_email' tool. It destructures the arguments, creates an SMTP transporter using nodemailer, constructs mail options (handling to/cc/bcc as arrays, attachments with base64 content or file paths), sends the email, and returns a success response with the message ID.async sendEmail(args) { const { to, cc, bcc, subject, text, html, attachments } = args; const transporter = this.createSMTPTransporter(); const mailOptions = { from: process.env.EMAIL_USER || process.env.WECHAT_EMAIL_USER, to: Array.isArray(to) ? to.join(', ') : to, cc: cc ? (Array.isArray(cc) ? cc.join(', ') : cc) : undefined, bcc: bcc ? (Array.isArray(bcc) ? bcc.join(', ') : bcc) : undefined, subject, text, html }; // 处理附件 if (attachments && attachments.length > 0) { mailOptions.attachments = attachments.map(att => { if (att.content) { return { filename: att.filename, content: att.content, encoding: 'base64' }; } else if (att.path) { return { filename: att.filename, path: att.path }; } return att; }); } const result = await transporter.sendMail(mailOptions); return { content: [ { type: 'text', text: `邮件发送成功!\n消息ID: ${result.messageId}\n收件人: ${Array.isArray(to) ? to.join(', ') : to}\n主题: ${subject}` } ] }; }
- index.js:169-212 (schema)The input schema definition for the 'send_email' tool, defining properties like to, cc, bcc, subject, text, html, attachments, with required fields: to, subject, text.inputSchema: { type: 'object', properties: { to: { type: 'array', items: { type: 'string' }, description: '收件人邮箱地址列表' }, cc: { type: 'array', items: { type: 'string' }, description: '抄送邮箱地址列表(可选)' }, bcc: { type: 'array', items: { type: 'string' }, description: '密送邮箱地址列表(可选)' }, subject: { type: 'string', description: '邮件主题' }, text: { type: 'string', description: '纯文本邮件内容' }, html: { type: 'string', description: 'HTML格式邮件内容(可选)' }, attachments: { type: 'array', items: { type: 'object', properties: { filename: { type: 'string', description: '附件文件名' }, path: { type: 'string', description: '附件文件路径' }, content: { type: 'string', description: '附件内容(base64编码)' } } }, description: '邮件附件列表(可选)' } }, required: ['to', 'subject', 'text']
- index.js:166-214 (registration)The tool registration in the ListTools response, including name 'send_email', description, and inputSchema.{ name: 'send_email', description: '发送邮件', inputSchema: { type: 'object', properties: { to: { type: 'array', items: { type: 'string' }, description: '收件人邮箱地址列表' }, cc: { type: 'array', items: { type: 'string' }, description: '抄送邮箱地址列表(可选)' }, bcc: { type: 'array', items: { type: 'string' }, description: '密送邮箱地址列表(可选)' }, subject: { type: 'string', description: '邮件主题' }, text: { type: 'string', description: '纯文本邮件内容' }, html: { type: 'string', description: 'HTML格式邮件内容(可选)' }, attachments: { type: 'array', items: { type: 'object', properties: { filename: { type: 'string', description: '附件文件名' }, path: { type: 'string', description: '附件文件路径' }, content: { type: 'string', description: '附件内容(base64编码)' } } }, description: '邮件附件列表(可选)' } }, required: ['to', 'subject', 'text'] } },
- index.js:346-347 (registration)The dispatch case in the CallToolRequestHandler that routes 'send_email' calls to the sendEmail handler method.case 'send_email': return await this.sendEmail(args);
- index.js:377-435 (helper)The helper method createSMTPTransporter() used by sendEmail to create the nodemailer transporter, supporting manual env config or auto-detection of email provider from EMAIL_CONFIGS.createSMTPTransporter() { try { // 如果已经有手动配置的设置,直接使用 if (process.env.EMAIL_SMTP_HOST || process.env.WECHAT_EMAIL_HOST) { const config = { host: process.env.EMAIL_SMTP_HOST || process.env.WECHAT_EMAIL_HOST, port: parseInt(process.env.EMAIL_SMTP_PORT || process.env.WECHAT_EMAIL_PORT) || 587, secure: (process.env.EMAIL_SMTP_SECURE || process.env.WECHAT_EMAIL_SECURE) !== 'false', auth: { user: process.env.EMAIL_USER || process.env.WECHAT_EMAIL_USER, pass: process.env.EMAIL_PASSWORD || process.env.WECHAT_EMAIL_PASSWORD }, connectionTimeout: 30000, greetingTimeout: 30000, socketTimeout: 30000 }; console.log('使用手动配置的SMTP设置:', { host: config.host, port: config.port, secure: config.secure }); return nodemailer.createTransport(config); } // 自动配置 const emailUser = process.env.EMAIL_USER || process.env.WECHAT_EMAIL_USER; const emailType = process.env.EMAIL_TYPE; // 手动指定的邮箱类型 if (!emailUser) { throw new Error('缺少邮箱用户名配置。请设置 EMAIL_USER 环境变量。'); } const provider = this.detectEmailProvider(emailUser, emailType); if (!provider) { throw new Error(`无法识别邮箱类型: ${emailUser}。如果是企业邮箱,请设置 EMAIL_TYPE 环境变量(如: 'exmail' 代表腾讯企业邮箱)`); } const emailConfig = EMAIL_CONFIGS[provider]; const config = { host: emailConfig.smtp.host, port: emailConfig.smtp.port, secure: emailConfig.smtp.secure, auth: { user: emailUser, pass: process.env.EMAIL_PASSWORD || process.env.WECHAT_EMAIL_PASSWORD }, connectionTimeout: 30000, greetingTimeout: 30000, socketTimeout: 30000 }; // 验证必需的配置项 if (!config.auth.pass) { throw new Error('缺少邮箱密码配置。请设置 EMAIL_PASSWORD 环境变量。'); } console.log(`自动配置SMTP设置 - 邮箱类型: ${emailConfig.name}`, { host: config.host, port: config.port, secure: config.secure }); return nodemailer.createTransport(config); } catch (error) { console.error('创建SMTP传输器失败:', error.message); throw error; } }