extern crate mail;
extern crate futures;
extern crate soft_ascii_string;
extern crate serde;
extern crate failure;
use std::{
path::Path
};
use failure::Error;
use futures::Future;
use soft_ascii_string::SoftAsciiString;
use serde::Serialize;
use mail::{
Domain,
MailType,
default_impl::simple_context,
template::{
load_toml_template_from_path,
handlebars::Handlebars,
TemplateExt
},
headers,
HeaderTryFrom,
HeaderKind
};
#[derive(Debug, Serialize)]
struct UserData {
name: &'static str
}
fn main() {
eprintln!(concat!(
"Warning: This example currently only works if mail/mail is the cwd\n",
" i.e. it fails when run from the workspace!"
));
let msg_id_domain = Domain::try_from("company_a.test").unwrap();
let unique_part = SoftAsciiString::from_string("c207n521cec").unwrap();
let ctx = simple_context::new(msg_id_domain, unique_part).unwrap();
let engine = Handlebars::new();
let template_path = Path::new("example_resources/templates/template_a/template.toml").to_owned();
let fut = load_toml_template_from_path(engine, template_path, &ctx)
.and_then(|template| -> Result<_, Error> {
let mut mail = template.render(UserData { name: "Ferris" }.into(), &ctx)?;
mail.insert_header(headers::_From::auto_body(["a@b.example"])?);
mail.insert_header(headers::_To::auto_body(["d@e.example"])?);
Ok(mail)
}).and_then(|mail| mail.into_encodable_mail(ctx.clone()).map_err(Into::into));
let enc_mail = fut.wait().unwrap();
let bytes = enc_mail.encode_into_bytes(MailType::Ascii).unwrap();
let string = String::from_utf8(bytes).unwrap();
println!("{}", string);
}