Email::MessageID - Generate world unique message-ids.


Email-MessageID documentation Contained in the Email-MessageID distribution.

Index


Code Index:

NAME

Top

Email::MessageID - Generate world unique message-ids.

SYNOPSIS

Top

  use Email::MessageID;

  my $mid = Email::MessageID->new->in_brackets;

  print "Message-ID: $mid\x0A\x0D";

DESCRIPTION

Top

Message-ids are optional, but highly recommended, headers that identify a message uniquely. This software generates a unique message-id.

METHODS

Top

new

  my $mid = Email::MessageID->new;

  my $new_mid = Email::MessageID->new( host => $myhost );

This class method constructs an Email::Address object containing a unique message-id. You may specify custom host and user parameters.

By default, the host is generated from Sys::Hostname::hostname.

By default, the user is generated using Time::HiRes's gettimeofday and the process ID.

Using these values we have the ability to ensure world uniqueness down to a specific process running on a specific host, and the exact time down to six digits of microsecond precision.

create_host

  my $domain_part = Email::MessageID->create_host;

This method returns the domain part of the message-id.

create_user

  my $local_part = Email::MessageID->create_user;

This method returns a unique local part for the message-id. It includes some random data and some predictable data.

in_brackets

When using Email::MessageID directly to populate the Message-ID field, be sure to use in_brackets to get the string inside angle brackets:



  header => [
    ...
    'Message-Id' => Email::MessageID->new->in_brackets,
  ],

Don't make this common mistake:

  header => [
    ...
    'Message-Id' => Email::MessageID->new->as_string, # WRONG!
  ],




SEE ALSO

Top

Email::Address, Time::HiRes, Sys::Hostname, perl.

PERL EMAIL PROJECT

Top

This module is maintained by the Perl Email Project.

http://emailproject.perl.org/wiki/Email::MessageID

AUTHOR

Top

Casey West, <casey@geeknest.com>.

COPYRIGHT

Top


Email-MessageID documentation Contained in the Email-MessageID distribution.
use strict;
use warnings;
package Email::MessageID;
use base 'Email::Address';

our $VERSION = '1.402';

sub new {
    my ($class, %args) = @_;
    
    $args{user} ||= $class->create_user;
    $args{host} ||= $class->create_host;
        
    my $mid = join '@', @args{qw[user host]};
    
    my $addr = Email::Address->new(undef, $mid);

    bless $addr => $class;
}

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()
                               : Sys::Hostname::hostname();
}

my @CHARS = ('A'..'F','a'..'f',0..9);

my $unique_value = 0;
sub _generate_string {
    my $length = 3;
    $length = rand(8) until $length > 3;
    
    join '', (map $CHARS[rand $#CHARS], 0 .. $length), $unique_value++;
}

sub create_user {
    my $pseudo_random = $_[0]->_generate_string;
    my $user = join '.', time, $pseudo_random, $$;
    return $user;
}

sub in_brackets {
    my ($self) = @_;
    return sprintf '<%s>', $self->as_string;
}

1;

__END__