Skip to main content
Glama

send_email

Send emails with HTML formatting and attachments using the Mail MCP Server. Configure recipients, subject, and content to deliver messages.

Instructions

Send an email with optional HTML body and attachments

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
toYesList of recipient email addresses
subjectYesEmail subject
body_textNoPlain text body
body_htmlNoHTML body
ccNoCC recipients
bccNoBCC recipients
attachmentsNoAttachments (base64 encoded)

Implementation Reference

  • The core logic for sending emails, including parameter validation, email construction, and actual SMTP transmission.
    def send_email(
        client,
        to: list[str],
        subject: str,
        body_text: str | None = None,
        body_html: str | None = None,
        cc: list[str] | None = None,
        bcc: list[str] | None = None,
        attachments: list[Attachment] | None = None,
        from_addr: str | None = None,
    ) -> SendResult:
        """
        发送邮件
    
        Args:
            client: SMTP 客户端对象
            to: 收件人列表 (必填)
            subject: 主题 (必填)
            body_text: 纯文本正文
            body_html: HTML 正文
            cc: 抄送列表
            bcc: 密送列表
            attachments: 附件列表
            from_addr: 发件人地址
    
        Returns:
            SendResult: 发送结果
        """
        # 1. 验证参数
        if not to:
            return SendResult(success=False, error="收件人列表不能为空")
    
        if not subject:
            return SendResult(success=False, error="邮件主题不能为空")
    
        if not body_text and not body_html:
            return SendResult(success=False, error="邮件正文不能为空")
    
        # 验证邮箱地址
        for addr in to:
            if not validate_email_address(addr):
                return SendResult(success=False, error=f"无效的收件人地址: {addr}")
    
        if cc:
            for addr in cc:
                if not validate_email_address(addr):
                    return SendResult(success=False, error=f"无效的抄送地址: {addr}")
    
        if bcc:
            for addr in bcc:
                if not validate_email_address(addr):
                    return SendResult(success=False, error=f"无效的密送地址: {addr}")
    
        try:
            # 获取发件人地址
            if not from_addr:
                if hasattr(client, "config") and hasattr(client.config, "user"):
                    from_addr = client.config.user
                else:
                    return SendResult(success=False, error="缺少发件人地址")
    
            # 2. 构建邮件消息
            message = build_email_message(
                sender=from_addr,
                to=to,
                subject=subject,
                body_text=body_text,
                body_html=body_html,
                cc=cc,
                bcc=bcc,
                attachments=attachments,
            )
    
            # 生成 Message-ID
            message_id = make_msgid()
            message["Message-ID"] = message_id
    
            # 3. 获取所有收件人
            all_recipients = to.copy()
            if cc:
                all_recipients.extend(cc)
            if bcc:
                all_recipients.extend(bcc)
    
            # 4. 发送邮件
            smtp = _get_smtp_client(client)
    
            # 确保连接已建立
            if smtp is None:
                # 尝试连接
                if hasattr(client, "connect"):
                    client.connect()
                    smtp = _get_smtp_client(client)
    
            if smtp is None:
                return SendResult(success=False, error="无法获取 SMTP 连接")
    
            smtp.send_message(message, from_addr=from_addr, to_addrs=all_recipients)
    
            return SendResult(success=True, message_id=message_id, rejected=None)
    
        except smtplib.SMTPAuthenticationError as e:
            return SendResult(success=False, error=f"认证失败: {str(e)}")
        except smtplib.SMTPSenderRefused as e:
            return SendResult(success=False, error=f"发件人被拒绝: {str(e)}")
        except smtplib.SMTPRecipientsRefused as e:
            rejected = list(e.recipients.keys())
            return SendResult(success=False, error=f"收件人被拒绝: {str(e)}", rejected=rejected)
        except smtplib.SMTPException as e:
            return SendResult(success=False, error=f"SMTP 错误: {str(e)}")
        except Exception as e:
            return SendResult(success=False, error=f"发送失败: {str(e)}")

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/AdJIa/mail-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server