#[non_exhaustive]pub struct Address {
pub name: Option<String>,
pub email: String,
}Expand description
An email address with optional display name.
§References
- RFC 5322 Section 3.4 (address specification)
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.name: Option<String>Display name, decoded from RFC 2047 encoded words if present.
email: StringEmail address (addr-spec form).
Implementations§
Source§impl Address
impl Address
Sourcepub fn new(email: impl Into<String>) -> Result<Self>
pub fn new(email: impl Into<String>) -> Result<Self>
Creates an address with only an email, validating the syntax.
§Errors
Returns crate::Error::InvalidAddress if the email is not valid
per RFC 5322 Section 3.4.
Sourcepub fn with_name(
name: impl Into<String>,
email: impl Into<String>,
) -> Result<Self>
pub fn with_name( name: impl Into<String>, email: impl Into<String>, ) -> Result<Self>
Creates an address with a display name and email, validating the syntax.
§Errors
Returns crate::Error::InvalidAddress if the email is not valid
per RFC 5322 Section 3.4.
Sourcepub fn new_unchecked(name: Option<String>, email: String) -> Self
pub fn new_unchecked(name: Option<String>, email: String) -> Self
Creates an Address without validating the email syntax.
§You almost certainly want Address::new or Address::with_name instead
Those constructors validate the email address against RFC 5322
Section 3.4 and reject malformed input. An Address built with
new_unchecked may contain syntax that is invalid for outgoing
mail, which can cause:
- Rejected
RCPT TOcommands at the SMTP layer. - Malformed
From/To/Ccheaders that downstream MTAs or MUAs cannot parse. - Messages that silently disappear into spam filters or bounce queues.
§When this method is appropriate
This exists for parser and protocol-conversion code that must
accept addresses already received from the network, where Postel’s
law applies (RFC 5322 Section 3.4): be liberal in what you accept.
For example, an IMAP ENVELOPE address or a From: header parsed
from an incoming message may contain syntax that is technically
non-conformant but still meaningful. Rejecting it would discard
data the user needs to see.
If you are building a new outgoing message, use Address::new
or Address::with_name — they enforce the same validation the
message builder applies and will catch mistakes at construction
time rather than at send time.
Trait Implementations§
Source§impl Display for Address
impl Display for Address
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Formats the address per RFC 5322 Section 3.4.
- Non-ASCII display names are RFC 2047 encoded (RFC 5322 Section 2.2 requires header field bodies to be US-ASCII; RFC 2047 Section 5 defines the encoded-word mechanism for non-ASCII text).
- ASCII display names containing specials are quoted per RFC 5322 Section 3.2.4.
- Without display name: bare
email