=encoding utf8

=head1 NAME

Mail::Transport::POP3 - receive messages via POP3

=head1 INHERITANCE

 Mail::Transport::POP3
   is a Mail::Transport::Receive
   is a Mail::Transport
   is a Mail::Reporter

=head1 SYNOPSIS

  my $receiver = Mail::Transport::POP3->new(...);
  my $message = $receiver->receive($id);

=head1 DESCRIPTION

Receive messages via the POP3 protocol from one remote server, as specified
in rfc1939.  This object hides much of the complications in the protocol and
recovers broken connections automatically.  Although it is part of the
MailBox distribution, this object can be used separately.

You probably should B<not use this> module, but L<Mail::Box::POP3|Mail::Box::POP3>.  This
module is the interface to POP3, whereas L<Mail::Box::POP3|Mail::Box::POP3> hides the
protocol weirdness and works as any other mail folder.

Extends L<"DESCRIPTION" in Mail::Transport::Receive|Mail::Transport::Receive/"DESCRIPTION">.

=head1 METHODS

Extends L<"METHODS" in Mail::Transport::Receive|Mail::Transport::Receive/"METHODS">.

=head2 Constructors

Extends L<"Constructors" in Mail::Transport::Receive|Mail::Transport::Receive/"Constructors">.

=over 4

=item $class-E<gt>B<new>(%options)

Create a new pop3 server connection.  One object can only handle one
connection: for a single user to one single server.  If the server
could not be reached, or when the login fails, this instantiating C<new>
will return C<undef>.
Improves base, see L<Mail::Reporter/"Constructors">

 -Option      --Defined in     --Default
  authenticate                   'AUTO'
  executable    Mail::Transport  undef
  hostname      Mail::Transport  'localhost'
  interval      Mail::Transport  30
  password      Mail::Transport  undef
  port          Mail::Transport  110
  proxy         Mail::Transport  undef
  retry         Mail::Transport  <unlimited>
  ssl_options                    undef
  timeout       Mail::Transport  120
  use_ssl                        false
  username      Mail::Transport  undef
  via           Mail::Transport  'sendmail'

=over 2

=item authenticate => 'LOGIN'|'APOP'|'AUTO'|'OAUTH2'|'OAUTH2_SEP'

Authenthication method.  The standard defines two methods, named LOGIN and
APOP.  The first sends the username and password in plain text to the server
to get permission, the latter encrypts this data using MD5.  When AUTO is
used, first APOP is tried, and then LOGIN.

OAUTH* requires the authorization token to be passed as Password.
Microsoft Office365 needs C<OAUTH2_SEP>, where other oauth2
implementations use C<OAUTH2>.

=item executable => $file

=item hostname => $host|\@hosts

=item interval => $span

=item password => $password

=item port => $port

=item proxy => $path

=item retry => $count|undef

=item ssl_options => HASH

Unless overruled, C<verify_hostname> will be set to C<false> and
C<SSL_verify_mode> to C<SSL_VERIFY_NONE}>

You can also set the SSL parameters via IO::Socket::SSL subroutine set_defaults.
Connections will get restarted when they are lost: you have to keep the
defaults in place during POP actions.

=item timeout => $span

=item use_ssl => BOOLEAN

=item username => $username

=item via => CLASS|$name

=back

=back

=head2 Attributes

Extends L<"Attributes" in Mail::Transport::Receive|Mail::Transport::Receive/"Attributes">.

=over 4

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

Z<>

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

Z<>

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

Returns C<true> when SSL must be used.

=back

=head2 Receiving mail

Extends L<"Receiving mail" in Mail::Transport::Receive|Mail::Transport::Receive/"Receiving mail">.

=over 4

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

Inherited, see L<Mail::Transport::Receive/"Receiving mail">

=back

=head2 Exchanging information

=over 4

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

Mark all messages that have been fetched with L<message()|Mail::Transport::POP3/"Exchanging information"> for deletion.
See L<fetched()|Mail::Transport::POP3/"Exchanging information">.

=item $obj-E<gt>B<deleted>(BOOLEAN, @ids)

Either mark the specified message(s) to be deleted on the remote server or
unmark them for deletion (if the first parameter is C<false>).  Deletion of
messages will take place B<only> when the connection is specifically
disconnected or the last reference to the object goes out of scope.

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

Break contact with the server, if that (still) exists.  Returns C<true> if
successful.  Please note that even if the disconnect was not successful,
all knowledge of messages etc. will be removed from the object: the object
basically has reverted to the state in which it was before anything was done
with the mail box.

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

Returns a reference to a list of ID's that have been fetched using
L<message()|Mail::Transport::POP3/"Exchanging information">.  This can be used to update a database of messages that
were fetched (but maybe not yet deleted) from the mailbox.

Please note that if the POP3 server did not support the UIDL command, this
method will always return C<undef> because it is not possibly to reliably
identify messages between sessions (other than looking at the contents of
the messages themselves).

See also L<deleteFetched()|Mail::Transport::POP3/"Exchanging information">.

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

Returns the total number of octets used by the mailbox on the remote server.

=item $obj-E<gt>B<header>( $id, [$bodylines] )

Returns an ARRAY which contains the header of the message with the
specified C<$id>.  A C<undef> is returned if something has gone wrong.

The optional integer C<$bodylines> specifies the number of lines from the body
which should be added, by default none.

» example: 

  my $ref_lines = $pop3->header($uidl);
  print @$ref_lines;

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

