feat(builder): Add helper methods for attachments and text (#618)

This commit is contained in:
Alexis Mousset
2021-05-14 16:59:08 +02:00
committed by GitHub
parent 94cae6df0d
commit 904789ac3d
3 changed files with 133 additions and 42 deletions

View File

@@ -1,7 +1,7 @@
use std::io::Write;
use crate::message::{
header::{ContentTransferEncoding, ContentType, Header, Headers},
header::{self, ContentTransferEncoding, ContentType, Header, Headers},
EmailFormat, IntoBody,
};
use mime::Mime;
@@ -112,6 +112,20 @@ impl SinglePart {
SinglePartBuilder::new()
}
/// Directly create a `SinglePart` from an plain UTF-8 content
pub fn plain<T: IntoBody>(body: T) -> Self {
Self::builder()
.header(header::ContentType::TEXT_PLAIN)
.body(body)
}
/// Directly create a `SinglePart` from an UTF-8 HTML content
pub fn html<T: IntoBody>(body: T) -> Self {
Self::builder()
.header(header::ContentType::TEXT_HTML)
.body(body)
}
/// Get the headers from singlepart
#[inline]
pub fn headers(&self) -> &Headers {
@@ -330,6 +344,13 @@ impl MultiPart {
MultiPart::builder().kind(MultiPartKind::Signed { protocol, micalg })
}
/// Alias for HTML and plain text versions of an email
pub fn alternative_plain_html<T: IntoBody, V: IntoBody>(plain: T, html: V) -> Self {
Self::alternative()
.singlepart(SinglePart::plain(plain))
.singlepart(SinglePart::html(html))
}
/// Add part to multipart
pub fn part(mut self, part: Part) -> Self {
self.parts.push(part);