使用 mailparse 时二进制附件损坏 ParsedMail::get_body
Binary attachments are corrupted when using mailparse's ParsedMail::get_body
我正在尝试使用 Rust 解析保存为 MIME 文件的电子邮件。我能够提取 body 和附件。当附件是 CSV 文件时,一切正常。当文件是 PDF 或 XLSX 文件时,保存的文件已损坏。我怀疑编码有问题,因为当我检查 headers 我得到
Content-Type = "application/vnd.openxmlformats officedocument.spreadsheetml.sheet"
这是我的代码,适用于 CSV 但不适用于 XLSX:
extern crate base64;
extern crate mailparse;
use mailparse::*;
use std::fs::File;
use std::string::*;
use std::io::prelude::*;
fn main() {
let mut file = File::open("test_mail").unwrap();
let mut contents = String::new();
let _silent = file.read_to_string(&mut contents);
let parsed = parse_mail(contents.as_bytes()).unwrap();
// This is the attached file
let attached_file = parsed.subparts[2].get_body().unwrap();
// Write the file
let mut out_file = File::create("out_file.xlsx").unwrap();
out_file.write_all(attached_file.as_bytes()).unwrap();
println!("Done")
}
我正在使用 Rust 1.23.0、Cargo 0.24.0,并且我在 Debian 上运行。
ParsedMail::get_body
仅适用于文本数据(可以转换为 unicode 字符串)。您要使用 ParsedMail::get_body_raw
访问二进制附件:
let attached_file = parsed.subparts[2].get_body()_raw.unwrap();
// Write the file
let mut out_file = File::create("out_file.xlsx").unwrap();
out_file.write_all(attached_file).unwrap();
我正在尝试使用 Rust 解析保存为 MIME 文件的电子邮件。我能够提取 body 和附件。当附件是 CSV 文件时,一切正常。当文件是 PDF 或 XLSX 文件时,保存的文件已损坏。我怀疑编码有问题,因为当我检查 headers 我得到
Content-Type = "application/vnd.openxmlformats officedocument.spreadsheetml.sheet"
这是我的代码,适用于 CSV 但不适用于 XLSX:
extern crate base64;
extern crate mailparse;
use mailparse::*;
use std::fs::File;
use std::string::*;
use std::io::prelude::*;
fn main() {
let mut file = File::open("test_mail").unwrap();
let mut contents = String::new();
let _silent = file.read_to_string(&mut contents);
let parsed = parse_mail(contents.as_bytes()).unwrap();
// This is the attached file
let attached_file = parsed.subparts[2].get_body().unwrap();
// Write the file
let mut out_file = File::create("out_file.xlsx").unwrap();
out_file.write_all(attached_file.as_bytes()).unwrap();
println!("Done")
}
我正在使用 Rust 1.23.0、Cargo 0.24.0,并且我在 Debian 上运行。
ParsedMail::get_body
仅适用于文本数据(可以转换为 unicode 字符串)。您要使用 ParsedMail::get_body_raw
访问二进制附件:
let attached_file = parsed.subparts[2].get_body()_raw.unwrap();
// Write the file
let mut out_file = File::create("out_file.xlsx").unwrap();
out_file.write_all(attached_file).unwrap();