pub struct Attachment {
pub filename: String,
pub content_type: String,
pub data: Vec<u8>,
pub path: Option<String>,
pub disposition: AttachmentType,
pub content_id: Option<String>,
pub headers: Vec<(String, String)>,
}Expand description
An email attachment.
Attachments can be created from bytes (eager) or from a file path (lazy). Path-based attachments defer reading until delivery time.
§Examples
use missive::Attachment;
// From bytes (eager - data loaded immediately)
let attachment = Attachment::from_bytes("report.pdf", b"PDF content".to_vec())
.content_type("application/pdf");
// Inline image for HTML emails
let png_bytes = vec![0x89, 0x50, 0x4E, 0x47]; // PNG header bytes
let logo = Attachment::from_bytes("logo.png", png_bytes)
.inline()
.content_id("company-logo");
// Reference in HTML: <img src="cid:company-logo">// From path (lazy - file read at delivery time)
let attachment = Attachment::from_path_lazy("/path/to/large-file.pdf")?;Fields§
§filename: StringFilename for the attachment
content_type: StringMIME content type (e.g., “application/pdf”, “image/png”)
data: Vec<u8>Raw attachment data (empty if using path-based lazy loading)
path: Option<String>File path for lazy loading. If set, data will be read from this path when needed.
disposition: AttachmentTypeWhether this is an inline or regular attachment
content_id: Option<String>Content-ID for inline attachments (used as cid: reference)
headers: Vec<(String, String)>Custom headers for the attachment
Implementations§
Source§impl Attachment
impl Attachment
Sourcepub fn from_bytes(filename: impl Into<String>, data: Vec<u8>) -> Self
pub fn from_bytes(filename: impl Into<String>, data: Vec<u8>) -> Self
Create a new attachment from raw bytes.
Content type is guessed from the filename extension.
Sourcepub fn from_path(path: impl AsRef<Path>) -> Result<Self, MailError>
pub fn from_path(path: impl AsRef<Path>) -> Result<Self, MailError>
Create a new attachment from a file path (eager loading).
Reads the file immediately and guesses the content type from the extension.
Sourcepub fn from_path_lazy(path: impl AsRef<Path>) -> Result<Self, MailError>
pub fn from_path_lazy(path: impl AsRef<Path>) -> Result<Self, MailError>
Create a new attachment from a file path (lazy loading).
This defers reading the file until delivery time. Useful for large files or when the file may be updated between email construction and sending.
The file will be read when get_data() is called.
Sourcepub fn content_type(self, content_type: impl Into<String>) -> Self
pub fn content_type(self, content_type: impl Into<String>) -> Self
Set the content type explicitly.
Sourcepub fn content_id(self, cid: impl Into<String>) -> Self
pub fn content_id(self, cid: impl Into<String>) -> Self
Set the Content-ID for inline attachments.
This is used to reference the attachment in HTML: <img src="cid:your-id">
Sourcepub fn header(self, name: impl Into<String>, value: impl Into<String>) -> Self
pub fn header(self, name: impl Into<String>, value: impl Into<String>) -> Self
Add a custom header to the attachment.
Sourcepub fn get_data(&self) -> Result<Vec<u8>, MailError>
pub fn get_data(&self) -> Result<Vec<u8>, MailError>
Get the attachment data, loading from path if necessary.
For path-based attachments, this reads the file. For byte-based attachments, this returns a clone of the data.
§Errors
AttachmentFileNotFound- File path doesn’t existAttachmentReadError- Failed to read fileAttachmentMissingContent- No data and no path provided
Sourcepub fn base64_data(&self) -> String
pub fn base64_data(&self) -> String
Get the attachment data as base64-encoded string.
For path-based attachments, reads and encodes the file.
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Get the size in bytes.
For path-based attachments, returns 0 (file not loaded yet).
Use get_size() for accurate size of path-based attachments.
Trait Implementations§
Source§impl Clone for Attachment
impl Clone for Attachment
Source§fn clone(&self) -> Attachment
fn clone(&self) -> Attachment
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more