Translates the unique C<$id> of a message into a sequence number which
represents the message as long a this connection to the POP3 server
exists.  When the message has been deleted for some reason, C<undef>
is returned.

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

Returns a list (in list context) or a reference to a list (in scalar context)
of all IDs which are known by the server on this moment.

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

Returns a reference to an array which contains the lines of the
message with the specified C<$id>.  Returns C<undef> if something has gone
wrong.

» example: 

  my $ref_lines = $pop3->message($uidl);
  print @$ref_lines;

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

Returns the size of the message which is indicated by the C<$id>, in octets.
If the message has been deleted on the remote server, this will return
C<undef>.

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

Returns (in scalar context only) the number of messages that are known
to exist in the mailbox.

=back

=head2 Protocol internals

The follow methods handle protocol internals, and should not be used
by a normal user of this class.

=over 4

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

Establish a new connection to the POP3 server, using username and password.

=item $obj-E<gt>B<send>($socket, $data)

Send C<$data> to the indicated socket and return the first line read from
that socket.  Logs an error if either writing to or reading from socket failed.

This method does B<not> attempt to reconnect or anything: if reading or
writing the socket fails, something is very definitely wrong.

=item $obj-E<gt>B<sendList>($socket, $command)

Sends the indicated C<$command> to the specified socket, and retrieves the
response.  It returns a reference to an array with all the lines that
were reveived after the first C<+OK> line and before the end-of-message
delimiter (a single dot on a line).  Returns C<undef>
whenever something has gone wrong.

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

Returns a connection to the POP3 server.  If there was no connection yet,
it will be created transparently.  If the connection with the POP3 server
was lost, it will be reconnected and the assures that internal
state information (STAT and UIDL) is up-to-date in the object.

If the contact to the server was still present, or could be established,
an IO::Socket::INET object is returned.  Else, C<undef> is returned and
no further actions should be tried on the object.

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

Update the current status of folder on the remote POP3 server.

=back

=head2 Server connection

Extends L<"Server connection" in Mail::Transport::Receive|Mail::Transport::Receive/"Server connection">.

=over 4

=item $obj-E<gt>B<findBinary>( $name, [@directories] )

Inherited, see L<Mail::Transport/"Server connection">

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

Inherited, see L<Mail::Transport/"Server connection">

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

Inherited, see L<Mail::Transport/"Server connection">

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

Represent this pop3 connection as URL.

=back

=head2 Error handling

Extends L<"Error handling" in Mail::Transport::Receive|Mail::Transport::Receive/"Error handling">.

=over 4

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

Inherited, see L<Mail::Reporter/"Error handling">

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

Inherited, see L<Mail::Reporter/"Error handling">

=back

=head2 Cleanup

Extends L<"Cleanup" in Mail::Transport::Receive|Mail::Transport::Receive/"Cleanup">.

=over 4

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

Inherited, see L<Mail::Reporter/"Cleanup">

=back

=head1 DIAGNOSTICS

=over 4

=item Error: POP3 Could not do a STAT

For some weird reason, the server does not respond to the STAT call.
Cast by C<status()>

=item Error: POP3 requires a username and password.

No username and/or no password specified for this POP3 folder, although
these are obligatory parts in the protocol.
Cast by C<login()>

=item Fault: cannot connect to $service for POP3: $!

Unsuccessful in connecting to the remote POP3 server.
Cast by C<login()>

=item Error: cannot get all messages of pop3 at once via messages().

It is not possible to retrieve all messages on a remote POP3 folder
at once: each shall be taken separately.  The POP3 folder will hide this
for you.
Cast by C<messages()>

=item Error: cannot re-connect reliably to server which doesn't support UIDL.

The connection to the remote POP3 was lost, and cannot be re-established
because the server's protocol implementation lacks the necessary information.
Cast by C<socket()>

=item Fault: cannot read POP3 from socket: $!

It is not possible to read the success status of the previously given POP3
command.  Connection lost?
Cast by C<send()>

=item Error: cannot write POP3 to socket: $@

It is not possible to send a protocol command to the POP3 server.  Connection lost?
Cast by C<send()>

=item Error: class $package does not implement method $method.

Fatal error: the specific C<$package> (or one of its superclasses) does not
implement this method where it should. This message means that some other
related classes do implement this method however the class at hand does
not.  Probably you should investigate this and probably inform the author
of the package.
Cast by C<notImplemented()>

=item Error: could not authenticate using '$type' method.

The authenication method to get access to the POP3 server did not result in
a connection.  Maybe you need a different authentication protocol, or your
username with password are invalid.
Cast by C<login()>

=item Error: could not authenticate using any login method.

No authentication method was explicitly prescribed, so both AUTH and APOP were
tried.  However, both failed.  There are other authentication methods, which
are not defined by the main POP3 RFC rfc1939.  These protocols are not
implemented yet.  Please contribute your implementation.
Cast by C<login()>

=item Error: server at $service does not seem to be talking POP3.

The remote server did not respond to an initial exchange of messages as is
expected by the POP3 protocol.  The server has probably a different
service on the specified port.
Cast by C<login()>

=back

=head1 SEE ALSO

This module is part of Mail-Box-POP3 version 4.02,
built on April 10, 2026. Website: F<http://perl.overmeer.net/CPAN/>

=head1 LICENSE

For contributors see file ChangeLog.

This software is copyright (c) 2001-2026 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.

