pub struct Address {
pub name: Option<String>,
pub email: String,
}Expand description
An email address with an optional display name.
§Examples
use missive::Address;
// From email string
let addr: Address = "user@example.com".into();
assert_eq!(addr.email, "user@example.com");
assert_eq!(addr.name, None);
// From tuple (name, email)
let addr: Address = ("Alice", "alice@example.com").into();
assert_eq!(addr.email, "alice@example.com");
assert_eq!(addr.name, Some("Alice".to_string()));Fields§
§name: Option<String>Optional display name (e.g., “Alice Smith”)
email: StringEmail address (e.g., “alice@example.com”)
Implementations§
Source§impl Address
impl Address
Sourcepub fn new(email: impl Into<String>) -> Self
pub fn new(email: impl Into<String>) -> Self
Create a new address with just an email.
This performs a basic sanity check (non-empty, contains @) and logs
a warning if the email looks invalid. For strict validation, use
Address::parse instead.
Sourcepub fn with_name(name: impl Into<String>, email: impl Into<String>) -> Self
pub fn with_name(name: impl Into<String>, email: impl Into<String>) -> Self
Create a new address with a name and email.
This performs a basic sanity check (non-empty, contains @) and logs
a warning if the email looks invalid. For strict validation, use
Address::parse_with_name instead.
Sourcepub fn parse(email: &str) -> Result<Self, MailError>
pub fn parse(email: &str) -> Result<Self, MailError>
Parse and validate an email address.
Uses RFC 5321/5322 compliant validation. Returns an error if the email address is invalid.
§Examples
use missive::Address;
// Valid address
let addr = Address::parse("user@example.com").unwrap();
assert_eq!(addr.email, "user@example.com");
// Invalid address
assert!(Address::parse("not-an-email").is_err());
assert!(Address::parse("").is_err());Sourcepub fn parse_with_name(name: &str, email: &str) -> Result<Self, MailError>
pub fn parse_with_name(name: &str, email: &str) -> Result<Self, MailError>
Parse and validate an email address with a display name.
§Examples
use missive::Address;
let addr = Address::parse_with_name("Alice", "alice@example.com").unwrap();
assert_eq!(addr.email, "alice@example.com");
assert_eq!(addr.name, Some("Alice".to_string()));
// Invalid email
assert!(Address::parse_with_name("Alice", "not-valid").is_err());Sourcepub fn to_ascii(&self) -> Result<String, MailError>
pub fn to_ascii(&self) -> Result<String, MailError>
Convert the domain part of the email address to ASCII (Punycode).
This is useful for international domain names (IDN) that contain non-ASCII characters. The local part (before @) is preserved as-is.
§Examples
use missive::Address;
// Japanese domain
let addr = Address::new("user@例え.jp");
assert_eq!(addr.to_ascii().unwrap(), "user@xn--r8jz45g.jp");
// Already ASCII domain
let addr = Address::new("user@example.com");
assert_eq!(addr.to_ascii().unwrap(), "user@example.com");Sourcepub fn formatted_ascii(&self) -> Result<String, MailError>
pub fn formatted_ascii(&self) -> Result<String, MailError>
Format with ASCII-encoded domain (Punycode for IDN).
Like formatted() but converts international domain names to ASCII.
Use this when sending emails through SMTP or other protocols that
require ASCII domain names.
§Examples
use missive::Address;
let addr = Address::with_name("User", "user@例え.jp");
assert_eq!(addr.formatted_ascii().unwrap(), "User <user@xn--r8jz45g.jp>");Sourcepub fn formatted_rfc5322_ascii(&self) -> Result<String, MailError>
pub fn formatted_rfc5322_ascii(&self) -> Result<String, MailError>
Format according to RFC 5322 with ASCII-encoded domain.
Combines RFC 5322 escaping with IDN/Punycode conversion.
Sourcepub fn formatted(&self) -> String
pub fn formatted(&self) -> String
Format as “Name
For simple names without special characters, returns Name <email>.
For names with special chars, use formatted_rfc5322() for proper quoting.
Sourcepub fn formatted_rfc5322(&self) -> String
pub fn formatted_rfc5322(&self) -> String
Format according to RFC 5322 with proper escaping.
This method:
- Escapes backslashes:
\→\\ - Escapes double quotes:
"→\" - Wraps the name in double quotes:
"Name" <email>
This is the format that should be used in email headers.