You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP中如何检测.msg格式文件是否包含附件?

MSG邮件文件附件存在性检测方案

以下是不同技术栈下的轻量实现,均仅读取文件元信息、不提取附件内容,直接返回布尔值结果,性能符合上传后即时校验的需求:

  • Python 实现
    依赖extract-msg第三方库,安装命令:pip install extract-msg
import extract_msg

def check_msg_has_attachment(msg_file_path: str) -> bool:
    with extract_msg.Message(msg_file_path, delayAttachments=True) as msg:
        return len(msg.attachments) > 0

新增delayAttachments=True参数可进一步跳过附件预加载,提升检测速度

  • C# /.NET 实现
    依赖OpenMcdfNuGet包,安装命令:Install-Package OpenMcdf
using OpenMcdf;
using System.IO;

public static bool CheckMsgHasAttachment(string msgFilePath)
{
    using var fileStream = File.OpenRead(msgFilePath);
    using var compoundFile = new CompoundFile(fileStream);
    // MSG格式的附件均存储在以__attach_version1.0_前缀命名的存储项中
    return compoundFile.RootStorage.Children.Any(c => c.Name.StartsWith("__attach_version1.0_"));
}

该实现直接解析MSG底层的OLE复合文件结构,无需加载完整邮件内容,检测速度最快

  • Node.js 实现
    依赖@kenjiuno/msgreader第三方包,安装命令:npm install @kenjiuno/msgreader
const MsgReader = require('@kenjiuno/msgreader');
const fs = require('fs');

function checkMsgHasAttachment(msgFilePath) {
    const msgBuffer = fs.readFileSync(msgFilePath);
    const msgReader = new MsgReader(msgBuffer);
    const mailMeta = msgReader.getFileData();
    return !!mailMeta.attachments?.length;
}

所有实现的返回值直接为true/false布尔值,满足无需提取附件内容的要求,单文件检测耗时普遍低于10ms。

内容的提问来源于stack exchange,提问作者Elias

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 14:57:02