html规范eml文件,eml 文件头解析

先简单说明下 eml 的格式:

邮件是由邮件头和邮件体构成,邮件体又可能由文本、超文本和附件构成,他们之间是通过 boundary 隔开,在每个 boundary 区域,可以看做由 header + value 组成。很显然,可以使用递归解析一个eml文件。

eml 模板:

Date: Fri, 25 Oct 2013 12:36:37 +0800

From: "kitty"

To: dbliu@eyou.net

Subject: =?gb2312?B?eXJkeQ==?=

X-mailer: cnpcmail 1.0.0.5 fix3_2013.10.17 [CN]

Mime-Version: 1.0

Content-Type: multipart/mixed;

boundary="4664686832757102513123637"

This is a multi-part message in MIME format.

--4664686832757102513123637

Content-Type: multipart/alternative;

boundary="7781842894210/25/131236372380515350"

--7781842894210/25/131236372380515350

Content-Type: multipart/alternative;

boundary="19629187561393110/25/131236372462616118"

--19629187561393110/25/131236372462616118

Content-Type: text/plain;

charset="gb2312"

Content-Transfer-Encoding: base64

ZGY=

--19629187561393110/25/131236372462616118

Content-Type: text/html;

charset="gb2312"

Content-Transfer-Encoding: base64

PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv

L0VOIj4NCjxIVE1MPjxIRUFEPjxUSVRMRT48L1RJVExFPg0KPE1FVEEgY29udGVudD0idGV4dC9o

dG1sOyBjaGFyc2V0PWdiMjMxMiIgaHR0cC1lcXVpdj1Db250ZW50LVR5cGU+DQo8TUVUQSBuYW1l

PUdFTkVSQVRPUiBjb250ZW50PSJNU0hUTUwgMTAuMDAuOTIwMC4xNjcyMSI+PC9IRUFEPg0KPEJP

RFk+DQo8RElWPjxGT05UIHNpemU9MiBmYWNlPc6iyO3RxbraPmRmPC9GT05UPjwvRElWPjwvQk9E

WT48L0hUTUw+DQo=

--19629187561393110/25/131236372462616118--

--7781842894210/25/131236372380515350--

--4664686832757102513123637

Content-Type: application/mixed; name="=?gb2312?B?U0hPUlRDVVQuRVhF?="

Content-Transfer-Encoding: base64

Content-Disposition: attachment; filename="=?gb2312?B?U0hPUlRDVVQuRVhF?="

TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

AAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1v

--4664686832757102513123637--

eml 文件解析源码:

#include

#include

#include

using namespace std;

int EmailHeader(ifstream &ifile,string &header)

{

//get header

while (ifile.eof() == false)

{

char outbuffer[1024] = {0};

ifile.getline(outbuffer,1024,'\n');

//std::cout << outbuffer << std::endl;

header += outbuffer;

header += "\n";

if (strcmp(outbuffer,"\r") == 0)

break;

}

return 0;

}

int ProcessHeader(const string &header)

{

std::cout << header << std::endl;

return 0;

}

int ExstractBoundaryFrom(const string &header,string &boundary)

{

size_t pos = header.find("boundary",0);

if (pos == string::npos)

return 1;

boundary = header.substr(pos);

pos = boundary.find("=",0);

boundary = boundary.substr(pos + 1);

//remove \" symbol

size_t s1 = boundary.find("\"",0);

size_t s2 = string::npos;

if (s1 != string::npos)

s2 = boundary.find("\"",s1 + 1);

if (s1 != string::npos && s2 != string::npos)

boundary = boundary.substr(s1 + 1,s2 - s1 - 1);

return 0;

}

bool hasBoundary(const string &header)

{

bool bHas = (header.find("boundary") == string::npos) ? false : true;

return bHas;

}

/*

note: boundary dosen't include any '\r','\n' and '\r\n'

*/

bool isBoundaryEnd(const string &boundary,const string &contentline)

{

bool bEnd = false;

string temp = "--";

temp += boundary;

temp += "--";

if (contentline.find(temp) != string::npos)

bEnd = true;

return bEnd;

}

/*

note: boundary dosen't include any '\r','\n' and '\r\n'

*/

bool isBoundaryBegin(const string &boundary,const string &contentline)

{

bool bBegin = false;

string temp = "--";

temp += boundary;

if (contentline.find(temp) != string::npos)

bBegin = true;

return bBegin;

}

int parseBoundaryZone(const string &boundary,ifstream &ifile)

{

if (boundary.empty())

return 1;

while(ifile.eof() == false)

{

char line[4096] = {0};

ifile.getline(line,4096,'\n');

if (isBoundaryBegin(boundary,line))

{

string header;

EmailHeader(ifile,header);

ProcessHeader(header);

if (hasBoundary(header))

{

string bound;

if (0 == ExstractBoundaryFrom(header,bound))

parseBoundaryZone(bound,ifile);

}

}

if (isBoundaryEnd(boundary,line))

break;

}

return 0;

}

string parseEmailFrom(ifstream &ifile)

{

//get header

string emailheader;

EmailHeader(ifile,emailheader);

std::cout << emailheader << std::endl;

if (hasBoundary(emailheader))

{

string boundary;

ExstractBoundaryFrom(emailheader,boundary);

std::cout << boundary << std::endl;

std::cout << "begin parse boudary zone ......" << std::endl;

parseBoundaryZone(boundary,ifile);

}

else

{

//print body text/plan

}

return "";

}

int _tmain(int argc, _TCHAR* argv[])

{

ifstream infile("C:\\Users\\db liu\\Desktop\\new3.txt",std::ifstream::binary);

parseEmailFrom(infile);

infile.close();

return 0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要批量读取多个 EML 文件,你可以使用 `glob` 模块来获取指定目录下的所有 EML 文件文件路径,然后使用 `email.parser` 模块来解析每个 EML 文件。 以下是一个示例代码,演示如何批量读取 EML 文件解析它们: ```python import glob from email.parser import Parser # 指定包含 EML 文件的目录路径 eml_dir = 'path/to/eml/files' # 获取目录下所有的 EML 文件路径 eml_files = glob.glob(eml_dir + '/*.eml') # 创建一个解析器对象 parser = Parser() # 遍历每个 EML 文件解析 for eml_file in eml_files: # 打开 EML 文件并读取内容 with open(eml_file, 'r') as f: eml_text = f.read() # 解析 EML 文件 email_object = parser.parsestr(eml_text) # 现在可以访问解析后的电子邮件对象的各个部分了 print('发件人:', email_object['From']) print('主题:', email_object['Subject']) print('正文:', email_object.get_payload()) print('---') # 用于分隔不同的 EML 文件输出 ``` 在上面的示例中,我们首先使用 `glob` 模块获取指定目录下的所有以 `.eml` 结尾的文件路径。然后,我们遍历每个文件,并打开它们,读取文件内容。接下来,我们使用 `Parser` 对象解析每个 EML 文件,并访问解析后的电子邮件对象的各个部分,如发件人、主题和正文。 请确保将 `eml_dir` 替换为你实际存储 EML 文件的目录路径。 希望这个示例对你有帮助!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值