~asimpson/mailpreview-cli

a44d951b13c5bb833f7e6186614684c0dc3300e1 — Adam Simpson 2 years ago
feat: init commit
3 files changed, 126 insertions(+), 0 deletions(-)

A Cargo.lock
A Cargo.toml
A src/main.rs
A  => Cargo.lock +71 -0
@@ 1,71 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "base64"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e"
dependencies = [
 "byteorder",
]

[[package]]
name = "base64"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff"

[[package]]
name = "byteorder"
version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"

[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"

[[package]]
name = "charset"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f426e64df1c3de26cbf44593c6ffff5dbfd43bbf9de0d075058558126b3fc73"
dependencies = [
 "base64 0.10.1",
 "encoding_rs",
]

[[package]]
name = "encoding_rs"
version = "0.8.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8ac63f94732332f44fe654443c46f6375d1939684c17b0afb6cb56b0456e171"
dependencies = [
 "cfg-if",
]

[[package]]
name = "mailparse"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479b94621ea0fe875638d27f4a0b68213174b63e1ff9355d0948a04f71a5055a"
dependencies = [
 "base64 0.12.3",
 "charset",
 "quoted_printable",
]

[[package]]
name = "mailpreview-cli"
version = "0.1.0"
dependencies = [
 "mailparse",
]

[[package]]
name = "quoted_printable"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47b080c5db639b292ac79cbd34be0cfc5d36694768d8341109634d90b86930e2"

A  => Cargo.toml +20 -0
@@ 1,20 @@
[package]
name = "mailpreview-cli"
version = "0.1.0"
authors = ["Adam Simpson <adam@heysparkbox.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
mailparse = "0.13.0"

[profile.release]
opt-level = 'z'
debug = false
lto = true
debug-assertions = false
codegen-units = 1
panic = 'abort'
incremental = false
overflow-checks = false

A  => src/main.rs +35 -0
@@ 1,35 @@
use mailparse::*;
use std::fs::File;
use std::io::prelude::*;
use std::io::Read;

fn main() {
    let file = std::env::args().nth(1).expect("mail message to parse");
    let mut file_handle = File::open(file.trim()).expect("file handle to contents of message");
    let mut contents = String::new();

    file_handle
        .read_to_string(&mut contents)
        .expect("actual message content");
    let mail = parse_mail(contents.as_bytes()).expect("parsed mail struct");
    let message_id = mail
        .headers
        .get_first_value("Message-ID")
        .expect("Message ID");
    let file_name = format!("/tmp/{}", message_id);
    let mut tmp_file = File::create(&file_name).expect("tmp file created");
    let mut body = "".to_string();

    if mail.subparts.len() > 0 {
        for m in mail.subparts.iter() {
            if m.ctype.mimetype == "text/plain" {
                body = m.get_body().expect("message content");
            }
        }
    } else {
        body = mail.get_body().expect("message content");
    }

    println!("{}", &file_name);
    tmp_file.write_all(body.as_bytes()).expect("file created");
}