=encoding utf8

=head1 NAME

Mail::Identity - an e-mail role

=head1 INHERITANCE

 Mail::Identity
   is a User::Identity::Item

=head1 SYNOPSIS

  use User::Identity;
  use Mail::Identity;
  my $me   = User::Identity->new(...);
  my $addr = Mail::Identity->new(address => 'x@y');
  $me->add(email => $addr);

  # Simpler
  use User::Identity;
  my $me   = User::Identity->new(...);
  my $addr = $me->add(email => 'x@y');
  my $addr = $me->add(email => 'home', address => 'x@y');

  # Conversion
  my $ma   = Mail::Address->new(...);
  my $mi   = Mail::Identity->coerce($ma);

=head1 DESCRIPTION

The C<Mail::Identity> object contains the description of role played by
a human when sending e-mail.  Most people have more than one role these
days: for instance, a private and a company role with different e-mail
addresses.

An C<Mail::Identity> object combines an e-mail address, user description
("phrase"), a signature, pgp-key, and so on.  All fields are optional,
and some fields are smart.  One such set of data represents one role.
C<Mail::Identity> is therefore the smart cousine of the Mail::Address
object.

Extends L<"DESCRIPTION" in User::Identity::Item|User::Identity::Item/"DESCRIPTION">.

=head1 METHODS

Extends L<"METHODS" in User::Identity::Item|User::Identity::Item/"METHODS">.

=head2 Constructors

Extends L<"Constructors" in User::Identity::Item|User::Identity::Item/"Constructors">.

=over 4

=item $class-E<gt>B<new>( [$name], %options )

Z<>

 -Option      --Defined in          --Default
  address                             <usernameP<@domain> or name>
  charset                             <user's charset>
  comment                             <user's fullname if phrase is different>
  description   User::Identity::Item  undef
  domain                              <from email or localhost>
  language                            <from user>
  location                            <random user's location>
  name          User::Identity::Item  <phrase or user's fullName>
  organization                        <location's organization>
  parent        User::Identity::Item  undef
  pgp_key                             undef
  phrase                              <user's fullName>
  signature                           undef
  username                            <from address or user's nickname>

=over 2

=item address => STRING

The e-mail address is constructed from the username/domain, but
when both do not exist, the name is taken.

=item charset => STRING

=item comment => STRING

=item description => STRING

=item domain => STRING

=item language => STRING

=item location => $name|OBJECT

The user's location which relates to this mail identity.  This can be
specified as location C<$name> (which will be looked-up when needed), or
as L<User::Identity::Location|User::Identity::Location> object.

=item name => STRING

=item organization => STRING

Usually defined for e-mail addresses which are used by a company or
other organization, but less common for personal addresses.  This
value will be used to fill the C<Organization> header field of messages.

=item parent => OBJECT

=item pgp_key => STRING|FILENAME

=item phrase => STRING

=item signature => STRING

=item username => STRING

=back

=back

=head2 Attributes

Extends L<"Attributes" in User::Identity::Item|User::Identity::Item/"Attributes">.

=over 4

=item $obj-E<gt>B<address>()

Returns the e-mail address for this role.  If none was specified, it will
be constructed from the username and domain.  If those are not present
as well, then the L<name()|User::Identity::Item/"Attributes"> is used when it contains a C<@>, else the
user's nickname is taken.

=item $obj-E<gt>B<charset>()

Returns the character set used in comment and phrase.  When set to
C<undef>, the strings (are already encoded to) contain only ASCII
characters.  This defaults to the value of the user's charset, if a user
is defined.

=item $obj-E<gt>B<comment>( [STRING] )

E-mail address -when included in message MIME headers- can contain a comment.
The RFCs advice not to store useful information in these comments, but it
you really want to, you can do it.  The comment defaults to the user's
fullname if the phrase is not the fullname and there is a user defined.

Comments will be enclosed in parenthesis when used. Parenthesis (matching)
or non-matching) which are already in the string will carefully escaped
when needed.  You do not need to worry.

=item $obj-E<gt>B<description>()

Inherited, see L<User::Identity::Item/"Attributes">

=item $obj-E<gt>B<domain>()

The domain is the part of the e-mail address after the C<@>-sign.
When this is not defined, it can be deducted from the email address
(see L<address()|Mail::Identity/"Attributes">).  If nothing is known, C<localhost> is returned.

=item $obj-E<gt>B<language>()

Returns the language which is used for the description fields of this
e-mail address, which defaults to the user's language.

=item $obj-E<gt>B<location>()

Returns the object which describes to which location this mail address relates.
The location may be used to find the name of the organization involved, or
to create a signature.  If no location is specified, but a user is defined
which has locations, one of those is randomly chosen.

=item $obj-E<gt>B<name>( [$newname] )

Inherited, see L<User::Identity::Item/"Attributes">

=item $obj-E<gt>B<organization>()

Returns the organization which relates to this e-mail identity.  If not
explicitly specified, it is tried to be found via the location.

=item $obj-E<gt>B<phrase>()

The phrase is used in an e-mail address to explain who is sending the
message.  This usually is the fullname (the user's fullname is used by
default), description of your function (Webmaster), or any other text.

When an email string is produced, the phase will be quoted if needed.
Quotes which are within the string will automatically be escaped, so
you do no need to worry: input cannot break the outcome!

=item $obj-E<gt>B<username>()

Returns the username of this e-mail address.  If none is specified, first
it is tried to extract it from the specified e-mail address.  If there is
also no username in the e-mail address, the user identity's nickname is
taken.

=back

=head2 Collections

Extends L<"Collections" in User::Identity::Item|User::Identity::Item/"Collections">.

=over 4

=item $obj-E<gt>B<add>($collection, $role)

Inherited, see L<User::Identity::Item/"Collections">

=item $obj-E<gt>B<addCollection>( $object | <[$type], %options> )

Inherited, see L<User::Identity::Item/"Collections">

=item $obj-E<gt>B<collection>($name)

Inherited, see L<User::Identity::Item/"Collections">

=item $obj-E<gt>B<parent>( [$parent] )

Inherited, see L<User::Identity::Item/"Collections">

=item $obj-E<gt>B<removeCollection>($object|$name)

Inherited, see L<User::Identity::Item/"Collections">

=item $any-E<gt>B<type>()

Inherited, see L<User::Identity::Item/"Collections">

=item $obj-E<gt>B<user>()

Inherited, see L<User::Identity::Item/"Collections">

=back

=head2 Searching

Extends L<"Searching" in User::Identity::Item|User::Identity::Item/"Searching">.

=over 4

=item $obj-E<gt>B<find>($collection, $role)

Inherited, see L<User::Identity::Item/"Searching">

=back

=head1 DIAGNOSTICS

=over 4

=item Error: cannot load collection module for $type ($class): $@

Either the specified C<$type> does not exist, or that module named C<$class> returns
compilation errors.  If the type as specified in the warning is not
the name of a package, you specified a nickname which was not defined.
Maybe you forgot the 'require' the package which defines the nickname.
Cast by C<addCollection()>

=item Error: nvalid collection $name.

The collection with C<$name> does not exist and can not be created.
Cast by C<add()>

=item Error: this $object is not a collection.

Cast by C<addCollection()>

=back

=head1 SEE ALSO

This module is part of User-Identity version 4.00,
built on December 12, 2025. Website: F<http://perl.overmeer.net/CPAN/>

=head1 LICENSE

For contributors see file ChangeLog.

This software is copyright (c) 2003-2025 by Mark Overmeer.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

