Email-MessageID
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
lib/Email/MessageID.pm view on Meta::CPAN
use strict;
use warnings;
package Email::MessageID 1.408;
# ABSTRACT: Generate world unique message-ids.
use overload '""' => 'as_string', fallback => 1;
#pod =head1 SYNOPSIS
#pod
#pod use Email::MessageID;
#pod
#pod my $mid = Email::MessageID->new->in_brackets;
#pod
#pod print "Message-ID: $mid\x0D\x0A";
#pod
#pod =head1 DESCRIPTION
#pod
#pod Message-ids are optional, but highly recommended, headers that identify a
#pod message uniquely. This software generates a unique message-id.
#pod
#pod =method new
#pod
#pod my $mid = Email::MessageID->new;
#pod
#pod my $new_mid = Email::MessageID->new( host => $myhost );
#pod
#pod This class method constructs an Email::MessageID object
#pod containing a unique message-id. You may specify custom C<host> and C<user>
#pod parameters.
#pod
#pod By default, the C<host> is generated from C<Sys::Hostname::hostname>.
#pod
#pod By default, the C<user> is generated using C<Time::HiRes>'s C<gettimeofday>
#pod and the process ID.
#pod
#pod Using these values we have the ability to ensure world uniqueness down to
#pod a specific process running on a specific host, and the exact time down to
#pod six digits of microsecond precision.
#pod
#pod =cut
sub new {
my ($class, %args) = @_;
$args{user} ||= $class->create_user;
$args{host} ||= $class->create_host;
my $str = "$args{user}\@$args{host}";
bless \$str => $class;
}
#pod =method create_host
#pod
#pod my $domain_part = Email::MessageID->create_host;
#pod
#pod This method returns the domain part of the message-id.
#pod
#pod =cut
my $_SYS_HOSTNAME_LONG;
sub create_host {
unless (defined $_SYS_HOSTNAME_LONG) {
$_SYS_HOSTNAME_LONG = (eval { require Sys::Hostname::Long; 1 }) || 0;
require Sys::Hostname unless $_SYS_HOSTNAME_LONG;
}
return $_SYS_HOSTNAME_LONG ? Sys::Hostname::Long::hostname_long()
: Sys::Hostname::hostname();
}
#pod =method create_user
#pod
#pod my $local_part = Email::MessageID->create_user;
#pod
#pod This method returns a unique local part for the message-id. It includes some
#pod random data and some predictable data.
#pod
#pod =cut
my @CHARS = ('A'..'F','a'..'f',0..9);
my %uniq;
sub create_user {
my $noise = join '',
map {; $CHARS[rand @CHARS] } (0 .. (3 + int rand 6));
my $t = time;
my $u = exists $uniq{$t} ? ++$uniq{$t} : (%uniq = ($t => 0))[1];
my $user = join '.', $t . $u, $noise, $$;
return $user;
}
#pod =method in_brackets
#pod
#pod When using Email::MessageID directly to populate the C<Message-ID> field, be
#pod sure to use C<in_brackets> to get the string inside angle brackets:
#pod
#pod header => [
#pod ...
#pod 'Message-Id' => Email::MessageID->new->in_brackets,
#pod ],
#pod
#pod Don't make this common mistake:
#pod
#pod header => [
#pod ...
#pod 'Message-Id' => Email::MessageID->new->as_string, # WRONG!
view all matches for this distributionview release on metacpan - search on metacpan
( run in 0.583 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )