Added attach_from_vec function to email builder
This commit is contained in:
@@ -217,7 +217,7 @@ impl EmailBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
/// Adds an attachment to the email
|
||||
/// Adds an attachment to the email from a file
|
||||
pub fn attachment(
|
||||
self,
|
||||
path: &Path,
|
||||
@@ -266,6 +266,32 @@ impl EmailBuilder {
|
||||
Ok(self.message_type(MimeMultipartType::Mixed).child(content))
|
||||
}
|
||||
|
||||
/// Adds an attachment to the email from a vector of bytes.
|
||||
/// This is usefull when your attachment is actually not a file, but a sequence of bytes.
|
||||
pub fn attach_from_vec(
|
||||
self,
|
||||
bytes_vec: Vec<u8>,
|
||||
filename: &str,
|
||||
content_type: &Mime,
|
||||
) -> Result<EmailBuilder, Error> {
|
||||
let body = bytes_vec;
|
||||
|
||||
let actual_filename = filename;
|
||||
|
||||
let encoded_body = base64::encode(&body);
|
||||
let content = PartBuilder::new()
|
||||
.body(encoded_body)
|
||||
.header((
|
||||
"Content-Disposition",
|
||||
format!("attachment; filename=\"{}\"", actual_filename),
|
||||
))
|
||||
.header(("Content-Type", content_type.to_string()))
|
||||
.header(("Content-Transfer-Encoding", "base64"))
|
||||
.build();
|
||||
|
||||
Ok(self.message_type(MimeMultipartType::Mixed).child(content))
|
||||
}
|
||||
|
||||
/// Set the message type
|
||||
pub fn message_type(mut self, message_type: MimeMultipartType) -> EmailBuilder {
|
||||
self.message = self.message.message_type(message_type);
|
||||
|
||||
Reference in New Issue
Block a user