1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
//! In this example a mail is created using the template engine with handlebars bindings
//! (and then printed)
//!
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() {
//TODO[workspace] check if [dir mail and has sub-dir mail]/[dir mail and has parent mail]
// depending on this switch the CWD to the subdir-mail
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> {
// If our "user data" would contain additional inline_emebeddings/attachments
// we would need another "and_then" chain here where we load such parts.
let mut mail = template.render(UserData { name: "Ferris" }.into(), &ctx)?;
// `auto_body` has some nice but failable conversion for **header bodies**.
// Alternatively there is `<HeaderKind>::body(<SomeHeaderBodyType>)` which
// is not failable.
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);
}
|