Attachment

Struct Attachment 

Source
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: String

Filename for the attachment

§content_type: String

MIME 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: AttachmentType

Whether 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

Source

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.

Source

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.

Source

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.

Source

pub fn content_type(self, content_type: impl Into<String>) -> Self

Set the content type explicitly.

Source

pub fn inline(self) -> Self

Set as inline attachment (for embedding in HTML).

Source

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">

Source

pub fn header(self, name: impl Into<String>, value: impl Into<String>) -> Self

Add a custom header to the attachment.

Source

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 exist
  • AttachmentReadError - Failed to read file
  • AttachmentMissingContent - No data and no path provided
Source

pub fn base64_data(&self) -> String

Get the attachment data as base64-encoded string.

For path-based attachments, reads and encodes the file.

Source

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.

Source

pub fn get_size(&self) -> Result<usize, MailError>

Get the accurate size, loading from path if necessary.

Source

pub fn is_lazy(&self) -> bool

Check if this is a path-based (lazy) attachment.

Source

pub fn is_inline(&self) -> bool

Check if this is an inline attachment.

Trait Implementations§

Source§

impl Clone for Attachment

Source§

fn clone(&self) -> Attachment

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Attachment

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Attachment

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Attachment

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,